blob: a06f02fe0ac5969346abca4c673dd0b96115609e [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 Grajciar053204a2019-03-18 13:17:53 +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) \
Matthew Smithe0792fd2019-07-12 11:48:24 -050071_(SW_INTERFACE_ADD_DEL_MAC_ADDRESS, sw_interface_add_del_mac_address) \
Neale Rannsb8d44812017-11-10 06:53:54 -080072_(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address) \
Juraj Slobodac0374232018-02-01 15:18:49 +010073_(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address) \
Neale Rannsb8d44812017-11-10 06:53:54 -080074_(CREATE_VLAN_SUBIF, create_vlan_subif) \
75_(CREATE_SUBIF, create_subif) \
76_(DELETE_SUBIF, delete_subif) \
77_(CREATE_LOOPBACK, create_loopback) \
78_(CREATE_LOOPBACK_INSTANCE, create_loopback_instance) \
79_(DELETE_LOOPBACK, delete_loopback) \
Neale Ranns6f4a6be2018-03-16 16:26:21 -070080_(INTERFACE_NAME_RENUMBER, interface_name_renumber) \
Neale Ranns1855b8e2018-07-11 10:31:26 -070081_(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats) \
Neale Ranns59f71132020-04-08 12:19:38 +000082_(SW_INTERFACE_SET_IP_DIRECTED_BROADCAST, \
83 sw_interface_set_ip_directed_broadcast) \
84_(SW_INTERFACE_ADDRESS_REPLACE_BEGIN, \
85 sw_interface_address_replace_begin) \
86_(SW_INTERFACE_ADDRESS_REPLACE_END, \
87 sw_interface_address_replace_end)
Dave Barachaff70772016-10-31 11:59:07 -040088
89static void
90vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
91{
92 vl_api_sw_interface_set_flags_reply_t *rmp;
93 vnet_main_t *vnm = vnet_get_main ();
94 int rv = 0;
95 clib_error_t *error;
96 u16 flags;
97
98 VALIDATE_SW_IF_INDEX (mp);
99
Jakub Grajciar053204a2019-03-18 13:17:53 +0100100 flags =
101 ((ntohl (mp->flags)) & IF_STATUS_API_FLAG_ADMIN_UP) ?
102 VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
Dave Barachaff70772016-10-31 11:59:07 -0400103
104 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
105 if (error)
106 {
107 rv = -1;
108 clib_error_report (error);
109 }
110
111 BAD_SW_IF_INDEX_LABEL;
112 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
113}
114
Matus Fabiand162f3d2016-12-05 01:05:35 -0800115static void
Ole Troand7231612018-06-07 10:17:57 +0200116vl_api_hw_interface_set_mtu_t_handler (vl_api_hw_interface_set_mtu_t * mp)
Matus Fabiand162f3d2016-12-05 01:05:35 -0800117{
Ole Troand7231612018-06-07 10:17:57 +0200118 vl_api_hw_interface_set_mtu_reply_t *rmp;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800119 vnet_main_t *vnm = vnet_get_main ();
Matus Fabiand162f3d2016-12-05 01:05:35 -0800120 u32 sw_if_index = ntohl (mp->sw_if_index);
121 u16 mtu = ntohs (mp->mtu);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200122 ethernet_main_t *em = &ethernet_main;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800123 int rv = 0;
124
125 VALIDATE_SW_IF_INDEX (mp);
126
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200127 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
128 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
129 {
130 rv = VNET_API_ERROR_INVALID_VALUE;
131 goto bad_sw_if_index;
132 }
133
134 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
135 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
136
137 if (!eif)
138 {
139 rv = VNET_API_ERROR_FEATURE_DISABLED;
140 goto bad_sw_if_index;
141 }
142
143 if (mtu < hi->min_supported_packet_bytes)
144 {
145 rv = VNET_API_ERROR_INVALID_VALUE;
146 goto bad_sw_if_index;
147 }
148
149 if (mtu > hi->max_supported_packet_bytes)
150 {
151 rv = VNET_API_ERROR_INVALID_VALUE;
152 goto bad_sw_if_index;
153 }
154
155 vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu);
Matus Fabiand162f3d2016-12-05 01:05:35 -0800156
157 BAD_SW_IF_INDEX_LABEL;
Ole Troand7231612018-06-07 10:17:57 +0200158 REPLY_MACRO (VL_API_HW_INTERFACE_SET_MTU_REPLY);
159}
160
161static void
162vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
163{
164 vl_api_sw_interface_set_mtu_reply_t *rmp;
165 vnet_main_t *vnm = vnet_get_main ();
166 u32 sw_if_index = ntohl (mp->sw_if_index);
167 int rv = 0;
168 int i;
169 u32 per_protocol_mtu[VNET_N_MTU];
170
171 VALIDATE_SW_IF_INDEX (mp);
172
173 for (i = 0; i < VNET_N_MTU; i++)
Ole Troan33a58172019-09-04 09:12:29 +0200174 {
175 per_protocol_mtu[i] = ntohl (mp->mtu[i]);
Ole Troan33a58172019-09-04 09:12:29 +0200176 }
Ole Troand7231612018-06-07 10:17:57 +0200177 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, per_protocol_mtu);
178
179 BAD_SW_IF_INDEX_LABEL;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800180 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
181}
182
Dave Barach6d963c22016-12-05 09:50:05 -0500183static void
Neale Ranns1855b8e2018-07-11 10:31:26 -0700184 vl_api_sw_interface_set_ip_directed_broadcast_t_handler
185 (vl_api_sw_interface_set_ip_directed_broadcast_t * mp)
186{
187 vl_api_sw_interface_set_ip_directed_broadcast_reply_t *rmp;
188 u32 sw_if_index = ntohl (mp->sw_if_index);
189 int rv = 0;
190
191 VALIDATE_SW_IF_INDEX (mp);
192
193 vnet_sw_interface_ip_directed_broadcast (vnet_get_main (),
194 sw_if_index, mp->enable);
195
196 BAD_SW_IF_INDEX_LABEL;
197 REPLY_MACRO (VL_API_SW_INTERFACE_SET_IP_DIRECTED_BROADCAST_REPLY);
198}
199
200static void
Dave Barach6d963c22016-12-05 09:50:05 -0500201send_sw_interface_details (vpe_api_main_t * am,
Dave Barach59b25652017-09-10 15:04:27 -0400202 vl_api_registration_t * rp,
Dave Barach6d963c22016-12-05 09:50:05 -0500203 vnet_sw_interface_t * swif,
204 u8 * interface_name, u32 context)
205{
Eyal Bari1c82cd42017-03-14 14:39:51 +0200206 vnet_hw_interface_t *hi =
207 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
Mohsin Kazmide312c22019-09-27 13:44:28 +0200208 vnet_device_class_t *dev_class =
209 vnet_get_device_class (am->vnet_main, hi->dev_class_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500210
Ole Troane5ff5a32019-08-23 22:55:18 +0200211 vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
212 clib_memset (mp, 0, sizeof (*mp));
Dave Barach6d963c22016-12-05 09:50:05 -0500213 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
214 mp->sw_if_index = ntohl (swif->sw_if_index);
215 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
Jakub Grajciar053204a2019-03-18 13:17:53 +0100216
217 mp->flags |= (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
218 IF_STATUS_API_FLAG_ADMIN_UP : 0;
219 mp->flags |= (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
220 IF_STATUS_API_FLAG_LINK_UP : 0;
221 mp->flags = ntohl (mp->flags);
222
223 switch (swif->type)
224 {
225 case VNET_SW_INTERFACE_TYPE_SUB:
226 mp->type = IF_API_TYPE_SUB;
227 break;
228 case VNET_SW_INTERFACE_TYPE_P2P:
229 mp->type = IF_API_TYPE_P2P;
230 break;
231 case VNET_SW_INTERFACE_TYPE_PIPE:
232 mp->type = IF_API_TYPE_PIPE;
233 break;
234 default:
235 mp->type = IF_API_TYPE_HARDWARE;
236 }
237 mp->type = ntohl (mp->type);
238
Dave Barach6d963c22016-12-05 09:50:05 -0500239 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
240 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
Ole Troan8ee72512018-11-20 11:03:10 +0100241 mp->link_speed = ntohl (hi->link_speed);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200242 mp->link_mtu = ntohs (hi->max_packet_bytes);
Ole Troand7231612018-06-07 10:17:57 +0200243 mp->mtu[VNET_MTU_L3] = ntohl (swif->mtu[VNET_MTU_L3]);
244 mp->mtu[VNET_MTU_IP4] = ntohl (swif->mtu[VNET_MTU_IP4]);
245 mp->mtu[VNET_MTU_IP6] = ntohl (swif->mtu[VNET_MTU_IP6]);
246 mp->mtu[VNET_MTU_MPLS] = ntohl (swif->mtu[VNET_MTU_MPLS]);
247
Dave Barach6d963c22016-12-05 09:50:05 -0500248 mp->context = context;
249
Ole Troane5ff5a32019-08-23 22:55:18 +0200250 strncpy ((char *) mp->interface_name,
251 (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
252
Mohsin Kazmide312c22019-09-27 13:44:28 +0200253 if (dev_class && dev_class->name)
254 strncpy ((char *) mp->interface_dev_type, (char *) dev_class->name,
255 ARRAY_LEN (mp->interface_dev_type) - 1);
256
Dave Barach6d963c22016-12-05 09:50:05 -0500257 /* Send the L2 address for ethernet physical intfcs */
258 if (swif->sup_sw_if_index == swif->sw_if_index
259 && hi->hw_class_index == ethernet_hw_interface_class.index)
260 {
261 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
262 ethernet_interface_t *ei;
263
264 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
Benoît Ganneb44c77d2020-10-20 16:24:17 +0200265 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address.mac));
266 mac_address_encode (&ei->address.mac, mp->l2_address);
Dave Barach6d963c22016-12-05 09:50:05 -0500267 }
268 else if (swif->sup_sw_if_index != swif->sw_if_index)
269 {
270 vnet_sub_interface_t *sub = &swif->sub;
271 mp->sub_id = ntohl (sub->id);
Dave Barach6d963c22016-12-05 09:50:05 -0500272 mp->sub_number_of_tags =
273 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
274 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
275 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
Jakub Grajciar053204a2019-03-18 13:17:53 +0100276 mp->sub_if_flags =
277 ntohl (sub->eth.raw_flags & SUB_IF_API_FLAG_MASK_VNET);
Jon Loeliger9485d992019-11-08 15:05:23 -0600278 }
Dave Barach6d963c22016-12-05 09:50:05 -0500279
Jon Loeliger9485d992019-11-08 15:05:23 -0600280 /* vlan tag rewrite data */
281 u32 vtr_op = L2_VTR_DISABLED;
282 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500283
Jon Loeliger9485d992019-11-08 15:05:23 -0600284 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
285 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
286 {
287 // error - default to disabled
288 mp->vtr_op = ntohl (L2_VTR_DISABLED);
289 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
290 swif->sw_if_index);
291 }
292 else
293 {
294 mp->vtr_op = ntohl (vtr_op);
295 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
296 mp->vtr_tag1 = ntohl (vtr_tag1);
297 mp->vtr_tag2 = ntohl (vtr_tag2);
Dave Barach6d963c22016-12-05 09:50:05 -0500298 }
299
Pavel Kotucek65e84572017-01-16 17:01:56 +0100300 /* pbb tag rewrite data */
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100301 ethernet_header_t eth_hdr;
Alexander Chernavinfdea5c62019-04-26 10:43:09 -0400302 u32 pbb_vtr_op = L2_VTR_DISABLED;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100303 u16 outer_tag = 0;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100304 u16 b_vlanid = 0;
305 u32 i_sid = 0;
Dave Barachb7b92992018-10-17 10:38:51 -0400306 clib_memset (&eth_hdr, 0, sizeof (eth_hdr));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100307
308 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
Alexander Chernavinfdea5c62019-04-26 10:43:09 -0400309 &pbb_vtr_op, &outer_tag, &eth_hdr, &b_vlanid, &i_sid))
Pavel Kotucek65e84572017-01-16 17:01:56 +0100310 {
Jakub Grajciar053204a2019-03-18 13:17:53 +0100311 mp->sub_if_flags |= ntohl (SUB_IF_API_FLAG_DOT1AH);
312 mac_address_encode ((mac_address_t *) eth_hdr.dst_address, mp->b_dmac);
313 mac_address_encode ((mac_address_t *) eth_hdr.src_address, mp->b_smac);
Pavel Kotucek65e84572017-01-16 17:01:56 +0100314 mp->b_vlanid = b_vlanid;
315 mp->i_sid = i_sid;
316 }
317
Ole Troane5ff5a32019-08-23 22:55:18 +0200318 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
319 if (tag)
320 strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500321
Florin Corase86a8ed2018-01-05 03:20:25 -0800322 vl_api_send_msg (rp, (u8 *) mp);
Dave Barach6d963c22016-12-05 09:50:05 -0500323}
324
325static void
326vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
327{
328 vpe_api_main_t *am = &vpe_api_main;
329 vnet_sw_interface_t *swif;
330 vnet_interface_main_t *im = &am->vnet_main->interface_main;
Dave Barach59b25652017-09-10 15:04:27 -0400331 vl_api_registration_t *rp;
Paul Vinciguerra6407ba52019-04-04 13:22:20 -0700332 u32 sw_if_index;
Dave Barach6d963c22016-12-05 09:50:05 -0500333
Dave Barach59b25652017-09-10 15:04:27 -0400334 rp = vl_api_client_index_to_registration (mp->client_index);
335
336 if (rp == 0)
337 {
338 clib_warning ("Client %d AWOL", mp->client_index);
339 return;
340 }
Dave Barach6d963c22016-12-05 09:50:05 -0500341
Eyal Bari1c82cd42017-03-14 14:39:51 +0200342 u8 *filter = 0, *name = 0;
Paul Vinciguerra6407ba52019-04-04 13:22:20 -0700343 sw_if_index = ntohl (mp->sw_if_index);
344
Vratko Polakb8591ac2019-04-23 12:26:44 +0200345 if (!mp->name_filter_valid && sw_if_index != ~0 && sw_if_index != 0)
Paul Vinciguerra6407ba52019-04-04 13:22:20 -0700346 {
Vratko Polakb8591ac2019-04-23 12:26:44 +0200347 /* is it a valid sw_if_index? */
Neale Rannsf5b01762019-06-14 08:26:21 +0000348 if (!vnet_sw_if_index_is_api_valid (sw_if_index))
Paul Vinciguerra6407ba52019-04-04 13:22:20 -0700349 return;
350
351 swif = vec_elt_at_index (im->sw_interfaces, sw_if_index);
352
Paul Vinciguerra6407ba52019-04-04 13:22:20 -0700353 vec_reset_length (name);
354 name =
355 format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
356 swif, 0);
357 send_sw_interface_details (am, rp, swif, name, mp->context);
358 vec_free (name);
359 return;
360 }
361
Dave Barach6d963c22016-12-05 09:50:05 -0500362 if (mp->name_filter_valid)
363 {
Dave Barach77841402020-04-29 17:04:10 -0400364 filter = vl_api_from_api_to_new_vec (mp, &mp->name_filter);
Aleksander Djuricc12eae72019-10-31 14:35:21 +0300365 vec_add1 (filter, 0); /* Ensure it's a C string for strcasecmp() */
Dave Barach6d963c22016-12-05 09:50:05 -0500366 }
367
Eyal Bari1c82cd42017-03-14 14:39:51 +0200368 char *strcasestr (char *, char *); /* lnx hdr file botch */
Dave Barach6d963c22016-12-05 09:50:05 -0500369 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100370 pool_foreach (swif, im->sw_interfaces)
371 {
Eyal Bari1c82cd42017-03-14 14:39:51 +0200372 if (!vnet_swif_is_api_visible (swif))
373 continue;
374 vec_reset_length(name);
375 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
376 swif, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500377
Eyal Bari1c82cd42017-03-14 14:39:51 +0200378 if (filter && !strcasestr((char *) name, (char *) filter))
379 continue;
Dave Barach6d963c22016-12-05 09:50:05 -0500380
Dave Barach59b25652017-09-10 15:04:27 -0400381 send_sw_interface_details (am, rp, swif, name, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100382 }
Dave Barach6d963c22016-12-05 09:50:05 -0500383 /* *INDENT-ON* */
384
Eyal Bari1c82cd42017-03-14 14:39:51 +0200385 vec_free (name);
386 vec_free (filter);
Dave Barach6d963c22016-12-05 09:50:05 -0500387}
388
389static void
390 vl_api_sw_interface_add_del_address_t_handler
391 (vl_api_sw_interface_add_del_address_t * mp)
392{
393 vlib_main_t *vm = vlib_get_main ();
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500394 vnet_main_t *vnm = vnet_get_main ();
Dave Barach6d963c22016-12-05 09:50:05 -0500395 vl_api_sw_interface_add_del_address_reply_t *rmp;
396 int rv = 0;
397 u32 is_del;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500398 clib_error_t *error = 0;
Jakub Grajciar053204a2019-03-18 13:17:53 +0100399 ip46_address_t address;
Dave Barach6d963c22016-12-05 09:50:05 -0500400
401 VALIDATE_SW_IF_INDEX (mp);
402
403 is_del = mp->is_add == 0;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500404 vnm->api_errno = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500405
406 if (mp->del_all)
407 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
Jakub Grajciar053204a2019-03-18 13:17:53 +0100408 else if (ip_address_decode (&mp->prefix.address, &address) == IP46_TYPE_IP6)
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500409 error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
Jakub Grajciar053204a2019-03-18 13:17:53 +0100410 (void *) &address.ip6,
411 mp->prefix.len, is_del);
Dave Barach6d963c22016-12-05 09:50:05 -0500412 else
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500413 error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
Jakub Grajciar053204a2019-03-18 13:17:53 +0100414 (void *) &address.ip4,
415 mp->prefix.len, is_del);
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500416
417 if (error)
418 {
419 rv = vnm->api_errno;
420 clib_error_report (error);
421 goto done;
422 }
Dave Barach6d963c22016-12-05 09:50:05 -0500423
424 BAD_SW_IF_INDEX_LABEL;
425
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500426done:
Dave Barach6d963c22016-12-05 09:50:05 -0500427 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
428}
429
Dave Barach6d963c22016-12-05 09:50:05 -0500430static void
431vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
432{
Dave Barach6d963c22016-12-05 09:50:05 -0500433 vl_api_sw_interface_set_table_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700434 u32 sw_if_index = ntohl (mp->sw_if_index);
435 u32 table_id = ntohl (mp->vrf_id);
436 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500437
438 VALIDATE_SW_IF_INDEX (mp);
439
Dave Barach6d963c22016-12-05 09:50:05 -0500440 if (mp->is_ipv6)
Neale Ranns15002542017-09-10 04:39:11 -0700441 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500442 else
Neale Ranns15002542017-09-10 04:39:11 -0700443 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500444
Dave Barach6d963c22016-12-05 09:50:05 -0500445 BAD_SW_IF_INDEX_LABEL;
446
447 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
448}
449
Neale Ranns15002542017-09-10 04:39:11 -0700450int
451ip_table_bind (fib_protocol_t fproto,
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700452 u32 sw_if_index, u32 table_id, u8 is_api)
Neale Ranns15002542017-09-10 04:39:11 -0700453{
454 CLIB_UNUSED (ip_interface_address_t * ia);
455 u32 fib_index, mfib_index;
456 fib_source_t src;
457 mfib_source_t msrc;
458
459 if (is_api)
460 {
461 src = FIB_SOURCE_API;
462 msrc = MFIB_SOURCE_API;
463 }
464 else
465 {
466 src = FIB_SOURCE_CLI;
467 msrc = MFIB_SOURCE_CLI;
468 }
469
Florin Corasd0a59722017-10-15 17:41:21 +0000470 /*
Florin Corasd0a59722017-10-15 17:41:21 +0000471 * This if table does not exist = error is what we want in the end.
472 */
Neale Ranns8f6dd322018-05-17 06:34:24 -0700473 fib_index = fib_table_find (fproto, table_id);
474 mfib_index = mfib_table_find (fproto, table_id);
Florin Corasd0a59722017-10-15 17:41:21 +0000475
Neale Ranns8f6dd322018-05-17 06:34:24 -0700476 if (~0 == fib_index || ~0 == mfib_index)
477 {
478 return (VNET_API_ERROR_NO_SUCH_FIB);
479 }
Florin Corasd0a59722017-10-15 17:41:21 +0000480
Neale Ranns15002542017-09-10 04:39:11 -0700481 if (FIB_PROTOCOL_IP6 == fproto)
482 {
483 /*
484 * If the interface already has in IP address, then a change int
485 * VRF is not allowed. The IP address applied must first be removed.
486 * We do not do that automatically here, since VPP has no knowledge
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700487 * of whether those subnets are valid in the destination VRF.
Neale Ranns15002542017-09-10 04:39:11 -0700488 */
489 /* *INDENT-OFF* */
490 foreach_ip_interface_address (&ip6_main.lookup_main,
491 ia, sw_if_index,
492 1 /* honor unnumbered */ ,
493 ({
494 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
495 }));
496 /* *INDENT-ON* */
497
498 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
499 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
500
501 /*
502 * tell those that are interested that the binding is changing.
503 */
504 ip6_table_bind_callback_t *cb;
505 vec_foreach (cb, ip6_main.table_bind_callbacks)
506 cb->function (&ip6_main, cb->function_opaque,
507 sw_if_index,
508 fib_index,
509 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
510
511 if (0 == table_id)
512 {
513 /* reset back to default */
514 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
515 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
516 FIB_PROTOCOL_IP6, src);
517 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
518 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
519 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
520
521 }
522 else
523 {
524 /* we need to lock the table now it's inuse */
525 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
526 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
527 }
528
529 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
530 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
531 }
532 else
533 {
534 /*
535 * If the interface already has in IP address, then a change int
536 * VRF is not allowed. The IP address applied must first be removed.
537 * We do not do that automatically here, since VPP has no knowledge
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700538 * of whether those subnets are valid in the destination VRF.
Neale Ranns15002542017-09-10 04:39:11 -0700539 */
540 /* *INDENT-OFF* */
541 foreach_ip_interface_address (&ip4_main.lookup_main,
542 ia, sw_if_index,
543 1 /* honor unnumbered */ ,
544 ({
545 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
546 }));
547 /* *INDENT-ON* */
548
549 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
550 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
551
552 /*
553 * tell those that are interested that the binding is changing.
554 */
555 ip4_table_bind_callback_t *cb;
556 vec_foreach (cb, ip4_main.table_bind_callbacks)
557 cb->function (&ip4_main, cb->function_opaque,
558 sw_if_index,
559 fib_index,
560 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
561
562 if (0 == table_id)
563 {
564 /* reset back to default */
565 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
566 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
567 FIB_PROTOCOL_IP4, src);
568 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
569 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
570 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
571
572 }
573 else
574 {
575 /* we need to lock the table now it's inuse */
576 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
577 table_id, src);
578
579 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
580 table_id, msrc);
581 }
582
583 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
584 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
585 }
586
Neale Ranns15002542017-09-10 04:39:11 -0700587 return (0);
588}
589
Juraj Slobodadfc19232016-12-05 13:20:37 +0100590static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800591send_sw_interface_get_table_reply (vl_api_registration_t * reg,
Juraj Slobodadfc19232016-12-05 13:20:37 +0100592 u32 context, int retval, u32 vrf_id)
593{
594 vl_api_sw_interface_get_table_reply_t *mp;
595
596 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400597 clib_memset (mp, 0, sizeof (*mp));
Juraj Slobodadfc19232016-12-05 13:20:37 +0100598 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
599 mp->context = context;
600 mp->retval = htonl (retval);
601 mp->vrf_id = htonl (vrf_id);
602
Florin Coras6c4dae22018-01-09 06:39:23 -0800603 vl_api_send_msg (reg, (u8 *) mp);
Juraj Slobodadfc19232016-12-05 13:20:37 +0100604}
605
606static void
607vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
608{
Florin Coras6c4dae22018-01-09 06:39:23 -0800609 vl_api_registration_t *reg;
Juraj Slobodadfc19232016-12-05 13:20:37 +0100610 fib_table_t *fib_table = 0;
611 u32 sw_if_index = ~0;
612 u32 fib_index = ~0;
613 u32 table_id = ~0;
614 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
615 int rv = 0;
616
Florin Coras6c4dae22018-01-09 06:39:23 -0800617 reg = vl_api_client_index_to_registration (mp->client_index);
618 if (!reg)
Juraj Slobodadfc19232016-12-05 13:20:37 +0100619 return;
620
621 VALIDATE_SW_IF_INDEX (mp);
622
623 sw_if_index = ntohl (mp->sw_if_index);
624
625 if (mp->is_ipv6)
626 fib_proto = FIB_PROTOCOL_IP6;
627
628 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
629 if (fib_index != ~0)
630 {
631 fib_table = fib_table_get (fib_index, fib_proto);
632 table_id = fib_table->ft_table_id;
633 }
634
635 BAD_SW_IF_INDEX_LABEL;
636
Florin Coras6c4dae22018-01-09 06:39:23 -0800637 send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
Juraj Slobodadfc19232016-12-05 13:20:37 +0100638}
639
Dave Barach6d963c22016-12-05 09:50:05 -0500640static void vl_api_sw_interface_set_unnumbered_t_handler
641 (vl_api_sw_interface_set_unnumbered_t * mp)
642{
643 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
644 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500645 vnet_main_t *vnm = vnet_get_main ();
Eyal Bari1c82cd42017-03-14 14:39:51 +0200646 u32 sw_if_index = ntohl (mp->sw_if_index);
647 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500648
649 /*
650 * The API message field names are backwards from
651 * the underlying data structure names.
652 * It's not worth changing them now.
653 */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200654 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500655 {
656 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
657 goto done;
658 }
659
660 /* Only check the "use loop0" field when setting the binding */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200661 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500662 {
663 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
664 goto done;
665 }
666
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700667 vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
668 sw_if_index, mp->is_add);
Dave Barach6d963c22016-12-05 09:50:05 -0500669done:
670 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
671}
672
673static void
674vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
675 mp)
676{
677 vl_api_sw_interface_clear_stats_reply_t *rmp;
678
679 vnet_main_t *vnm = vnet_get_main ();
680 vnet_interface_main_t *im = &vnm->interface_main;
681 vlib_simple_counter_main_t *sm;
682 vlib_combined_counter_main_t *cm;
Dave Barach8fdde3c2019-05-17 10:46:40 -0400683 int j, n_counters;
Dave Barach6d963c22016-12-05 09:50:05 -0500684 int rv = 0;
685
686 if (mp->sw_if_index != ~0)
687 VALIDATE_SW_IF_INDEX (mp);
688
Dave Barach6d963c22016-12-05 09:50:05 -0500689 n_counters = vec_len (im->combined_sw_if_counters);
690
691 for (j = 0; j < n_counters; j++)
692 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400693 im = &vnm->interface_main;
694 cm = im->combined_sw_if_counters + j;
695 if (mp->sw_if_index == (u32) ~ 0)
696 vlib_clear_combined_counters (cm);
697 else
698 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
Dave Barach6d963c22016-12-05 09:50:05 -0500699 }
700
701 n_counters = vec_len (im->sw_if_counters);
702
703 for (j = 0; j < n_counters; j++)
704 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400705 im = &vnm->interface_main;
706 sm = im->sw_if_counters + j;
707 if (mp->sw_if_index == (u32) ~ 0)
708 vlib_clear_simple_counters (sm);
709 else
710 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
Dave Barach6d963c22016-12-05 09:50:05 -0500711 }
712
713 BAD_SW_IF_INDEX_LABEL;
714
715 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
716}
717
Ole Troan2e1c8962019-04-10 09:44:23 +0200718/*
719 * Events used for sw_interface_events
720 */
721enum api_events
Dave Barach6d963c22016-12-05 09:50:05 -0500722{
Ole Troan2e1c8962019-04-10 09:44:23 +0200723 API_LINK_STATE_UP_EVENT = 1 << 1,
724 API_LINK_STATE_DOWN_EVENT = 1 << 2,
725 API_ADMIN_UP_EVENT = 1 << 3,
726 API_ADMIN_DOWN_EVENT = 1 << 4,
727 API_SW_INTERFACE_ADD_EVENT = 1 << 5,
728 API_SW_INTERFACE_DEL_EVENT = 1 << 6,
729};
Dave Barach6d963c22016-12-05 09:50:05 -0500730
731static void
Neale Rannsa07bd702017-08-07 07:53:49 -0700732send_sw_interface_event (vpe_api_main_t * am,
Neale Rannsd292ab12017-08-15 12:29:48 -0700733 vpe_client_registration_t * reg,
Florin Coras6c4dae22018-01-09 06:39:23 -0800734 vl_api_registration_t * vl_reg,
Ole Troan2e1c8962019-04-10 09:44:23 +0200735 u32 sw_if_index, enum api_events events)
Dave Barach6d963c22016-12-05 09:50:05 -0500736{
Neale Rannsa07bd702017-08-07 07:53:49 -0700737 vl_api_sw_interface_event_t *mp;
Dave Barach6d963c22016-12-05 09:50:05 -0500738
Dave Barach6d963c22016-12-05 09:50:05 -0500739 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400740 clib_memset (mp, 0, sizeof (*mp));
Neale Rannsa07bd702017-08-07 07:53:49 -0700741 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
Ole Troan2e1c8962019-04-10 09:44:23 +0200742 mp->sw_if_index = ntohl (sw_if_index);
Neale Rannsd292ab12017-08-15 12:29:48 -0700743 mp->client_index = reg->client_index;
744 mp->pid = reg->client_pid;
Jakub Grajciar053204a2019-03-18 13:17:53 +0100745 mp->flags = 0;
746 mp->flags |= (events & API_ADMIN_UP_EVENT) ?
747 IF_STATUS_API_FLAG_ADMIN_UP : 0;
748 mp->flags |= (events & API_LINK_STATE_UP_EVENT) ?
749 IF_STATUS_API_FLAG_LINK_UP : 0;
750 mp->flags = ntohl (mp->flags);
751 mp->deleted = events & API_SW_INTERFACE_DEL_EVENT ? true : false;
Florin Coras6c4dae22018-01-09 06:39:23 -0800752 vl_api_send_msg (vl_reg, (u8 *) mp);
Dave Barach6d963c22016-12-05 09:50:05 -0500753}
754
755static uword
756link_state_process (vlib_main_t * vm,
757 vlib_node_runtime_t * rt, vlib_frame_t * f)
758{
759 vpe_api_main_t *vam = &vpe_api_main;
Ole Troan2e1c8962019-04-10 09:44:23 +0200760 uword *event_by_sw_if_index = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500761 vpe_client_registration_t *reg;
762 int i;
Florin Coras6c4dae22018-01-09 06:39:23 -0800763 vl_api_registration_t *vl_reg;
Ole Troan2e1c8962019-04-10 09:44:23 +0200764 uword event_type;
765 uword *event_data = 0;
766 u32 sw_if_index;
Dave Barach6d963c22016-12-05 09:50:05 -0500767
768 vam->link_state_process_up = 1;
769
770 while (1)
771 {
772 vlib_process_wait_for_event (vm);
773
Ole Troan2e1c8962019-04-10 09:44:23 +0200774 /* Batch up events */
775 while ((event_type = vlib_process_get_events (vm, &event_data)) != ~0)
Dave Barach6d963c22016-12-05 09:50:05 -0500776 {
Ole Troan2e1c8962019-04-10 09:44:23 +0200777 for (i = 0; i < vec_len (event_data); i++)
778 {
779 sw_if_index = event_data[i];
780 vec_validate_init_empty (event_by_sw_if_index, sw_if_index, 0);
781 event_by_sw_if_index[sw_if_index] |= event_type;
782 }
783 vec_reset_length (event_data);
784 }
785
786 for (i = 0; i < vec_len (event_by_sw_if_index); i++)
787 {
788 if (event_by_sw_if_index[i] == 0)
Dave Barach6d963c22016-12-05 09:50:05 -0500789 continue;
Dave Barach6d963c22016-12-05 09:50:05 -0500790
791 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100792 pool_foreach (reg, vam->interface_events_registrations)
793 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800794 vl_reg = vl_api_client_index_to_registration (reg->client_index);
795 if (vl_reg)
Ole Troan2e1c8962019-04-10 09:44:23 +0200796 send_sw_interface_event (vam, reg, vl_reg, i, event_by_sw_if_index[i]);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100797 }
Dave Barach6d963c22016-12-05 09:50:05 -0500798 /* *INDENT-ON* */
799 }
Ole Troan2e1c8962019-04-10 09:44:23 +0200800 vec_reset_length (event_by_sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500801 }
802
803 return 0;
804}
805
806static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
807 u32 flags);
808static clib_error_t *admin_up_down_function (vnet_main_t * vm,
809 u32 hw_if_index, u32 flags);
Ole Troan2e1c8962019-04-10 09:44:23 +0200810static clib_error_t *sw_interface_add_del_function (vnet_main_t * vm,
811 u32 sw_if_index,
812 u32 flags);
Dave Barach6d963c22016-12-05 09:50:05 -0500813
814/* *INDENT-OFF* */
815VLIB_REGISTER_NODE (link_state_process_node,static) = {
816 .function = link_state_process,
817 .type = VLIB_NODE_TYPE_PROCESS,
818 .name = "vpe-link-state-process",
819};
820/* *INDENT-ON* */
821
822VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
823VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
Ole Troan2e1c8962019-04-10 09:44:23 +0200824VNET_SW_INTERFACE_ADD_DEL_FUNCTION (sw_interface_add_del_function);
Dave Barach6d963c22016-12-05 09:50:05 -0500825
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)
Ole Troan2e1c8962019-04-10 09:44:23 +0200833 {
Alexander Chernavinaa27dcb2019-11-06 09:58:18 -0500834 enum api_events event = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ?
835 API_LINK_STATE_UP_EVENT :
836 API_LINK_STATE_DOWN_EVENT);
Ole Troan2e1c8962019-04-10 09:44:23 +0200837 vlib_process_signal_event (vam->vlib_main,
838 link_state_process_node.index, event,
839 hi->sw_if_index);
840 }
Dave Barach6d963c22016-12-05 09:50:05 -0500841 return 0;
842}
843
844static clib_error_t *
845admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
846{
847 vpe_api_main_t *vam = &vpe_api_main;
848
849 /*
850 * Note: it's perfectly fair to set a subif admin up / admin down.
851 * Note the subtle distinction between this routine and the previous
852 * routine.
853 */
854 if (vam->link_state_process_up)
Ole Troan2e1c8962019-04-10 09:44:23 +0200855 {
Alexander Chernavinaa27dcb2019-11-06 09:58:18 -0500856 enum api_events event = ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
857 API_ADMIN_UP_EVENT : API_ADMIN_DOWN_EVENT);
Ole Troan2e1c8962019-04-10 09:44:23 +0200858 vlib_process_signal_event (vam->vlib_main,
859 link_state_process_node.index, event,
860 sw_if_index);
861 }
862 return 0;
863}
864
865static clib_error_t *
866sw_interface_add_del_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
867{
868 vpe_api_main_t *vam = &vpe_api_main;
869
870 if (vam->link_state_process_up)
871 {
872 enum api_events event =
873 flags ? API_SW_INTERFACE_ADD_EVENT : API_SW_INTERFACE_DEL_EVENT;
874 vlib_process_signal_event (vam->vlib_main,
875 link_state_process_node.index, event,
876 sw_if_index);
877 }
Dave Barach6d963c22016-12-05 09:50:05 -0500878 return 0;
879}
880
881static void vl_api_sw_interface_tag_add_del_t_handler
882 (vl_api_sw_interface_tag_add_del_t * mp)
883{
884 vnet_main_t *vnm = vnet_get_main ();
885 vl_api_sw_interface_tag_add_del_reply_t *rmp;
886 int rv = 0;
887 u8 *tag;
888 u32 sw_if_index = ntohl (mp->sw_if_index);
889
890 VALIDATE_SW_IF_INDEX (mp);
891
892 if (mp->is_add)
893 {
Ole Troane5ff5a32019-08-23 22:55:18 +0200894 if (mp->tag[0] == 0)
Dave Barach6d963c22016-12-05 09:50:05 -0500895 {
896 rv = VNET_API_ERROR_INVALID_VALUE;
897 goto out;
898 }
899
Ole Troane5ff5a32019-08-23 22:55:18 +0200900 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
901 tag = format (0, "%s%c", mp->tag, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500902 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
903 }
904 else
905 vnet_clear_sw_interface_tag (vnm, sw_if_index);
906
907 BAD_SW_IF_INDEX_LABEL;
908out:
909 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
910}
911
Matthew Smithe0792fd2019-07-12 11:48:24 -0500912static void vl_api_sw_interface_add_del_mac_address_t_handler
913 (vl_api_sw_interface_add_del_mac_address_t * mp)
914{
915 vl_api_sw_interface_add_del_mac_address_reply_t *rmp;
916 vnet_main_t *vnm = vnet_get_main ();
917 u32 sw_if_index = ntohl (mp->sw_if_index);
918 vnet_hw_interface_t *hi;
919 clib_error_t *error;
920 int rv = 0;
921
922 VALIDATE_SW_IF_INDEX (mp);
923
924 /* for subifs, the MAC should be changed on the actual hw if */
925 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
926 error = vnet_hw_interface_add_del_mac_address (vnm, hi->hw_if_index,
927 mp->addr, mp->is_add);
928 if (error)
929 {
930 rv = VNET_API_ERROR_UNIMPLEMENTED;
931 clib_error_report (error);
932 goto out;
933 }
934
935 BAD_SW_IF_INDEX_LABEL;
936out:
937 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_MAC_ADDRESS_REPLY);
938}
939
Jon Loeliger10c273b2017-03-30 08:39:33 -0500940static void vl_api_sw_interface_set_mac_address_t_handler
941 (vl_api_sw_interface_set_mac_address_t * mp)
942{
943 vl_api_sw_interface_set_mac_address_reply_t *rmp;
944 vnet_main_t *vnm = vnet_get_main ();
945 u32 sw_if_index = ntohl (mp->sw_if_index);
Neale Rannsd867a7c2017-10-04 02:29:07 -0700946 vnet_sw_interface_t *si;
Jon Loeliger10c273b2017-03-30 08:39:33 -0500947 clib_error_t *error;
948 int rv = 0;
Jakub Grajciar053204a2019-03-18 13:17:53 +0100949 mac_address_t mac;
Jon Loeliger10c273b2017-03-30 08:39:33 -0500950
951 VALIDATE_SW_IF_INDEX (mp);
952
Neale Rannsd867a7c2017-10-04 02:29:07 -0700953 si = vnet_get_sw_interface (vnm, sw_if_index);
Jakub Grajciar053204a2019-03-18 13:17:53 +0100954 mac_address_decode (mp->mac_address, &mac);
955 error =
956 vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, (u8 *) & mac);
Jon Loeliger10c273b2017-03-30 08:39:33 -0500957 if (error)
958 {
959 rv = VNET_API_ERROR_UNIMPLEMENTED;
960 clib_error_report (error);
961 goto out;
962 }
963
964 BAD_SW_IF_INDEX_LABEL;
965out:
966 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
967}
968
Juraj Slobodac0374232018-02-01 15:18:49 +0100969static void vl_api_sw_interface_get_mac_address_t_handler
970 (vl_api_sw_interface_get_mac_address_t * mp)
971{
972 vl_api_sw_interface_get_mac_address_reply_t *rmp;
973 vl_api_registration_t *reg;
974 vnet_main_t *vnm = vnet_get_main ();
975 u32 sw_if_index = ntohl (mp->sw_if_index);
976 vnet_sw_interface_t *si;
977 ethernet_interface_t *eth_if = 0;
978 int rv = 0;
979
980 VALIDATE_SW_IF_INDEX (mp);
981
982 si = vnet_get_sup_sw_interface (vnm, sw_if_index);
983 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
984 eth_if = ethernet_get_interface (&ethernet_main, si->hw_if_index);
985
986 BAD_SW_IF_INDEX_LABEL;
987
988 reg = vl_api_client_index_to_registration (mp->client_index);
989 if (!reg)
990 return;
991 rmp = vl_msg_api_alloc (sizeof (*rmp));
992 rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
993 rmp->context = mp->context;
994 rmp->retval = htonl (rv);
995 if (!rv && eth_if)
Benoît Ganneb44c77d2020-10-20 16:24:17 +0200996 mac_address_encode (&eth_if->address.mac, rmp->mac_address);
Juraj Slobodac0374232018-02-01 15:18:49 +0100997 vl_api_send_msg (reg, (u8 *) rmp);
998}
999
Stevenad8015b2017-10-29 22:10:46 -07001000static void vl_api_sw_interface_set_rx_mode_t_handler
1001 (vl_api_sw_interface_set_rx_mode_t * mp)
1002{
1003 vl_api_sw_interface_set_rx_mode_reply_t *rmp;
1004 vnet_main_t *vnm = vnet_get_main ();
1005 u32 sw_if_index = ntohl (mp->sw_if_index);
1006 vnet_sw_interface_t *si;
1007 clib_error_t *error;
1008 int rv = 0;
Damjan Marioneabd4242020-10-07 20:59:07 +02001009 vnet_hw_if_rx_mode rx_mode;
Stevenad8015b2017-10-29 22:10:46 -07001010
1011 VALIDATE_SW_IF_INDEX (mp);
1012
1013 si = vnet_get_sw_interface (vnm, sw_if_index);
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001014 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
1015 {
1016 rv = VNET_API_ERROR_INVALID_VALUE;
1017 goto bad_sw_if_index;
1018 }
1019
Damjan Marioneabd4242020-10-07 20:59:07 +02001020 rx_mode = (vnet_hw_if_rx_mode) ntohl (mp->mode);
Stevenad8015b2017-10-29 22:10:46 -07001021 error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
1022 mp->queue_id_valid,
Jakub Grajciar053204a2019-03-18 13:17:53 +01001023 ntohl (mp->queue_id),
Damjan Marioneabd4242020-10-07 20:59:07 +02001024 (vnet_hw_if_rx_mode) rx_mode);
Jakub Grajciar053204a2019-03-18 13:17:53 +01001025
Stevenad8015b2017-10-29 22:10:46 -07001026 if (error)
1027 {
1028 rv = VNET_API_ERROR_UNIMPLEMENTED;
1029 clib_error_report (error);
1030 goto out;
1031 }
1032
1033 BAD_SW_IF_INDEX_LABEL;
1034out:
1035 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
1036}
1037
Mohsin Kazmif0b42f42018-09-10 18:11:00 +02001038static void
1039send_interface_rx_placement_details (vpe_api_main_t * am,
1040 vl_api_registration_t * rp,
1041 u32 sw_if_index, u32 worker_id,
1042 u32 queue_id, u8 mode, u32 context)
1043{
1044 vl_api_sw_interface_rx_placement_details_t *mp;
1045 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001046 clib_memset (mp, 0, sizeof (*mp));
Mohsin Kazmif0b42f42018-09-10 18:11:00 +02001047
1048 mp->_vl_msg_id = htons (VL_API_SW_INTERFACE_RX_PLACEMENT_DETAILS);
1049 mp->sw_if_index = htonl (sw_if_index);
1050 mp->queue_id = htonl (queue_id);
1051 mp->worker_id = htonl (worker_id);
Jakub Grajciare30f1e92020-05-05 12:30:30 +02001052 mp->mode = htonl (mode);
Mohsin Kazmif0b42f42018-09-10 18:11:00 +02001053 mp->context = context;
1054
1055 vl_api_send_msg (rp, (u8 *) mp);
1056}
1057
1058static void vl_api_sw_interface_rx_placement_dump_t_handler
1059 (vl_api_sw_interface_rx_placement_dump_t * mp)
1060{
1061 vnet_main_t *vnm = vnet_get_main ();
1062 vpe_api_main_t *am = &vpe_api_main;
1063 u32 sw_if_index = ntohl (mp->sw_if_index);
1064 vl_api_registration_t *reg;
1065
1066 reg = vl_api_client_index_to_registration (mp->client_index);
1067 if (!reg)
1068 return;
1069
1070 if (sw_if_index == ~0)
1071 {
1072 vnet_device_input_runtime_t *rt;
1073 vnet_device_and_queue_t *dq;
1074 vlib_node_t *pn = vlib_get_node_by_name (am->vlib_main,
1075 (u8 *) "device-input");
1076 uword si;
1077 int index = 0;
1078
1079 /* *INDENT-OFF* */
1080 foreach_vlib_main (({
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001081 clib_bitmap_foreach (si, pn->sibling_bitmap)
1082 {
Mohsin Kazmif0b42f42018-09-10 18:11:00 +02001083 rt = vlib_node_get_runtime_data (this_vlib_main, si);
1084 vec_foreach (dq, rt->devices_and_queues)
1085 {
1086 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm,
1087 dq->hw_if_index);
1088 send_interface_rx_placement_details (am, reg, hw->sw_if_index, index,
1089 dq->queue_id, dq->mode, mp->context);
1090 }
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001091 }
Mohsin Kazmif0b42f42018-09-10 18:11:00 +02001092 index++;
1093 }));
1094 /* *INDENT-ON* */
1095 }
1096 else
1097 {
1098 int i;
1099 vnet_sw_interface_t *si;
1100
1101 if (!vnet_sw_if_index_is_api_valid (sw_if_index))
1102 {
1103 clib_warning ("sw_if_index %u does not exist", sw_if_index);
1104 goto bad_sw_if_index;
1105 }
1106
1107 si = vnet_get_sw_interface (vnm, sw_if_index);
1108 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
1109 {
1110 clib_warning ("interface type is not HARDWARE! P2P, PIPE and SUB"
1111 " interfaces are not supported");
1112 goto bad_sw_if_index;
1113 }
1114
1115 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, si->hw_if_index);
1116
1117 for (i = 0; i < vec_len (hw->dq_runtime_index_by_queue); i++)
1118 {
1119 send_interface_rx_placement_details (am, reg, hw->sw_if_index,
1120 hw->input_node_thread_index_by_queue
1121 [i], i,
1122 hw->rx_mode_by_queue[i],
1123 mp->context);
1124 }
1125 }
1126
1127 BAD_SW_IF_INDEX_LABEL;
1128}
1129
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001130static void vl_api_sw_interface_set_rx_placement_t_handler
1131 (vl_api_sw_interface_set_rx_placement_t * mp)
1132{
1133 vl_api_sw_interface_set_rx_placement_reply_t *rmp;
1134 vnet_main_t *vnm = vnet_get_main ();
1135 u32 sw_if_index = ntohl (mp->sw_if_index);
1136 vnet_sw_interface_t *si;
1137 clib_error_t *error = 0;
1138 int rv = 0;
1139
1140 VALIDATE_SW_IF_INDEX (mp);
1141
1142 si = vnet_get_sw_interface (vnm, sw_if_index);
1143 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
1144 {
1145 rv = VNET_API_ERROR_INVALID_VALUE;
1146 goto bad_sw_if_index;
1147 }
1148
1149 error = set_hw_interface_rx_placement (si->hw_if_index,
1150 ntohl (mp->queue_id),
1151 ntohl (mp->worker_id), mp->is_main);
1152 if (error)
1153 {
1154 rv = VNET_API_ERROR_UNIMPLEMENTED;
1155 clib_error_report (error);
1156 goto out;
1157 }
1158
1159 BAD_SW_IF_INDEX_LABEL;
1160out:
1161 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_PLACEMENT_REPLY);
1162}
1163
Neale Rannsb8d44812017-11-10 06:53:54 -08001164static void
1165vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
1166{
1167 vl_api_create_vlan_subif_reply_t *rmp;
1168 vnet_main_t *vnm = vnet_get_main ();
1169 u32 sw_if_index = (u32) ~ 0;
1170 vnet_hw_interface_t *hi;
1171 int rv = 0;
1172 u32 id;
1173 vnet_sw_interface_t template;
1174 uword *p;
1175 vnet_interface_main_t *im = &vnm->interface_main;
1176 u64 sup_and_sub_key;
Florin Coras6c4dae22018-01-09 06:39:23 -08001177 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08001178 clib_error_t *error;
1179
1180 VALIDATE_SW_IF_INDEX (mp);
1181
1182 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1183
1184 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1185 {
1186 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1187 goto out;
1188 }
1189
1190 id = ntohl (mp->vlan_id);
1191 if (id == 0 || id > 4095)
1192 {
1193 rv = VNET_API_ERROR_INVALID_VLAN;
1194 goto out;
1195 }
1196
1197 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
1198
1199 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1200 if (p)
1201 {
1202 rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
1203 goto out;
1204 }
1205
Dave Barachb7b92992018-10-17 10:38:51 -04001206 clib_memset (&template, 0, sizeof (template));
Neale Rannsb8d44812017-11-10 06:53:54 -08001207 template.type = VNET_SW_INTERFACE_TYPE_SUB;
Joe Zhou715f94e2019-03-06 23:05:32 -08001208 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
Neale Rannsb8d44812017-11-10 06:53:54 -08001209 template.sup_sw_if_index = hi->sw_if_index;
1210 template.sub.id = id;
1211 template.sub.eth.raw_flags = 0;
1212 template.sub.eth.flags.one_tag = 1;
1213 template.sub.eth.outer_vlan_id = id;
1214 template.sub.eth.flags.exact_match = 1;
1215
1216 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1217 if (error)
1218 {
1219 clib_error_report (error);
1220 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1221 goto out;
1222 }
1223
1224 u64 *kp = clib_mem_alloc (sizeof (*kp));
1225 *kp = sup_and_sub_key;
1226
1227 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1228 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1229
1230 BAD_SW_IF_INDEX_LABEL;
1231
1232out:
Florin Coras6c4dae22018-01-09 06:39:23 -08001233 reg = vl_api_client_index_to_registration (mp->client_index);
1234 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08001235 return;
1236
1237 rmp = vl_msg_api_alloc (sizeof (*rmp));
1238 rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
1239 rmp->context = mp->context;
1240 rmp->retval = htonl (rv);
1241 rmp->sw_if_index = htonl (sw_if_index);
Florin Coras6c4dae22018-01-09 06:39:23 -08001242 vl_api_send_msg (reg, (u8 *) rmp);
Neale Rannsb8d44812017-11-10 06:53:54 -08001243}
1244
1245static void
1246vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1247{
1248 vl_api_create_subif_reply_t *rmp;
1249 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns669f4e32019-10-28 07:56:15 -07001250 u32 sub_sw_if_index = ~0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001251 vnet_hw_interface_t *hi;
Neale Ranns669f4e32019-10-28 07:56:15 -07001252 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001253
1254 VALIDATE_SW_IF_INDEX (mp);
1255
Neale Rannsb8d44812017-11-10 06:53:54 -08001256 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1257
1258 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
Neale Ranns669f4e32019-10-28 07:56:15 -07001259 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1260 else
1261 rv = vnet_create_sub_interface (ntohl (mp->sw_if_index),
1262 ntohl (mp->sub_id),
1263 ntohl (mp->sub_if_flags),
1264 ntohs (mp->inner_vlan_id),
1265 ntohs (mp->outer_vlan_id),
1266 &sub_sw_if_index);
Neale Rannsb8d44812017-11-10 06:53:54 -08001267
1268 BAD_SW_IF_INDEX_LABEL;
1269
Neale Rannsb8d44812017-11-10 06:53:54 -08001270 /* *INDENT-OFF* */
1271 REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1272 ({
Neale Ranns669f4e32019-10-28 07:56:15 -07001273 rmp->sw_if_index = ntohl(sub_sw_if_index);
Neale Rannsb8d44812017-11-10 06:53:54 -08001274 }));
1275 /* *INDENT-ON* */
1276}
1277
1278static void
1279vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1280{
1281 vl_api_delete_subif_reply_t *rmp;
1282 int rv;
1283
1284 rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1285
1286 REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1287}
1288
1289static void
1290vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1291 mp)
1292{
1293 vl_api_interface_name_renumber_reply_t *rmp;
1294 int rv = 0;
1295
1296 VALIDATE_SW_IF_INDEX (mp);
1297
1298 rv = vnet_interface_name_renumber
1299 (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1300
1301 BAD_SW_IF_INDEX_LABEL;
1302
1303 REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1304}
1305
1306static void
1307vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1308{
1309 vl_api_create_loopback_reply_t *rmp;
1310 u32 sw_if_index;
1311 int rv;
Jakub Grajciar053204a2019-03-18 13:17:53 +01001312 mac_address_t mac;
Neale Rannsb8d44812017-11-10 06:53:54 -08001313
Jakub Grajciar053204a2019-03-18 13:17:53 +01001314 mac_address_decode (mp->mac_address, &mac);
1315 rv = vnet_create_loopback_interface (&sw_if_index, (u8 *) & mac, 0, 0);
Neale Rannsb8d44812017-11-10 06:53:54 -08001316
1317 /* *INDENT-OFF* */
1318 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1319 ({
1320 rmp->sw_if_index = ntohl (sw_if_index);
1321 }));
1322 /* *INDENT-ON* */
1323}
1324
1325static void vl_api_create_loopback_instance_t_handler
1326 (vl_api_create_loopback_instance_t * mp)
1327{
1328 vl_api_create_loopback_instance_reply_t *rmp;
1329 u32 sw_if_index;
1330 u8 is_specified = mp->is_specified;
1331 u32 user_instance = ntohl (mp->user_instance);
1332 int rv;
Jakub Grajciar053204a2019-03-18 13:17:53 +01001333 mac_address_t mac;
Neale Rannsb8d44812017-11-10 06:53:54 -08001334
Jakub Grajciar053204a2019-03-18 13:17:53 +01001335 mac_address_decode (mp->mac_address, &mac);
1336 rv = vnet_create_loopback_interface (&sw_if_index, (u8 *) & mac,
Neale Rannsb8d44812017-11-10 06:53:54 -08001337 is_specified, user_instance);
1338
1339 /* *INDENT-OFF* */
1340 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1341 ({
1342 rmp->sw_if_index = ntohl (sw_if_index);
1343 }));
1344 /* *INDENT-ON* */
1345}
1346
1347static void
1348vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1349{
1350 vl_api_delete_loopback_reply_t *rmp;
1351 u32 sw_if_index;
1352 int rv;
1353
1354 sw_if_index = ntohl (mp->sw_if_index);
1355 rv = vnet_delete_loopback_interface (sw_if_index);
1356
1357 REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1358}
1359
Neale Ranns6f4a6be2018-03-16 16:26:21 -07001360static void
1361 vl_api_collect_detailed_interface_stats_t_handler
1362 (vl_api_collect_detailed_interface_stats_t * mp)
1363{
1364 vl_api_collect_detailed_interface_stats_reply_t *rmp;
1365 int rv = 0;
1366
Neale Ranns871dc422018-03-29 01:28:09 -07001367 rv =
1368 vnet_sw_interface_stats_collect_enable_disable (ntohl (mp->sw_if_index),
1369 mp->enable_disable);
Neale Ranns6f4a6be2018-03-16 16:26:21 -07001370
1371 REPLY_MACRO (VL_API_COLLECT_DETAILED_INTERFACE_STATS_REPLY);
1372}
1373
Neale Ranns59f71132020-04-08 12:19:38 +00001374static void
1375 vl_api_sw_interface_address_replace_begin_t_handler
1376 (vl_api_sw_interface_address_replace_begin_t * mp)
1377{
1378 vl_api_sw_interface_address_replace_begin_reply_t *rmp;
1379 int rv = 0;
1380
1381 ip_interface_address_mark ();
1382
1383 REPLY_MACRO (VL_API_SW_INTERFACE_ADDRESS_REPLACE_BEGIN_REPLY);
1384}
1385
1386static void
1387 vl_api_sw_interface_address_replace_end_t_handler
1388 (vl_api_sw_interface_address_replace_end_t * mp)
1389{
1390 vl_api_sw_interface_address_replace_end_reply_t *rmp;
1391 int rv = 0;
1392
1393 ip_interface_address_sweep ();
1394
1395 REPLY_MACRO (VL_API_SW_INTERFACE_ADDRESS_REPLACE_END_REPLY);
1396}
1397
Dave Barachaff70772016-10-31 11:59:07 -04001398/*
1399 * vpe_api_hookup
1400 * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001401 * vlib has already mapped shared memory and
Dave Barachaff70772016-10-31 11:59:07 -04001402 * added the client registration handlers.
1403 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1404 */
1405#define vl_msg_name_crc_list
Dave Barachb5e8a772016-12-06 12:04:42 -05001406#include <vnet/interface.api.h>
Dave Barachaff70772016-10-31 11:59:07 -04001407#undef vl_msg_name_crc_list
1408
1409static void
1410setup_message_id_table (api_main_t * am)
1411{
1412#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1413 foreach_vl_msg_name_crc_interface;
1414#undef _
1415}
1416
Dave Barach6d963c22016-12-05 09:50:05 -05001417pub_sub_handler (interface_events, INTERFACE_EVENTS);
1418
Dave Barachaff70772016-10-31 11:59:07 -04001419static clib_error_t *
1420interface_api_hookup (vlib_main_t * vm)
1421{
Dave Barach39d69112019-11-27 11:42:13 -05001422 api_main_t *am = vlibapi_get_main ();
Dave Barachaff70772016-10-31 11:59:07 -04001423
1424#define _(N,n) \
1425 vl_msg_api_set_handlers(VL_API_##N, #n, \
1426 vl_api_##n##_t_handler, \
1427 vl_noop_handler, \
1428 vl_api_##n##_t_endian, \
1429 vl_api_##n##_t_print, \
1430 sizeof(vl_api_##n##_t), 1);
1431 foreach_vpe_api_msg;
1432#undef _
1433
Steven Luongc5fa2b82019-04-25 11:19:49 -07001434 /* Mark these APIs as mp safe */
1435 am->is_mp_safe[VL_API_SW_INTERFACE_DUMP] = 1;
1436 am->is_mp_safe[VL_API_SW_INTERFACE_DETAILS] = 1;
1437 am->is_mp_safe[VL_API_SW_INTERFACE_TAG_ADD_DEL] = 1;
1438
Dave Barach3940de32019-07-23 16:28:36 -04001439 /* Do not replay VL_API_SW_INTERFACE_DUMP messages */
1440 am->api_trace_cfg[VL_API_SW_INTERFACE_DUMP].replay_enable = 0;
1441
Dave Barachaff70772016-10-31 11:59:07 -04001442 /*
1443 * Set up the (msg_name, crc, message-id) table
1444 */
1445 setup_message_id_table (am);
1446
1447 return 0;
1448}
1449
1450VLIB_API_INIT_FUNCTION (interface_api_hookup);
1451
1452/*
1453 * fd.io coding-style-patch-verification: ON
1454 *
1455 * Local Variables:
1456 * eval: (c-set-style "gnu")
1457 * End:
1458 */