blob: 1d167c680fea0d5c293ceb209a08bbb01be065fb [file] [log] [blame]
Dave Barachaff70772016-10-31 11:59:07 -04001/*
2 *------------------------------------------------------------------
3 * interface_api.c - vnet interface api
4 *
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
Matus Fabiand162f3d2016-12-05 01:05:35 -080025#include <vnet/ethernet/ethernet.h>
Dave Barach6d963c22016-12-05 09:50:05 -050026#include <vnet/ip/ip.h>
27#include <vnet/fib/fib_table.h>
Neale Ranns4008ac92017-02-13 23:20:04 -080028#include <vnet/mfib/mfib_table.h>
Dave Barach6d963c22016-12-05 09:50:05 -050029#include <vnet/l2/l2_vtr.h>
Dave Barachaff70772016-10-31 11:59:07 -040030#include <vnet/vnet_msg_enum.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050031#include <vnet/fib/fib_api.h>
Neale Ranns358425b2017-02-20 09:42:36 -080032#include <vnet/mfib/mfib_table.h>
Dave Barachaff70772016-10-31 11:59:07 -040033
34#define vl_typedefs /* define message structures */
35#include <vnet/vnet_all_api_h.h>
36#undef vl_typedefs
37
38#define vl_endianfun /* define message structures */
39#include <vnet/vnet_all_api_h.h>
40#undef vl_endianfun
41
42/* instantiate all the print functions we know about */
43#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44#define vl_printfun
45#include <vnet/vnet_all_api_h.h>
46#undef vl_printfun
47
48#include <vlibapi/api_helper_macros.h>
Dave Barachf1586612017-03-30 09:33:01 -040049vpe_api_main_t vpe_api_main;
Dave Barachaff70772016-10-31 11:59:07 -040050
Dave Barach6d963c22016-12-05 09:50:05 -050051#define foreach_vpe_api_msg \
52_(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags) \
Ole Troand7231612018-06-07 10:17:57 +020053_(HW_INTERFACE_SET_MTU, hw_interface_set_mtu) \
Dave Barach6d963c22016-12-05 09:50:05 -050054_(SW_INTERFACE_SET_MTU, sw_interface_set_mtu) \
55_(WANT_INTERFACE_EVENTS, want_interface_events) \
56_(SW_INTERFACE_DUMP, sw_interface_dump) \
Dave Barach6d963c22016-12-05 09:50:05 -050057_(SW_INTERFACE_ADD_DEL_ADDRESS, sw_interface_add_del_address) \
Stevenad8015b2017-10-29 22:10:46 -070058_(SW_INTERFACE_SET_RX_MODE, sw_interface_set_rx_mode) \
Dave Barach6d963c22016-12-05 09:50:05 -050059_(SW_INTERFACE_SET_TABLE, sw_interface_set_table) \
Juraj Slobodadfc19232016-12-05 13:20:37 +010060_(SW_INTERFACE_GET_TABLE, sw_interface_get_table) \
Dave Barach6d963c22016-12-05 09:50:05 -050061_(SW_INTERFACE_SET_UNNUMBERED, sw_interface_set_unnumbered) \
62_(SW_INTERFACE_CLEAR_STATS, sw_interface_clear_stats) \
Jon Loeliger10c273b2017-03-30 08:39:33 -050063_(SW_INTERFACE_TAG_ADD_DEL, sw_interface_tag_add_del) \
Neale Rannsb8d44812017-11-10 06:53:54 -080064_(SW_INTERFACE_SET_MAC_ADDRESS, sw_interface_set_mac_address) \
Juraj Slobodac0374232018-02-01 15:18:49 +010065_(SW_INTERFACE_GET_MAC_ADDRESS, sw_interface_get_mac_address) \
Neale Rannsb8d44812017-11-10 06:53:54 -080066_(CREATE_VLAN_SUBIF, create_vlan_subif) \
67_(CREATE_SUBIF, create_subif) \
68_(DELETE_SUBIF, delete_subif) \
69_(CREATE_LOOPBACK, create_loopback) \
70_(CREATE_LOOPBACK_INSTANCE, create_loopback_instance) \
71_(DELETE_LOOPBACK, delete_loopback) \
Neale Ranns6f4a6be2018-03-16 16:26:21 -070072_(INTERFACE_NAME_RENUMBER, interface_name_renumber) \
Neale Ranns1855b8e2018-07-11 10:31:26 -070073_(COLLECT_DETAILED_INTERFACE_STATS, collect_detailed_interface_stats) \
74_(SW_INTERFACE_SET_IP_DIRECTED_BROADCAST, \
75 sw_interface_set_ip_directed_broadcast)
Dave Barachaff70772016-10-31 11:59:07 -040076
77static void
78vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
79{
80 vl_api_sw_interface_set_flags_reply_t *rmp;
81 vnet_main_t *vnm = vnet_get_main ();
82 int rv = 0;
83 clib_error_t *error;
84 u16 flags;
85
86 VALIDATE_SW_IF_INDEX (mp);
87
88 flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
89
90 error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
91 if (error)
92 {
93 rv = -1;
94 clib_error_report (error);
95 }
96
97 BAD_SW_IF_INDEX_LABEL;
98 REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
99}
100
Matus Fabiand162f3d2016-12-05 01:05:35 -0800101static void
Ole Troand7231612018-06-07 10:17:57 +0200102vl_api_hw_interface_set_mtu_t_handler (vl_api_hw_interface_set_mtu_t * mp)
Matus Fabiand162f3d2016-12-05 01:05:35 -0800103{
Ole Troand7231612018-06-07 10:17:57 +0200104 vl_api_hw_interface_set_mtu_reply_t *rmp;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800105 vnet_main_t *vnm = vnet_get_main ();
Matus Fabiand162f3d2016-12-05 01:05:35 -0800106 u32 sw_if_index = ntohl (mp->sw_if_index);
107 u16 mtu = ntohs (mp->mtu);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200108 ethernet_main_t *em = &ethernet_main;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800109 int rv = 0;
110
111 VALIDATE_SW_IF_INDEX (mp);
112
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200113 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
114 if (si->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
115 {
116 rv = VNET_API_ERROR_INVALID_VALUE;
117 goto bad_sw_if_index;
118 }
119
120 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, si->hw_if_index);
121 ethernet_interface_t *eif = ethernet_get_interface (em, si->hw_if_index);
122
123 if (!eif)
124 {
125 rv = VNET_API_ERROR_FEATURE_DISABLED;
126 goto bad_sw_if_index;
127 }
128
129 if (mtu < hi->min_supported_packet_bytes)
130 {
131 rv = VNET_API_ERROR_INVALID_VALUE;
132 goto bad_sw_if_index;
133 }
134
135 if (mtu > hi->max_supported_packet_bytes)
136 {
137 rv = VNET_API_ERROR_INVALID_VALUE;
138 goto bad_sw_if_index;
139 }
140
141 vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu);
Matus Fabiand162f3d2016-12-05 01:05:35 -0800142
143 BAD_SW_IF_INDEX_LABEL;
Ole Troand7231612018-06-07 10:17:57 +0200144 REPLY_MACRO (VL_API_HW_INTERFACE_SET_MTU_REPLY);
145}
146
147static void
148vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
149{
150 vl_api_sw_interface_set_mtu_reply_t *rmp;
151 vnet_main_t *vnm = vnet_get_main ();
152 u32 sw_if_index = ntohl (mp->sw_if_index);
153 int rv = 0;
154 int i;
155 u32 per_protocol_mtu[VNET_N_MTU];
156
157 VALIDATE_SW_IF_INDEX (mp);
158
159 for (i = 0; i < VNET_N_MTU; i++)
160 per_protocol_mtu[i] = ntohl (mp->mtu[i]);
161
162 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, per_protocol_mtu);
163
164 BAD_SW_IF_INDEX_LABEL;
Matus Fabiand162f3d2016-12-05 01:05:35 -0800165 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
166}
167
Dave Barach6d963c22016-12-05 09:50:05 -0500168static void
Neale Ranns1855b8e2018-07-11 10:31:26 -0700169 vl_api_sw_interface_set_ip_directed_broadcast_t_handler
170 (vl_api_sw_interface_set_ip_directed_broadcast_t * mp)
171{
172 vl_api_sw_interface_set_ip_directed_broadcast_reply_t *rmp;
173 u32 sw_if_index = ntohl (mp->sw_if_index);
174 int rv = 0;
175
176 VALIDATE_SW_IF_INDEX (mp);
177
178 vnet_sw_interface_ip_directed_broadcast (vnet_get_main (),
179 sw_if_index, mp->enable);
180
181 BAD_SW_IF_INDEX_LABEL;
182 REPLY_MACRO (VL_API_SW_INTERFACE_SET_IP_DIRECTED_BROADCAST_REPLY);
183}
184
185static void
Dave Barach6d963c22016-12-05 09:50:05 -0500186send_sw_interface_details (vpe_api_main_t * am,
Dave Barach59b25652017-09-10 15:04:27 -0400187 vl_api_registration_t * rp,
Dave Barach6d963c22016-12-05 09:50:05 -0500188 vnet_sw_interface_t * swif,
189 u8 * interface_name, u32 context)
190{
Eyal Bari1c82cd42017-03-14 14:39:51 +0200191 vnet_hw_interface_t *hi =
192 vnet_get_sup_hw_interface (am->vnet_main, swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500193
Eyal Bari1c82cd42017-03-14 14:39:51 +0200194 vl_api_sw_interface_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barach6d963c22016-12-05 09:50:05 -0500195 memset (mp, 0, sizeof (*mp));
196 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_DETAILS);
197 mp->sw_if_index = ntohl (swif->sw_if_index);
198 mp->sup_sw_if_index = ntohl (swif->sup_sw_if_index);
199 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
200 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
201 mp->link_duplex = ((hi->flags & VNET_HW_INTERFACE_FLAG_DUPLEX_MASK) >>
202 VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT);
203 mp->link_speed = ((hi->flags & VNET_HW_INTERFACE_FLAG_SPEED_MASK) >>
204 VNET_HW_INTERFACE_FLAG_SPEED_SHIFT);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200205 mp->link_mtu = ntohs (hi->max_packet_bytes);
Ole Troand7231612018-06-07 10:17:57 +0200206 mp->mtu[VNET_MTU_L3] = ntohl (swif->mtu[VNET_MTU_L3]);
207 mp->mtu[VNET_MTU_IP4] = ntohl (swif->mtu[VNET_MTU_IP4]);
208 mp->mtu[VNET_MTU_IP6] = ntohl (swif->mtu[VNET_MTU_IP6]);
209 mp->mtu[VNET_MTU_MPLS] = ntohl (swif->mtu[VNET_MTU_MPLS]);
210
Dave Barach6d963c22016-12-05 09:50:05 -0500211 mp->context = context;
212
213 strncpy ((char *) mp->interface_name,
214 (char *) interface_name, ARRAY_LEN (mp->interface_name) - 1);
215
216 /* Send the L2 address for ethernet physical intfcs */
217 if (swif->sup_sw_if_index == swif->sw_if_index
218 && hi->hw_class_index == ethernet_hw_interface_class.index)
219 {
220 ethernet_main_t *em = ethernet_get_main (am->vlib_main);
221 ethernet_interface_t *ei;
222
223 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
224 ASSERT (sizeof (mp->l2_address) >= sizeof (ei->address));
225 clib_memcpy (mp->l2_address, ei->address, sizeof (ei->address));
226 mp->l2_address_length = ntohl (sizeof (ei->address));
227 }
228 else if (swif->sup_sw_if_index != swif->sw_if_index)
229 {
230 vnet_sub_interface_t *sub = &swif->sub;
231 mp->sub_id = ntohl (sub->id);
232 mp->sub_dot1ad = sub->eth.flags.dot1ad;
233 mp->sub_number_of_tags =
234 sub->eth.flags.one_tag + sub->eth.flags.two_tags * 2;
235 mp->sub_outer_vlan_id = ntohs (sub->eth.outer_vlan_id);
236 mp->sub_inner_vlan_id = ntohs (sub->eth.inner_vlan_id);
237 mp->sub_exact_match = sub->eth.flags.exact_match;
238 mp->sub_default = sub->eth.flags.default_sub;
239 mp->sub_outer_vlan_id_any = sub->eth.flags.outer_vlan_id_any;
240 mp->sub_inner_vlan_id_any = sub->eth.flags.inner_vlan_id_any;
241
242 /* vlan tag rewrite data */
243 u32 vtr_op = L2_VTR_DISABLED;
244 u32 vtr_push_dot1q = 0, vtr_tag1 = 0, vtr_tag2 = 0;
245
246 if (l2vtr_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
247 &vtr_op, &vtr_push_dot1q, &vtr_tag1, &vtr_tag2) != 0)
248 {
249 // error - default to disabled
250 mp->vtr_op = ntohl (L2_VTR_DISABLED);
251 clib_warning ("cannot get vlan tag rewrite for sw_if_index %d",
252 swif->sw_if_index);
253 }
254 else
255 {
256 mp->vtr_op = ntohl (vtr_op);
257 mp->vtr_push_dot1q = ntohl (vtr_push_dot1q);
258 mp->vtr_tag1 = ntohl (vtr_tag1);
259 mp->vtr_tag2 = ntohl (vtr_tag2);
260 }
261 }
262
Pavel Kotucek65e84572017-01-16 17:01:56 +0100263 /* pbb tag rewrite data */
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100264 ethernet_header_t eth_hdr;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100265 u32 vtr_op = L2_VTR_DISABLED;
266 u16 outer_tag = 0;
Pavel Kotucek65e84572017-01-16 17:01:56 +0100267 u16 b_vlanid = 0;
268 u32 i_sid = 0;
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100269 memset (&eth_hdr, 0, sizeof (eth_hdr));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100270
271 if (!l2pbb_get (am->vlib_main, am->vnet_main, swif->sw_if_index,
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100272 &vtr_op, &outer_tag, &eth_hdr, &b_vlanid, &i_sid))
Pavel Kotucek65e84572017-01-16 17:01:56 +0100273 {
274 mp->sub_dot1ah = 1;
Gabriel Gannef7f2a9f2017-03-06 15:19:40 +0100275 clib_memcpy (mp->b_dmac, eth_hdr.dst_address,
276 sizeof (eth_hdr.dst_address));
277 clib_memcpy (mp->b_smac, eth_hdr.src_address,
278 sizeof (eth_hdr.src_address));
Pavel Kotucek65e84572017-01-16 17:01:56 +0100279 mp->b_vlanid = b_vlanid;
280 mp->i_sid = i_sid;
281 }
282
Eyal Bari1c82cd42017-03-14 14:39:51 +0200283 u8 *tag = vnet_get_sw_interface_tag (vnet_get_main (), swif->sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500284 if (tag)
285 strncpy ((char *) mp->tag, (char *) tag, ARRAY_LEN (mp->tag) - 1);
286
Florin Corase86a8ed2018-01-05 03:20:25 -0800287 vl_api_send_msg (rp, (u8 *) mp);
Dave Barach6d963c22016-12-05 09:50:05 -0500288}
289
290static void
291vl_api_sw_interface_dump_t_handler (vl_api_sw_interface_dump_t * mp)
292{
293 vpe_api_main_t *am = &vpe_api_main;
294 vnet_sw_interface_t *swif;
295 vnet_interface_main_t *im = &am->vnet_main->interface_main;
Dave Barach59b25652017-09-10 15:04:27 -0400296 vl_api_registration_t *rp;
Dave Barach6d963c22016-12-05 09:50:05 -0500297
Dave Barach59b25652017-09-10 15:04:27 -0400298 rp = vl_api_client_index_to_registration (mp->client_index);
299
300 if (rp == 0)
301 {
302 clib_warning ("Client %d AWOL", mp->client_index);
303 return;
304 }
Dave Barach6d963c22016-12-05 09:50:05 -0500305
Eyal Bari1c82cd42017-03-14 14:39:51 +0200306 u8 *filter = 0, *name = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500307 if (mp->name_filter_valid)
308 {
309 mp->name_filter[ARRAY_LEN (mp->name_filter) - 1] = 0;
Eyal Bari1c82cd42017-03-14 14:39:51 +0200310 filter = format (0, "%s%c", mp->name_filter, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500311 }
312
Eyal Bari1c82cd42017-03-14 14:39:51 +0200313 char *strcasestr (char *, char *); /* lnx hdr file botch */
Dave Barach6d963c22016-12-05 09:50:05 -0500314 /* *INDENT-OFF* */
315 pool_foreach (swif, im->sw_interfaces,
316 ({
Eyal Bari1c82cd42017-03-14 14:39:51 +0200317 if (!vnet_swif_is_api_visible (swif))
318 continue;
319 vec_reset_length(name);
320 name = format (name, "%U%c", format_vnet_sw_interface_name, am->vnet_main,
321 swif, 0);
Dave Barach6d963c22016-12-05 09:50:05 -0500322
Eyal Bari1c82cd42017-03-14 14:39:51 +0200323 if (filter && !strcasestr((char *) name, (char *) filter))
324 continue;
Dave Barach6d963c22016-12-05 09:50:05 -0500325
Dave Barach59b25652017-09-10 15:04:27 -0400326 send_sw_interface_details (am, rp, swif, name, mp->context);
Dave Barach6d963c22016-12-05 09:50:05 -0500327 }));
328 /* *INDENT-ON* */
329
Eyal Bari1c82cd42017-03-14 14:39:51 +0200330 vec_free (name);
331 vec_free (filter);
Dave Barach6d963c22016-12-05 09:50:05 -0500332}
333
334static void
335 vl_api_sw_interface_add_del_address_t_handler
336 (vl_api_sw_interface_add_del_address_t * mp)
337{
338 vlib_main_t *vm = vlib_get_main ();
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500339 vnet_main_t *vnm = vnet_get_main ();
Dave Barach6d963c22016-12-05 09:50:05 -0500340 vl_api_sw_interface_add_del_address_reply_t *rmp;
341 int rv = 0;
342 u32 is_del;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500343 clib_error_t *error = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500344
345 VALIDATE_SW_IF_INDEX (mp);
346
347 is_del = mp->is_add == 0;
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500348 vnm->api_errno = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500349
350 if (mp->del_all)
351 ip_del_all_interface_addresses (vm, ntohl (mp->sw_if_index));
352 else if (mp->is_ipv6)
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500353 error = ip6_add_del_interface_address (vm, ntohl (mp->sw_if_index),
354 (void *) mp->address,
355 mp->address_length, is_del);
Dave Barach6d963c22016-12-05 09:50:05 -0500356 else
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500357 error = ip4_add_del_interface_address (vm, ntohl (mp->sw_if_index),
358 (void *) mp->address,
359 mp->address_length, is_del);
360
361 if (error)
362 {
363 rv = vnm->api_errno;
364 clib_error_report (error);
365 goto done;
366 }
Dave Barach6d963c22016-12-05 09:50:05 -0500367
368 BAD_SW_IF_INDEX_LABEL;
369
Jon Loeliger35ffa3e2017-09-28 13:54:16 -0500370done:
Dave Barach6d963c22016-12-05 09:50:05 -0500371 REPLY_MACRO (VL_API_SW_INTERFACE_ADD_DEL_ADDRESS_REPLY);
372}
373
374void stats_dslock_with_hint (int hint, int tag) __attribute__ ((weak));
375void
376stats_dslock_with_hint (int hint, int tag)
377{
378}
379
380void stats_dsunlock (void) __attribute__ ((weak));
381void
382stats_dsunlock (void)
383{
384}
385
386static void
387vl_api_sw_interface_set_table_t_handler (vl_api_sw_interface_set_table_t * mp)
388{
Dave Barach6d963c22016-12-05 09:50:05 -0500389 vl_api_sw_interface_set_table_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700390 u32 sw_if_index = ntohl (mp->sw_if_index);
391 u32 table_id = ntohl (mp->vrf_id);
392 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500393
394 VALIDATE_SW_IF_INDEX (mp);
395
396 stats_dslock_with_hint (1 /* release hint */ , 4 /* tag */ );
397
398 if (mp->is_ipv6)
Neale Ranns15002542017-09-10 04:39:11 -0700399 rv = ip_table_bind (FIB_PROTOCOL_IP6, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500400 else
Neale Ranns15002542017-09-10 04:39:11 -0700401 rv = ip_table_bind (FIB_PROTOCOL_IP4, sw_if_index, table_id, 1);
Dave Barach6d963c22016-12-05 09:50:05 -0500402
Dave Barach6d963c22016-12-05 09:50:05 -0500403 stats_dsunlock ();
404
405 BAD_SW_IF_INDEX_LABEL;
406
407 REPLY_MACRO (VL_API_SW_INTERFACE_SET_TABLE_REPLY);
408}
409
Neale Ranns15002542017-09-10 04:39:11 -0700410int
411ip_table_bind (fib_protocol_t fproto,
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700412 u32 sw_if_index, u32 table_id, u8 is_api)
Neale Ranns15002542017-09-10 04:39:11 -0700413{
414 CLIB_UNUSED (ip_interface_address_t * ia);
415 u32 fib_index, mfib_index;
416 fib_source_t src;
417 mfib_source_t msrc;
418
419 if (is_api)
420 {
421 src = FIB_SOURCE_API;
422 msrc = MFIB_SOURCE_API;
423 }
424 else
425 {
426 src = FIB_SOURCE_CLI;
427 msrc = MFIB_SOURCE_CLI;
428 }
429
Florin Corasd0a59722017-10-15 17:41:21 +0000430 /*
Florin Corasd0a59722017-10-15 17:41:21 +0000431 * This if table does not exist = error is what we want in the end.
432 */
Neale Ranns8f6dd322018-05-17 06:34:24 -0700433 fib_index = fib_table_find (fproto, table_id);
434 mfib_index = mfib_table_find (fproto, table_id);
Florin Corasd0a59722017-10-15 17:41:21 +0000435
Neale Ranns8f6dd322018-05-17 06:34:24 -0700436 if (~0 == fib_index || ~0 == mfib_index)
437 {
438 return (VNET_API_ERROR_NO_SUCH_FIB);
439 }
Florin Corasd0a59722017-10-15 17:41:21 +0000440
Neale Ranns15002542017-09-10 04:39:11 -0700441 if (FIB_PROTOCOL_IP6 == fproto)
442 {
443 /*
444 * If the interface already has in IP address, then a change int
445 * VRF is not allowed. The IP address applied must first be removed.
446 * We do not do that automatically here, since VPP has no knowledge
447 * of whether thoses subnets are valid in the destination VRF.
448 */
449 /* *INDENT-OFF* */
450 foreach_ip_interface_address (&ip6_main.lookup_main,
451 ia, sw_if_index,
452 1 /* honor unnumbered */ ,
453 ({
454 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
455 }));
456 /* *INDENT-ON* */
457
458 vec_validate (ip6_main.fib_index_by_sw_if_index, sw_if_index);
459 vec_validate (ip6_main.mfib_index_by_sw_if_index, sw_if_index);
460
461 /*
462 * tell those that are interested that the binding is changing.
463 */
464 ip6_table_bind_callback_t *cb;
465 vec_foreach (cb, ip6_main.table_bind_callbacks)
466 cb->function (&ip6_main, cb->function_opaque,
467 sw_if_index,
468 fib_index,
469 ip6_main.fib_index_by_sw_if_index[sw_if_index]);
470
471 if (0 == table_id)
472 {
473 /* reset back to default */
474 if (0 != ip6_main.fib_index_by_sw_if_index[sw_if_index])
475 fib_table_unlock (ip6_main.fib_index_by_sw_if_index[sw_if_index],
476 FIB_PROTOCOL_IP6, src);
477 if (0 != ip6_main.mfib_index_by_sw_if_index[sw_if_index])
478 mfib_table_unlock (ip6_main.mfib_index_by_sw_if_index
479 [sw_if_index], FIB_PROTOCOL_IP6, msrc);
480
481 }
482 else
483 {
484 /* we need to lock the table now it's inuse */
485 fib_table_lock (fib_index, FIB_PROTOCOL_IP6, src);
486 mfib_table_lock (mfib_index, FIB_PROTOCOL_IP6, msrc);
487 }
488
489 ip6_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
490 ip6_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
491 }
492 else
493 {
494 /*
495 * If the interface already has in IP address, then a change int
496 * VRF is not allowed. The IP address applied must first be removed.
497 * We do not do that automatically here, since VPP has no knowledge
498 * of whether thoses subnets are valid in the destination VRF.
499 */
500 /* *INDENT-OFF* */
501 foreach_ip_interface_address (&ip4_main.lookup_main,
502 ia, sw_if_index,
503 1 /* honor unnumbered */ ,
504 ({
505 return (VNET_API_ERROR_ADDRESS_FOUND_FOR_INTERFACE);
506 }));
507 /* *INDENT-ON* */
508
509 vec_validate (ip4_main.fib_index_by_sw_if_index, sw_if_index);
510 vec_validate (ip4_main.mfib_index_by_sw_if_index, sw_if_index);
511
512 /*
513 * tell those that are interested that the binding is changing.
514 */
515 ip4_table_bind_callback_t *cb;
516 vec_foreach (cb, ip4_main.table_bind_callbacks)
517 cb->function (&ip4_main, cb->function_opaque,
518 sw_if_index,
519 fib_index,
520 ip4_main.fib_index_by_sw_if_index[sw_if_index]);
521
522 if (0 == table_id)
523 {
524 /* reset back to default */
525 if (0 != ip4_main.fib_index_by_sw_if_index[sw_if_index])
526 fib_table_unlock (ip4_main.fib_index_by_sw_if_index[sw_if_index],
527 FIB_PROTOCOL_IP4, src);
528 if (0 != ip4_main.mfib_index_by_sw_if_index[sw_if_index])
529 mfib_table_unlock (ip4_main.mfib_index_by_sw_if_index
530 [sw_if_index], FIB_PROTOCOL_IP4, msrc);
531
532 }
533 else
534 {
535 /* we need to lock the table now it's inuse */
536 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
537 table_id, src);
538
539 mfib_index = mfib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
540 table_id, msrc);
541 }
542
543 ip4_main.fib_index_by_sw_if_index[sw_if_index] = fib_index;
544 ip4_main.mfib_index_by_sw_if_index[sw_if_index] = mfib_index;
545 }
546
Neale Ranns15002542017-09-10 04:39:11 -0700547 return (0);
548}
549
Juraj Slobodadfc19232016-12-05 13:20:37 +0100550static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800551send_sw_interface_get_table_reply (vl_api_registration_t * reg,
Juraj Slobodadfc19232016-12-05 13:20:37 +0100552 u32 context, int retval, u32 vrf_id)
553{
554 vl_api_sw_interface_get_table_reply_t *mp;
555
556 mp = vl_msg_api_alloc (sizeof (*mp));
557 memset (mp, 0, sizeof (*mp));
558 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_GET_TABLE_REPLY);
559 mp->context = context;
560 mp->retval = htonl (retval);
561 mp->vrf_id = htonl (vrf_id);
562
Florin Coras6c4dae22018-01-09 06:39:23 -0800563 vl_api_send_msg (reg, (u8 *) mp);
Juraj Slobodadfc19232016-12-05 13:20:37 +0100564}
565
566static void
567vl_api_sw_interface_get_table_t_handler (vl_api_sw_interface_get_table_t * mp)
568{
Florin Coras6c4dae22018-01-09 06:39:23 -0800569 vl_api_registration_t *reg;
Juraj Slobodadfc19232016-12-05 13:20:37 +0100570 fib_table_t *fib_table = 0;
571 u32 sw_if_index = ~0;
572 u32 fib_index = ~0;
573 u32 table_id = ~0;
574 fib_protocol_t fib_proto = FIB_PROTOCOL_IP4;
575 int rv = 0;
576
Florin Coras6c4dae22018-01-09 06:39:23 -0800577 reg = vl_api_client_index_to_registration (mp->client_index);
578 if (!reg)
Juraj Slobodadfc19232016-12-05 13:20:37 +0100579 return;
580
581 VALIDATE_SW_IF_INDEX (mp);
582
583 sw_if_index = ntohl (mp->sw_if_index);
584
585 if (mp->is_ipv6)
586 fib_proto = FIB_PROTOCOL_IP6;
587
588 fib_index = fib_table_get_index_for_sw_if_index (fib_proto, sw_if_index);
589 if (fib_index != ~0)
590 {
591 fib_table = fib_table_get (fib_index, fib_proto);
592 table_id = fib_table->ft_table_id;
593 }
594
595 BAD_SW_IF_INDEX_LABEL;
596
Florin Coras6c4dae22018-01-09 06:39:23 -0800597 send_sw_interface_get_table_reply (reg, mp->context, rv, table_id);
Juraj Slobodadfc19232016-12-05 13:20:37 +0100598}
599
Dave Barach6d963c22016-12-05 09:50:05 -0500600static void vl_api_sw_interface_set_unnumbered_t_handler
601 (vl_api_sw_interface_set_unnumbered_t * mp)
602{
603 vl_api_sw_interface_set_unnumbered_reply_t *rmp;
604 int rv = 0;
Dave Barach6d963c22016-12-05 09:50:05 -0500605 vnet_main_t *vnm = vnet_get_main ();
Eyal Bari1c82cd42017-03-14 14:39:51 +0200606 u32 sw_if_index = ntohl (mp->sw_if_index);
607 u32 unnumbered_sw_if_index = ntohl (mp->unnumbered_sw_if_index);
Dave Barach6d963c22016-12-05 09:50:05 -0500608
609 /*
610 * The API message field names are backwards from
611 * the underlying data structure names.
612 * It's not worth changing them now.
613 */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200614 if (!vnet_sw_interface_is_api_valid (vnm, unnumbered_sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500615 {
616 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
617 goto done;
618 }
619
620 /* Only check the "use loop0" field when setting the binding */
Eyal Bari1c82cd42017-03-14 14:39:51 +0200621 if (mp->is_add && !vnet_sw_interface_is_api_valid (vnm, sw_if_index))
Dave Barach6d963c22016-12-05 09:50:05 -0500622 {
623 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
624 goto done;
625 }
626
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700627 vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
628 sw_if_index, mp->is_add);
Dave Barach6d963c22016-12-05 09:50:05 -0500629done:
630 REPLY_MACRO (VL_API_SW_INTERFACE_SET_UNNUMBERED_REPLY);
631}
632
633static void
634vl_api_sw_interface_clear_stats_t_handler (vl_api_sw_interface_clear_stats_t *
635 mp)
636{
637 vl_api_sw_interface_clear_stats_reply_t *rmp;
638
639 vnet_main_t *vnm = vnet_get_main ();
640 vnet_interface_main_t *im = &vnm->interface_main;
641 vlib_simple_counter_main_t *sm;
642 vlib_combined_counter_main_t *cm;
643 static vnet_main_t **my_vnet_mains;
644 int i, j, n_counters;
645 int rv = 0;
646
647 if (mp->sw_if_index != ~0)
648 VALIDATE_SW_IF_INDEX (mp);
649
650 vec_reset_length (my_vnet_mains);
651
652 for (i = 0; i < vec_len (vnet_mains); i++)
653 {
654 if (vnet_mains[i])
655 vec_add1 (my_vnet_mains, vnet_mains[i]);
656 }
657
658 if (vec_len (vnet_mains) == 0)
659 vec_add1 (my_vnet_mains, vnm);
660
661 n_counters = vec_len (im->combined_sw_if_counters);
662
663 for (j = 0; j < n_counters; j++)
664 {
665 for (i = 0; i < vec_len (my_vnet_mains); i++)
666 {
667 im = &my_vnet_mains[i]->interface_main;
668 cm = im->combined_sw_if_counters + j;
669 if (mp->sw_if_index == (u32) ~ 0)
670 vlib_clear_combined_counters (cm);
671 else
672 vlib_zero_combined_counter (cm, ntohl (mp->sw_if_index));
673 }
674 }
675
676 n_counters = vec_len (im->sw_if_counters);
677
678 for (j = 0; j < n_counters; j++)
679 {
680 for (i = 0; i < vec_len (my_vnet_mains); i++)
681 {
682 im = &my_vnet_mains[i]->interface_main;
683 sm = im->sw_if_counters + j;
684 if (mp->sw_if_index == (u32) ~ 0)
685 vlib_clear_simple_counters (sm);
686 else
687 vlib_zero_simple_counter (sm, ntohl (mp->sw_if_index));
688 }
689 }
690
691 BAD_SW_IF_INDEX_LABEL;
692
693 REPLY_MACRO (VL_API_SW_INTERFACE_CLEAR_STATS_REPLY);
694}
695
696#define API_LINK_STATE_EVENT 1
697#define API_ADMIN_UP_DOWN_EVENT 2
698
699static int
700event_data_cmp (void *a1, void *a2)
701{
702 uword *e1 = a1;
703 uword *e2 = a2;
704
705 return (word) e1[0] - (word) e2[0];
706}
707
708static void
Neale Rannsa07bd702017-08-07 07:53:49 -0700709send_sw_interface_event (vpe_api_main_t * am,
Neale Rannsd292ab12017-08-15 12:29:48 -0700710 vpe_client_registration_t * reg,
Florin Coras6c4dae22018-01-09 06:39:23 -0800711 vl_api_registration_t * vl_reg,
712 vnet_sw_interface_t * swif)
Dave Barach6d963c22016-12-05 09:50:05 -0500713{
Neale Rannsa07bd702017-08-07 07:53:49 -0700714 vl_api_sw_interface_event_t *mp;
Dave Barach6d963c22016-12-05 09:50:05 -0500715 vnet_main_t *vnm = am->vnet_main;
716
717 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm,
718 swif->sw_if_index);
719 mp = vl_msg_api_alloc (sizeof (*mp));
720 memset (mp, 0, sizeof (*mp));
Neale Rannsa07bd702017-08-07 07:53:49 -0700721 mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
Dave Barach6d963c22016-12-05 09:50:05 -0500722 mp->sw_if_index = ntohl (swif->sw_if_index);
Neale Rannsd292ab12017-08-15 12:29:48 -0700723 mp->client_index = reg->client_index;
724 mp->pid = reg->client_pid;
Dave Barach6d963c22016-12-05 09:50:05 -0500725
726 mp->admin_up_down = (swif->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? 1 : 0;
727 mp->link_up_down = (hi->flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? 1 : 0;
Florin Coras6c4dae22018-01-09 06:39:23 -0800728 vl_api_send_msg (vl_reg, (u8 *) mp);
Dave Barach6d963c22016-12-05 09:50:05 -0500729}
730
731static uword
732link_state_process (vlib_main_t * vm,
733 vlib_node_runtime_t * rt, vlib_frame_t * f)
734{
735 vpe_api_main_t *vam = &vpe_api_main;
736 vnet_main_t *vnm = vam->vnet_main;
737 vnet_sw_interface_t *swif;
738 uword *event_data = 0;
739 vpe_client_registration_t *reg;
740 int i;
741 u32 prev_sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800742 vl_api_registration_t *vl_reg;
Dave Barach6d963c22016-12-05 09:50:05 -0500743
744 vam->link_state_process_up = 1;
745
746 while (1)
747 {
748 vlib_process_wait_for_event (vm);
749
750 /* Unified list of changed link or admin state sw_if_indices */
751 vlib_process_get_events_with_type
752 (vm, &event_data, API_LINK_STATE_EVENT);
753 vlib_process_get_events_with_type
754 (vm, &event_data, API_ADMIN_UP_DOWN_EVENT);
755
756 /* Sort, so we can eliminate duplicates */
757 vec_sort_with_function (event_data, event_data_cmp);
758
759 prev_sw_if_index = ~0;
760
761 for (i = 0; i < vec_len (event_data); i++)
762 {
763 /* Only one message per swif */
764 if (prev_sw_if_index == event_data[i])
765 continue;
766 prev_sw_if_index = event_data[i];
767
768 /* *INDENT-OFF* */
769 pool_foreach(reg, vam->interface_events_registrations,
770 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800771 vl_reg = vl_api_client_index_to_registration (reg->client_index);
772 if (vl_reg)
Dave Barach6d963c22016-12-05 09:50:05 -0500773 {
774 /* sw_interface may be deleted already */
775 if (!pool_is_free_index (vnm->interface_main.sw_interfaces,
776 event_data[i]))
777 {
778 swif = vnet_get_sw_interface (vnm, event_data[i]);
Florin Coras6c4dae22018-01-09 06:39:23 -0800779 send_sw_interface_event (vam, reg, vl_reg, swif);
Dave Barach6d963c22016-12-05 09:50:05 -0500780 }
781 }
782 }));
783 /* *INDENT-ON* */
784 }
785 vec_reset_length (event_data);
786 }
787
788 return 0;
789}
790
791static clib_error_t *link_up_down_function (vnet_main_t * vm, u32 hw_if_index,
792 u32 flags);
793static clib_error_t *admin_up_down_function (vnet_main_t * vm,
794 u32 hw_if_index, u32 flags);
795
796/* *INDENT-OFF* */
797VLIB_REGISTER_NODE (link_state_process_node,static) = {
798 .function = link_state_process,
799 .type = VLIB_NODE_TYPE_PROCESS,
800 .name = "vpe-link-state-process",
801};
802/* *INDENT-ON* */
803
804VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (admin_up_down_function);
805VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (link_up_down_function);
806
807static clib_error_t *
808link_up_down_function (vnet_main_t * vm, u32 hw_if_index, u32 flags)
809{
810 vpe_api_main_t *vam = &vpe_api_main;
811 vnet_hw_interface_t *hi = vnet_get_hw_interface (vm, hw_if_index);
812
813 if (vam->link_state_process_up)
814 vlib_process_signal_event (vam->vlib_main,
815 link_state_process_node.index,
816 API_LINK_STATE_EVENT, hi->sw_if_index);
817 return 0;
818}
819
820static clib_error_t *
821admin_up_down_function (vnet_main_t * vm, u32 sw_if_index, u32 flags)
822{
823 vpe_api_main_t *vam = &vpe_api_main;
824
825 /*
826 * Note: it's perfectly fair to set a subif admin up / admin down.
827 * Note the subtle distinction between this routine and the previous
828 * routine.
829 */
830 if (vam->link_state_process_up)
831 vlib_process_signal_event (vam->vlib_main,
832 link_state_process_node.index,
833 API_ADMIN_UP_DOWN_EVENT, sw_if_index);
834 return 0;
835}
836
837static void vl_api_sw_interface_tag_add_del_t_handler
838 (vl_api_sw_interface_tag_add_del_t * mp)
839{
840 vnet_main_t *vnm = vnet_get_main ();
841 vl_api_sw_interface_tag_add_del_reply_t *rmp;
842 int rv = 0;
843 u8 *tag;
844 u32 sw_if_index = ntohl (mp->sw_if_index);
845
846 VALIDATE_SW_IF_INDEX (mp);
847
848 if (mp->is_add)
849 {
850 if (mp->tag[0] == 0)
851 {
852 rv = VNET_API_ERROR_INVALID_VALUE;
853 goto out;
854 }
855
856 mp->tag[ARRAY_LEN (mp->tag) - 1] = 0;
857 tag = format (0, "%s%c", mp->tag, 0);
858 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
859 }
860 else
861 vnet_clear_sw_interface_tag (vnm, sw_if_index);
862
863 BAD_SW_IF_INDEX_LABEL;
864out:
865 REPLY_MACRO (VL_API_SW_INTERFACE_TAG_ADD_DEL_REPLY);
866}
867
Jon Loeliger10c273b2017-03-30 08:39:33 -0500868static void vl_api_sw_interface_set_mac_address_t_handler
869 (vl_api_sw_interface_set_mac_address_t * mp)
870{
871 vl_api_sw_interface_set_mac_address_reply_t *rmp;
872 vnet_main_t *vnm = vnet_get_main ();
873 u32 sw_if_index = ntohl (mp->sw_if_index);
Neale Rannsd867a7c2017-10-04 02:29:07 -0700874 vnet_sw_interface_t *si;
Jon Loeliger10c273b2017-03-30 08:39:33 -0500875 clib_error_t *error;
876 int rv = 0;
877
878 VALIDATE_SW_IF_INDEX (mp);
879
Neale Rannsd867a7c2017-10-04 02:29:07 -0700880 si = vnet_get_sw_interface (vnm, sw_if_index);
John Lo62fcc0a2017-10-31 14:31:10 -0400881 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index,
882 mp->mac_address);
Jon Loeliger10c273b2017-03-30 08:39:33 -0500883 if (error)
884 {
885 rv = VNET_API_ERROR_UNIMPLEMENTED;
886 clib_error_report (error);
887 goto out;
888 }
889
890 BAD_SW_IF_INDEX_LABEL;
891out:
892 REPLY_MACRO (VL_API_SW_INTERFACE_SET_MAC_ADDRESS_REPLY);
893}
894
Juraj Slobodac0374232018-02-01 15:18:49 +0100895static void vl_api_sw_interface_get_mac_address_t_handler
896 (vl_api_sw_interface_get_mac_address_t * mp)
897{
898 vl_api_sw_interface_get_mac_address_reply_t *rmp;
899 vl_api_registration_t *reg;
900 vnet_main_t *vnm = vnet_get_main ();
901 u32 sw_if_index = ntohl (mp->sw_if_index);
902 vnet_sw_interface_t *si;
903 ethernet_interface_t *eth_if = 0;
904 int rv = 0;
905
906 VALIDATE_SW_IF_INDEX (mp);
907
908 si = vnet_get_sup_sw_interface (vnm, sw_if_index);
909 if (si->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
910 eth_if = ethernet_get_interface (&ethernet_main, si->hw_if_index);
911
912 BAD_SW_IF_INDEX_LABEL;
913
914 reg = vl_api_client_index_to_registration (mp->client_index);
915 if (!reg)
916 return;
917 rmp = vl_msg_api_alloc (sizeof (*rmp));
918 rmp->_vl_msg_id = htons (VL_API_SW_INTERFACE_GET_MAC_ADDRESS_REPLY);
919 rmp->context = mp->context;
920 rmp->retval = htonl (rv);
921 if (!rv && eth_if)
922 memcpy (rmp->mac_address, eth_if->address, 6);
923 vl_api_send_msg (reg, (u8 *) rmp);
924}
925
Stevenad8015b2017-10-29 22:10:46 -0700926static void vl_api_sw_interface_set_rx_mode_t_handler
927 (vl_api_sw_interface_set_rx_mode_t * mp)
928{
929 vl_api_sw_interface_set_rx_mode_reply_t *rmp;
930 vnet_main_t *vnm = vnet_get_main ();
931 u32 sw_if_index = ntohl (mp->sw_if_index);
932 vnet_sw_interface_t *si;
933 clib_error_t *error;
934 int rv = 0;
935
936 VALIDATE_SW_IF_INDEX (mp);
937
938 si = vnet_get_sw_interface (vnm, sw_if_index);
939 error = set_hw_interface_change_rx_mode (vnm, si->hw_if_index,
940 mp->queue_id_valid,
941 ntohl (mp->queue_id), mp->mode);
942 if (error)
943 {
944 rv = VNET_API_ERROR_UNIMPLEMENTED;
945 clib_error_report (error);
946 goto out;
947 }
948
949 BAD_SW_IF_INDEX_LABEL;
950out:
951 REPLY_MACRO (VL_API_SW_INTERFACE_SET_RX_MODE_REPLY);
952}
953
Neale Rannsb8d44812017-11-10 06:53:54 -0800954static void
955vl_api_create_vlan_subif_t_handler (vl_api_create_vlan_subif_t * mp)
956{
957 vl_api_create_vlan_subif_reply_t *rmp;
958 vnet_main_t *vnm = vnet_get_main ();
959 u32 sw_if_index = (u32) ~ 0;
960 vnet_hw_interface_t *hi;
961 int rv = 0;
962 u32 id;
963 vnet_sw_interface_t template;
964 uword *p;
965 vnet_interface_main_t *im = &vnm->interface_main;
966 u64 sup_and_sub_key;
Florin Coras6c4dae22018-01-09 06:39:23 -0800967 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -0800968 clib_error_t *error;
969
970 VALIDATE_SW_IF_INDEX (mp);
971
972 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
973
974 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
975 {
976 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
977 goto out;
978 }
979
980 id = ntohl (mp->vlan_id);
981 if (id == 0 || id > 4095)
982 {
983 rv = VNET_API_ERROR_INVALID_VLAN;
984 goto out;
985 }
986
987 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
988
989 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
990 if (p)
991 {
992 rv = VNET_API_ERROR_VLAN_ALREADY_EXISTS;
993 goto out;
994 }
995
996 memset (&template, 0, sizeof (template));
997 template.type = VNET_SW_INTERFACE_TYPE_SUB;
998 template.sup_sw_if_index = hi->sw_if_index;
999 template.sub.id = id;
1000 template.sub.eth.raw_flags = 0;
1001 template.sub.eth.flags.one_tag = 1;
1002 template.sub.eth.outer_vlan_id = id;
1003 template.sub.eth.flags.exact_match = 1;
1004
1005 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1006 if (error)
1007 {
1008 clib_error_report (error);
1009 rv = VNET_API_ERROR_INVALID_REGISTRATION;
1010 goto out;
1011 }
1012
1013 u64 *kp = clib_mem_alloc (sizeof (*kp));
1014 *kp = sup_and_sub_key;
1015
1016 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
1017 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1018
1019 BAD_SW_IF_INDEX_LABEL;
1020
1021out:
Florin Coras6c4dae22018-01-09 06:39:23 -08001022 reg = vl_api_client_index_to_registration (mp->client_index);
1023 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08001024 return;
1025
1026 rmp = vl_msg_api_alloc (sizeof (*rmp));
1027 rmp->_vl_msg_id = htons (VL_API_CREATE_VLAN_SUBIF_REPLY);
1028 rmp->context = mp->context;
1029 rmp->retval = htonl (rv);
1030 rmp->sw_if_index = htonl (sw_if_index);
Florin Coras6c4dae22018-01-09 06:39:23 -08001031 vl_api_send_msg (reg, (u8 *) rmp);
Neale Rannsb8d44812017-11-10 06:53:54 -08001032}
1033
1034static void
1035vl_api_create_subif_t_handler (vl_api_create_subif_t * mp)
1036{
1037 vl_api_create_subif_reply_t *rmp;
1038 vnet_main_t *vnm = vnet_get_main ();
1039 u32 sw_if_index = ~0;
1040 int rv = 0;
1041 u32 sub_id;
1042 vnet_sw_interface_t *si;
1043 vnet_hw_interface_t *hi;
1044 vnet_sw_interface_t template;
1045 uword *p;
1046 vnet_interface_main_t *im = &vnm->interface_main;
1047 u64 sup_and_sub_key;
1048 clib_error_t *error;
1049
1050 VALIDATE_SW_IF_INDEX (mp);
1051
1052 si = vnet_get_sup_sw_interface (vnm, ntohl (mp->sw_if_index));
1053 hi = vnet_get_sup_hw_interface (vnm, ntohl (mp->sw_if_index));
1054
1055 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
1056 {
1057 rv = VNET_API_ERROR_BOND_SLAVE_NOT_ALLOWED;
1058 goto out;
1059 }
1060
1061 sw_if_index = si->sw_if_index;
1062 sub_id = ntohl (mp->sub_id);
1063
1064 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) sub_id;
1065
1066 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1067 if (p)
1068 {
1069 if (CLIB_DEBUG > 0)
1070 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
1071 sw_if_index, sub_id);
1072 rv = VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
1073 goto out;
1074 }
1075
1076 memset (&template, 0, sizeof (template));
1077 template.type = VNET_SW_INTERFACE_TYPE_SUB;
1078 template.sup_sw_if_index = sw_if_index;
1079 template.sub.id = sub_id;
1080 template.sub.eth.flags.no_tags = mp->no_tags;
1081 template.sub.eth.flags.one_tag = mp->one_tag;
1082 template.sub.eth.flags.two_tags = mp->two_tags;
1083 template.sub.eth.flags.dot1ad = mp->dot1ad;
1084 template.sub.eth.flags.exact_match = mp->exact_match;
1085 template.sub.eth.flags.default_sub = mp->default_sub;
1086 template.sub.eth.flags.outer_vlan_id_any = mp->outer_vlan_id_any;
1087 template.sub.eth.flags.inner_vlan_id_any = mp->inner_vlan_id_any;
1088 template.sub.eth.outer_vlan_id = ntohs (mp->outer_vlan_id);
1089 template.sub.eth.inner_vlan_id = ntohs (mp->inner_vlan_id);
1090
1091 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
1092 if (error)
1093 {
1094 clib_error_report (error);
1095 rv = VNET_API_ERROR_SUBIF_CREATE_FAILED;
1096 goto out;
1097 }
1098
1099 u64 *kp = clib_mem_alloc (sizeof (*kp));
1100 *kp = sup_and_sub_key;
1101
1102 hash_set (hi->sub_interface_sw_if_index_by_id, sub_id, sw_if_index);
1103 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
1104
1105 BAD_SW_IF_INDEX_LABEL;
1106
1107out:
1108
1109 /* *INDENT-OFF* */
1110 REPLY_MACRO2(VL_API_CREATE_SUBIF_REPLY,
1111 ({
1112 rmp->sw_if_index = ntohl(sw_if_index);
1113 }));
1114 /* *INDENT-ON* */
1115}
1116
1117static void
1118vl_api_delete_subif_t_handler (vl_api_delete_subif_t * mp)
1119{
1120 vl_api_delete_subif_reply_t *rmp;
1121 int rv;
1122
1123 rv = vnet_delete_sub_interface (ntohl (mp->sw_if_index));
1124
1125 REPLY_MACRO (VL_API_DELETE_SUBIF_REPLY);
1126}
1127
1128static void
1129vl_api_interface_name_renumber_t_handler (vl_api_interface_name_renumber_t *
1130 mp)
1131{
1132 vl_api_interface_name_renumber_reply_t *rmp;
1133 int rv = 0;
1134
1135 VALIDATE_SW_IF_INDEX (mp);
1136
1137 rv = vnet_interface_name_renumber
1138 (ntohl (mp->sw_if_index), ntohl (mp->new_show_dev_instance));
1139
1140 BAD_SW_IF_INDEX_LABEL;
1141
1142 REPLY_MACRO (VL_API_INTERFACE_NAME_RENUMBER_REPLY);
1143}
1144
1145static void
1146vl_api_create_loopback_t_handler (vl_api_create_loopback_t * mp)
1147{
1148 vl_api_create_loopback_reply_t *rmp;
1149 u32 sw_if_index;
1150 int rv;
1151
1152 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address, 0, 0);
1153
1154 /* *INDENT-OFF* */
1155 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_REPLY,
1156 ({
1157 rmp->sw_if_index = ntohl (sw_if_index);
1158 }));
1159 /* *INDENT-ON* */
1160}
1161
1162static void vl_api_create_loopback_instance_t_handler
1163 (vl_api_create_loopback_instance_t * mp)
1164{
1165 vl_api_create_loopback_instance_reply_t *rmp;
1166 u32 sw_if_index;
1167 u8 is_specified = mp->is_specified;
1168 u32 user_instance = ntohl (mp->user_instance);
1169 int rv;
1170
1171 rv = vnet_create_loopback_interface (&sw_if_index, mp->mac_address,
1172 is_specified, user_instance);
1173
1174 /* *INDENT-OFF* */
1175 REPLY_MACRO2(VL_API_CREATE_LOOPBACK_INSTANCE_REPLY,
1176 ({
1177 rmp->sw_if_index = ntohl (sw_if_index);
1178 }));
1179 /* *INDENT-ON* */
1180}
1181
1182static void
1183vl_api_delete_loopback_t_handler (vl_api_delete_loopback_t * mp)
1184{
1185 vl_api_delete_loopback_reply_t *rmp;
1186 u32 sw_if_index;
1187 int rv;
1188
1189 sw_if_index = ntohl (mp->sw_if_index);
1190 rv = vnet_delete_loopback_interface (sw_if_index);
1191
1192 REPLY_MACRO (VL_API_DELETE_LOOPBACK_REPLY);
1193}
1194
Neale Ranns6f4a6be2018-03-16 16:26:21 -07001195static void
1196 vl_api_collect_detailed_interface_stats_t_handler
1197 (vl_api_collect_detailed_interface_stats_t * mp)
1198{
1199 vl_api_collect_detailed_interface_stats_reply_t *rmp;
1200 int rv = 0;
1201
Neale Ranns871dc422018-03-29 01:28:09 -07001202 rv =
1203 vnet_sw_interface_stats_collect_enable_disable (ntohl (mp->sw_if_index),
1204 mp->enable_disable);
Neale Ranns6f4a6be2018-03-16 16:26:21 -07001205
1206 REPLY_MACRO (VL_API_COLLECT_DETAILED_INTERFACE_STATS_REPLY);
1207}
1208
Dave Barachaff70772016-10-31 11:59:07 -04001209/*
1210 * vpe_api_hookup
1211 * Add vpe's API message handlers to the table.
1212 * vlib has alread mapped shared memory and
1213 * added the client registration handlers.
1214 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
1215 */
1216#define vl_msg_name_crc_list
Dave Barachb5e8a772016-12-06 12:04:42 -05001217#include <vnet/interface.api.h>
Dave Barachaff70772016-10-31 11:59:07 -04001218#undef vl_msg_name_crc_list
1219
1220static void
1221setup_message_id_table (api_main_t * am)
1222{
1223#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1224 foreach_vl_msg_name_crc_interface;
1225#undef _
1226}
1227
Dave Barach6d963c22016-12-05 09:50:05 -05001228pub_sub_handler (interface_events, INTERFACE_EVENTS);
1229
Dave Barachaff70772016-10-31 11:59:07 -04001230static clib_error_t *
1231interface_api_hookup (vlib_main_t * vm)
1232{
1233 api_main_t *am = &api_main;
1234
1235#define _(N,n) \
1236 vl_msg_api_set_handlers(VL_API_##N, #n, \
1237 vl_api_##n##_t_handler, \
1238 vl_noop_handler, \
1239 vl_api_##n##_t_endian, \
1240 vl_api_##n##_t_print, \
1241 sizeof(vl_api_##n##_t), 1);
1242 foreach_vpe_api_msg;
1243#undef _
1244
1245 /*
1246 * Set up the (msg_name, crc, message-id) table
1247 */
1248 setup_message_id_table (am);
1249
1250 return 0;
1251}
1252
1253VLIB_API_INIT_FUNCTION (interface_api_hookup);
1254
1255/*
1256 * fd.io coding-style-patch-verification: ON
1257 *
1258 * Local Variables:
1259 * eval: (c-set-style "gnu")
1260 * End:
1261 */