1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Zebra Layer-2 interface handling code
4 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
19 #include "zebra/rib.h"
21 #include "zebra/zebra_ns.h"
22 #include "zebra/zserv.h"
23 #include "zebra/debug.h"
24 #include "zebra/interface.h"
25 #include "zebra/zebra_vrf.h"
26 #include "zebra/rt_netlink.h"
27 #include "zebra/interface.h"
28 #include "zebra/zebra_l2.h"
29 #include "zebra/zebra_l2_bridge_if.h"
30 #include "zebra/zebra_vxlan.h"
31 #include "zebra/zebra_vxlan_if.h"
32 #include "zebra/zebra_evpn_mh.h"
36 /* static function declarations */
38 /* Private functions */
39 static void map_slaves_to_bridge(struct interface
*br_if
, int link
,
40 bool update_slave
, uint8_t chgflags
)
43 struct interface
*ifp
;
44 struct zebra_vrf
*zvrf
;
47 zvrf
= br_if
->vrf
->info
;
51 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
52 FOR_ALL_INTERFACES (vrf
, ifp
) {
54 struct zebra_l2info_brslave
*br_slave
;
56 if (ifp
->ifindex
== IFINDEX_INTERNAL
|| !ifp
->info
)
58 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
61 /* NOTE: This assumes 'zebra_l2info_brslave' is the
63 * for any L2 interface.
65 zif
= (struct zebra_if
*)ifp
->info
;
66 br_slave
= &zif
->brslave_info
;
69 if (br_slave
->bridge_ifindex
== br_if
->ifindex
70 && br_slave
->ns_id
== zns
->ns_id
) {
71 br_slave
->br_if
= br_if
;
73 zebra_l2if_update_bridge_slave(
75 br_slave
->bridge_ifindex
,
81 if (br_slave
->br_if
== br_if
)
82 br_slave
->br_if
= NULL
;
88 /* Public functions */
89 void zebra_l2_map_slave_to_bridge(struct zebra_l2info_brslave
*br_slave
,
92 struct interface
*br_if
;
94 /* TODO: Handle change of master */
96 br_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(zns
->ns_id
),
97 br_slave
->bridge_ifindex
);
99 br_slave
->br_if
= br_if
;
102 void zebra_l2_unmap_slave_from_bridge(struct zebra_l2info_brslave
*br_slave
)
104 br_slave
->br_if
= NULL
;
107 /* If any of the bond members are in bypass state the bond is placed
110 static void zebra_l2_bond_lacp_bypass_eval(struct zebra_if
*bond_zif
)
112 struct listnode
*node
;
113 struct zebra_if
*bond_mbr
;
114 bool old_bypass
= !!(bond_zif
->flags
& ZIF_FLAG_LACP_BYPASS
);
115 bool new_bypass
= false;
117 if (bond_zif
->bond_info
.mbr_zifs
) {
118 for (ALL_LIST_ELEMENTS_RO(bond_zif
->bond_info
.mbr_zifs
, node
,
120 if (bond_mbr
->flags
& ZIF_FLAG_LACP_BYPASS
) {
127 if (old_bypass
== new_bypass
)
130 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
131 zlog_debug("bond %s lacp bypass changed to %s",
132 bond_zif
->ifp
->name
, new_bypass
? "on" : "off");
135 bond_zif
->flags
|= ZIF_FLAG_LACP_BYPASS
;
137 bond_zif
->flags
&= ~ZIF_FLAG_LACP_BYPASS
;
139 if (bond_zif
->es_info
.es
)
140 zebra_evpn_es_bypass_update(bond_zif
->es_info
.es
, bond_zif
->ifp
,
144 /* Returns true if member was newly linked to bond */
145 void zebra_l2_map_slave_to_bond(struct zebra_if
*zif
, vrf_id_t vrf_id
)
147 struct interface
*bond_if
;
148 struct zebra_if
*bond_zif
;
149 struct zebra_l2info_bondslave
*bond_slave
= &zif
->bondslave_info
;
151 bond_if
= if_lookup_by_index(bond_slave
->bond_ifindex
, vrf_id
);
152 if (bond_if
== bond_slave
->bond_if
)
155 /* unlink the slave from the old master */
156 zebra_l2_unmap_slave_from_bond(zif
);
158 /* If the bond is present and ready link the bond-member
161 if (bond_if
&& (bond_zif
= bond_if
->info
)) {
162 if (bond_zif
->bond_info
.mbr_zifs
) {
163 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
164 zlog_debug("bond mbr %s linked to %s",
165 zif
->ifp
->name
, bond_if
->name
);
166 bond_slave
->bond_if
= bond_if
;
167 /* link the slave to the new bond master */
168 listnode_add(bond_zif
->bond_info
.mbr_zifs
, zif
);
169 /* inherit protodown flags from the es-bond */
170 if (zebra_evpn_is_es_bond(bond_if
))
171 zebra_evpn_mh_update_protodown_bond_mbr(
172 zif
, false /*clear*/, __func__
);
173 zebra_l2_bond_lacp_bypass_eval(bond_zif
);
176 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
177 zlog_debug("bond mbr %s link to bond skipped",
182 void zebra_l2_unmap_slave_from_bond(struct zebra_if
*zif
)
184 struct zebra_l2info_bondslave
*bond_slave
= &zif
->bondslave_info
;
185 struct zebra_if
*bond_zif
;
187 if (!bond_slave
->bond_if
) {
188 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
189 zlog_debug("bond mbr %s unlink from bond skipped",
194 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
195 zlog_debug("bond mbr %s un-linked from %s", zif
->ifp
->name
,
196 bond_slave
->bond_if
->name
);
198 /* unlink the slave from the bond master */
199 bond_zif
= bond_slave
->bond_if
->info
;
200 /* clear protodown flags */
201 if (zebra_evpn_is_es_bond(bond_zif
->ifp
))
202 zebra_evpn_mh_update_protodown_bond_mbr(zif
, true /*clear*/,
204 listnode_delete(bond_zif
->bond_info
.mbr_zifs
, zif
);
205 bond_slave
->bond_if
= NULL
;
206 zebra_l2_bond_lacp_bypass_eval(bond_zif
);
209 void zebra_l2if_update_bond(struct interface
*ifp
, bool add
)
211 struct zebra_if
*zif
;
212 struct zebra_l2info_bond
*bond
;
216 bond
= &zif
->bond_info
;
219 if (!bond
->mbr_zifs
) {
220 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
221 zlog_debug("bond %s mbr list create",
223 bond
->mbr_zifs
= list_new();
226 struct listnode
*node
;
227 struct listnode
*nnode
;
228 struct zebra_if
*bond_mbr
;
233 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
234 zlog_debug("bond %s mbr list delete", ifp
->name
);
235 for (ALL_LIST_ELEMENTS(bond
->mbr_zifs
, node
, nnode
, bond_mbr
))
236 zebra_l2_unmap_slave_from_bond(bond_mbr
);
238 list_delete(&bond
->mbr_zifs
);
243 * Handle Bridge interface add or update. Update relevant info,
244 * map slaves (if any) to the bridge.
246 void zebra_l2_bridge_add_update(struct interface
*ifp
,
247 struct zebra_l2info_bridge
*bridge_info
,
250 struct zebra_if
*zif
;
251 struct zebra_l2_bridge_if
*br
;
256 br
= BRIDGE_FROM_ZEBRA_IF(zif
);
257 br
->vlan_aware
= bridge_info
->bridge
.vlan_aware
;
258 zebra_l2_bridge_if_add(ifp
);
260 /* Link all slaves to this bridge */
261 map_slaves_to_bridge(ifp
, 1, false, ZEBRA_BRIDGE_NO_ACTION
);
265 * Handle Bridge interface delete.
267 void zebra_l2_bridge_del(struct interface
*ifp
)
269 zebra_l2_bridge_if_del(ifp
);
271 /* Unlink all slaves to this bridge */
272 map_slaves_to_bridge(ifp
, 0, false, ZEBRA_BRIDGE_NO_ACTION
);
275 void zebra_l2if_update_bridge(struct interface
*ifp
, uint8_t chgflags
)
279 map_slaves_to_bridge(ifp
, 1, true, chgflags
);
283 * Update L2 info for a VLAN interface. Only relevant parameter is the
284 * VLAN Id and this cannot change.
286 void zebra_l2_vlanif_update(struct interface
*ifp
,
287 struct zebra_l2info_vlan
*vlan_info
)
289 struct zebra_if
*zif
;
294 /* Copy over the L2 information. */
295 memcpy(&zif
->l2info
.vl
, vlan_info
, sizeof(*vlan_info
));
299 * Update L2 info for a GRE interface. This is called upon interface
300 * addition as well as update. Upon add/update, need to inform
301 * clients about GRE information.
303 void zebra_l2_greif_add_update(struct interface
*ifp
,
304 struct zebra_l2info_gre
*gre_info
, int add
)
306 struct zebra_if
*zif
;
307 struct in_addr old_vtep_ip
;
313 memcpy(&zif
->l2info
.gre
, gre_info
, sizeof(*gre_info
));
317 old_vtep_ip
= zif
->l2info
.gre
.vtep_ip
;
318 if (IPV4_ADDR_SAME(&old_vtep_ip
, &gre_info
->vtep_ip
))
321 zif
->l2info
.gre
.vtep_ip
= gre_info
->vtep_ip
;
325 * Update L2 info for a VxLAN interface. This is called upon interface
326 * addition as well as update. Upon add, need to invoke the VNI create
327 * function. Upon update, the params of interest are the local tunnel
328 * IP and VLAN mapping, but the latter is handled separately.
330 void zebra_l2_vxlanif_add_update(struct interface
*ifp
,
331 struct zebra_l2info_vxlan
*vxlan_info
, int add
)
333 struct zebra_if
*zif
;
334 uint16_t chgflags
= 0;
335 struct zebra_vxlan_if_update_ctx ctx
;
341 memcpy(&zif
->l2info
.vxl
, vxlan_info
, sizeof(*vxlan_info
));
342 zebra_vxlan_if_add(ifp
);
346 memset(&ctx
, 0, sizeof(ctx
));
347 ctx
.old_vtep_ip
= zif
->l2info
.vxl
.vtep_ip
;
349 if (!IPV4_ADDR_SAME(&ctx
.old_vtep_ip
, &vxlan_info
->vtep_ip
)) {
350 chgflags
|= ZEBRA_VXLIF_LOCAL_IP_CHANGE
;
351 zif
->l2info
.vxl
.vtep_ip
= vxlan_info
->vtep_ip
;
354 if (IS_ZEBRA_VXLAN_IF_VNI(zif
)) {
355 ctx
.old_vni
= vxlan_info
->vni_info
.vni
;
356 if (!IPV4_ADDR_SAME(&zif
->l2info
.vxl
.vni_info
.vni
.mcast_grp
,
357 &vxlan_info
->vni_info
.vni
.mcast_grp
)) {
358 chgflags
|= ZEBRA_VXLIF_MCAST_GRP_CHANGE
;
359 zif
->l2info
.vxl
.vni_info
.vni
.mcast_grp
=
360 vxlan_info
->vni_info
.vni
.mcast_grp
;
365 ctx
.chgflags
= chgflags
;
366 zebra_vxlan_if_update(ifp
, &ctx
);
371 * Handle change to VLAN to VNI mapping.
373 void zebra_l2_vxlanif_update_access_vlan(struct interface
*ifp
,
374 vlanid_t access_vlan
)
376 struct zebra_if
*zif
;
377 vlanid_t old_access_vlan
;
378 struct zebra_vxlan_vni
*vni
;
379 struct zebra_vxlan_if_update_ctx ctx
;
385 /* This would be called only in non svd case */
386 assert(IS_ZEBRA_VXLAN_IF_VNI(zif
));
388 old_access_vlan
= zif
->l2info
.vxl
.vni_info
.vni
.access_vlan
;
390 if (old_access_vlan
== access_vlan
)
393 memset(&ctx
, 0, sizeof(ctx
));
394 vni
= zebra_vxlan_if_vni_find(zif
, 0);
396 ctx
.chgflags
= ZEBRA_VXLIF_VLAN_CHANGE
;
397 vni
->access_vlan
= access_vlan
;
399 zebra_evpn_vl_vxl_deref(old_access_vlan
, vni
->vni
, zif
);
400 zebra_evpn_vl_vxl_ref(access_vlan
, vni
->vni
, zif
);
401 zebra_vxlan_if_update(ifp
, &ctx
);
405 * Handle VxLAN interface delete.
407 void zebra_l2_vxlanif_del(struct interface
*ifp
)
409 struct zebra_if
*zif
;
414 zebra_vxlan_if_del(ifp
);
418 * Map or unmap interface from bridge.
419 * NOTE: It is currently assumped that an interface has to be unmapped
420 * from a bridge before it can be mapped to another bridge.
422 void zebra_l2if_update_bridge_slave(struct interface
*ifp
,
423 ifindex_t bridge_ifindex
, ns_id_t ns_id
,
426 struct zebra_if
*zif
;
427 ifindex_t old_bridge_ifindex
;
429 struct zebra_vrf
*zvrf
;
430 struct zebra_vxlan_if_update_ctx ctx
;
432 memset(&ctx
, 0, sizeof(ctx
));
437 zvrf
= ifp
->vrf
->info
;
441 if (zif
->zif_type
== ZEBRA_IF_VXLAN
442 && chgflags
!= ZEBRA_BRIDGE_NO_ACTION
) {
443 if (chgflags
& ZEBRA_BRIDGE_MASTER_MAC_CHANGE
) {
444 ctx
.chgflags
= ZEBRA_VXLIF_MASTER_MAC_CHANGE
;
445 zebra_vxlan_if_update(ifp
, &ctx
);
447 if (chgflags
& ZEBRA_BRIDGE_MASTER_UP
) {
448 ctx
.chgflags
= ZEBRA_VXLIF_MASTER_CHANGE
;
449 zebra_vxlan_if_update(ifp
, &ctx
);
452 old_bridge_ifindex
= zif
->brslave_info
.bridge_ifindex
;
453 old_ns_id
= zif
->brslave_info
.ns_id
;
454 if (old_bridge_ifindex
== bridge_ifindex
&&
455 old_ns_id
== zif
->brslave_info
.ns_id
)
458 ctx
.chgflags
= ZEBRA_VXLIF_MASTER_CHANGE
;
461 zif
->brslave_info
.ns_id
= ns_id
;
462 zif
->brslave_info
.bridge_ifindex
= bridge_ifindex
;
463 /* Set up or remove link with master */
464 if (bridge_ifindex
!= IFINDEX_INTERNAL
) {
465 zebra_l2_map_slave_to_bridge(&zif
->brslave_info
, zvrf
->zns
);
466 /* In the case of VxLAN, invoke the handler for EVPN. */
467 if (zif
->zif_type
== ZEBRA_IF_VXLAN
)
468 zebra_vxlan_if_update(ifp
, &ctx
);
470 zebra_evpn_es_local_br_port_update(zif
);
471 } else if (old_bridge_ifindex
!= IFINDEX_INTERNAL
) {
473 * In the case of VxLAN, invoke the handler for EVPN.
474 * Note that this should be done *prior*
475 * to unmapping the interface from the bridge.
477 if (zif
->zif_type
== ZEBRA_IF_VXLAN
)
478 zebra_vxlan_if_update(ifp
, &ctx
);
480 zebra_evpn_es_local_br_port_update(zif
);
481 zebra_l2_unmap_slave_from_bridge(&zif
->brslave_info
);
485 void zebra_l2if_update_bond_slave(struct interface
*ifp
, ifindex_t bond_ifindex
,
488 struct zebra_if
*zif
;
489 ifindex_t old_bond_ifindex
;
491 struct zebra_l2info_bondslave
*bond_mbr
;
496 old_bypass
= !!(zif
->flags
& ZIF_FLAG_LACP_BYPASS
);
497 if (old_bypass
!= new_bypass
) {
498 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
499 zlog_debug("bond-mbr %s lacp bypass changed to %s",
500 zif
->ifp
->name
, new_bypass
? "on" : "off");
503 zif
->flags
|= ZIF_FLAG_LACP_BYPASS
;
505 zif
->flags
&= ~ZIF_FLAG_LACP_BYPASS
;
507 bond_mbr
= &zif
->bondslave_info
;
508 if (bond_mbr
->bond_if
) {
509 struct zebra_if
*bond_zif
= bond_mbr
->bond_if
->info
;
511 zebra_l2_bond_lacp_bypass_eval(bond_zif
);
515 old_bond_ifindex
= zif
->bondslave_info
.bond_ifindex
;
516 if (old_bond_ifindex
== bond_ifindex
)
519 zif
->bondslave_info
.bond_ifindex
= bond_ifindex
;
521 /* Set up or remove link with master */
522 if (bond_ifindex
!= IFINDEX_INTERNAL
)
523 zebra_l2_map_slave_to_bond(zif
, ifp
->vrf
->vrf_id
);
524 else if (old_bond_ifindex
!= IFINDEX_INTERNAL
)
525 zebra_l2_unmap_slave_from_bond(zif
);
528 void zebra_vlan_bitmap_compute(struct interface
*ifp
,
529 uint32_t vid_start
, uint16_t vid_end
)
532 struct zebra_if
*zif
;
534 zif
= (struct zebra_if
*)ifp
->info
;
537 for (vid
= vid_start
; vid
<= vid_end
; ++vid
)
538 bf_set_bit(zif
->vlan_bitmap
, vid
);
541 void zebra_vlan_mbr_re_eval(struct interface
*ifp
, bitfield_t old_vlan_bitmap
)
544 struct zebra_if
*zif
;
546 zif
= (struct zebra_if
*)ifp
->info
;
549 if (!bf_cmp(zif
->vlan_bitmap
, old_vlan_bitmap
))
553 bf_for_each_set_bit(zif
->vlan_bitmap
, vid
, IF_VLAN_BITMAP_MAX
) {
554 /* if not already set create new reference */
555 if (!bf_test_index(old_vlan_bitmap
, vid
))
556 zebra_evpn_vl_mbr_ref(vid
, zif
);
558 /* also clear from the old vlan bitmap */
559 bf_release_index(old_vlan_bitmap
, vid
);
562 /* any bits remaining in the old vlan bitmap are stale references */
563 bf_for_each_set_bit(old_vlan_bitmap
, vid
, IF_VLAN_BITMAP_MAX
) {
564 zebra_evpn_vl_mbr_deref(vid
, zif
);