2 * Zebra Layer-2 interface handling code
3 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
5 * This file is part of FRR.
7 * FRR is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * FRR is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with FRR; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
35 #include "zebra/rib.h"
37 #include "zebra/zebra_ns.h"
38 #include "zebra/zserv.h"
39 #include "zebra/debug.h"
40 #include "zebra/interface.h"
41 #include "zebra/zebra_vrf.h"
42 #include "zebra/rt_netlink.h"
43 #include "zebra/interface.h"
44 #include "zebra/zebra_l2.h"
45 #include "zebra/zebra_vxlan.h"
46 #include "zebra/zebra_evpn_mh.h"
50 /* static function declarations */
52 /* Private functions */
53 static void map_slaves_to_bridge(struct interface
*br_if
, int link
,
54 bool update_slave
, uint8_t chgflags
)
57 struct interface
*ifp
;
58 struct zebra_vrf
*zvrf
;
61 zvrf
= zebra_vrf_lookup_by_id(br_if
->vrf_id
);
65 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
66 FOR_ALL_INTERFACES (vrf
, ifp
) {
68 struct zebra_l2info_brslave
*br_slave
;
70 if (ifp
->ifindex
== IFINDEX_INTERNAL
|| !ifp
->info
)
72 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
75 /* NOTE: This assumes 'zebra_l2info_brslave' is the
77 * for any L2 interface.
79 zif
= (struct zebra_if
*)ifp
->info
;
80 br_slave
= &zif
->brslave_info
;
83 if (br_slave
->bridge_ifindex
== br_if
->ifindex
84 && br_slave
->ns_id
== zns
->ns_id
) {
85 br_slave
->br_if
= br_if
;
87 zebra_l2if_update_bridge_slave(
89 br_slave
->bridge_ifindex
,
95 if (br_slave
->br_if
== br_if
)
96 br_slave
->br_if
= NULL
;
102 /* Public functions */
103 void zebra_l2_map_slave_to_bridge(struct zebra_l2info_brslave
*br_slave
,
104 struct zebra_ns
*zns
)
106 struct interface
*br_if
;
108 /* TODO: Handle change of master */
110 br_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(zns
->ns_id
),
111 br_slave
->bridge_ifindex
);
113 br_slave
->br_if
= br_if
;
116 void zebra_l2_unmap_slave_from_bridge(struct zebra_l2info_brslave
*br_slave
)
118 br_slave
->br_if
= NULL
;
121 /* If any of the bond members are in bypass state the bond is placed
124 static void zebra_l2_bond_lacp_bypass_eval(struct zebra_if
*bond_zif
)
126 struct listnode
*node
;
127 struct zebra_if
*bond_mbr
;
128 bool old_bypass
= !!(bond_zif
->flags
& ZIF_FLAG_LACP_BYPASS
);
129 bool new_bypass
= false;
131 if (bond_zif
->bond_info
.mbr_zifs
) {
132 for (ALL_LIST_ELEMENTS_RO(bond_zif
->bond_info
.mbr_zifs
, node
,
134 if (bond_mbr
->flags
& ZIF_FLAG_LACP_BYPASS
) {
141 if (old_bypass
== new_bypass
)
144 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
145 zlog_debug("bond %s lacp bypass changed to %s",
146 bond_zif
->ifp
->name
, new_bypass
? "on" : "off");
149 bond_zif
->flags
|= ZIF_FLAG_LACP_BYPASS
;
151 bond_zif
->flags
&= ~ZIF_FLAG_LACP_BYPASS
;
153 if (bond_zif
->es_info
.es
)
154 zebra_evpn_es_bypass_update(bond_zif
->es_info
.es
, bond_zif
->ifp
,
158 /* Returns true if member was newly linked to bond */
159 void zebra_l2_map_slave_to_bond(struct zebra_if
*zif
, vrf_id_t vrf_id
)
161 struct interface
*bond_if
;
162 struct zebra_if
*bond_zif
;
163 struct zebra_l2info_bondslave
*bond_slave
= &zif
->bondslave_info
;
165 bond_if
= if_lookup_by_index_all_vrf(bond_slave
->bond_ifindex
);
166 if (bond_if
== bond_slave
->bond_if
)
169 /* unlink the slave from the old master */
170 zebra_l2_unmap_slave_from_bond(zif
);
172 /* If the bond is present and ready link the bond-member
175 if (bond_if
&& (bond_zif
= bond_if
->info
)) {
176 if (bond_zif
->bond_info
.mbr_zifs
) {
177 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
178 zlog_debug("bond mbr %s linked to %s",
179 zif
->ifp
->name
, bond_if
->name
);
180 bond_slave
->bond_if
= bond_if
;
181 /* link the slave to the new bond master */
182 listnode_add(bond_zif
->bond_info
.mbr_zifs
, zif
);
183 /* inherit protodown flags from the es-bond */
184 if (zebra_evpn_is_es_bond(bond_if
))
185 zebra_evpn_mh_update_protodown_bond_mbr(
186 zif
, false /*clear*/, __func__
);
187 zebra_l2_bond_lacp_bypass_eval(bond_zif
);
190 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
191 zlog_debug("bond mbr %s link to bond skipped",
196 void zebra_l2_unmap_slave_from_bond(struct zebra_if
*zif
)
198 struct zebra_l2info_bondslave
*bond_slave
= &zif
->bondslave_info
;
199 struct zebra_if
*bond_zif
;
201 if (!bond_slave
->bond_if
) {
202 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
203 zlog_debug("bond mbr %s unlink from bond skipped",
208 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
209 zlog_debug("bond mbr %s un-linked from %s", zif
->ifp
->name
,
210 bond_slave
->bond_if
->name
);
212 /* unlink the slave from the bond master */
213 bond_zif
= bond_slave
->bond_if
->info
;
214 /* clear protodown flags */
215 if (zebra_evpn_is_es_bond(bond_zif
->ifp
))
216 zebra_evpn_mh_update_protodown_bond_mbr(zif
, true /*clear*/,
218 listnode_delete(bond_zif
->bond_info
.mbr_zifs
, zif
);
219 bond_slave
->bond_if
= NULL
;
220 zebra_l2_bond_lacp_bypass_eval(bond_zif
);
223 void zebra_l2if_update_bond(struct interface
*ifp
, bool add
)
225 struct zebra_if
*zif
;
226 struct zebra_l2info_bond
*bond
;
230 bond
= &zif
->bond_info
;
233 if (!bond
->mbr_zifs
) {
234 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
235 zlog_debug("bond %s mbr list create",
237 bond
->mbr_zifs
= list_new();
240 struct listnode
*node
;
241 struct listnode
*nnode
;
242 struct zebra_if
*bond_mbr
;
247 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
248 zlog_debug("bond %s mbr list delete", ifp
->name
);
249 for (ALL_LIST_ELEMENTS(bond
->mbr_zifs
, node
, nnode
, bond_mbr
))
250 zebra_l2_unmap_slave_from_bond(bond_mbr
);
252 list_delete(&bond
->mbr_zifs
);
257 * Handle Bridge interface add or update. Update relevant info,
258 * map slaves (if any) to the bridge.
260 void zebra_l2_bridge_add_update(struct interface
*ifp
,
261 struct zebra_l2info_bridge
*bridge_info
,
264 struct zebra_if
*zif
;
269 /* Copy over the L2 information. */
270 memcpy(&zif
->l2info
.br
, bridge_info
, sizeof(*bridge_info
));
272 /* Link all slaves to this bridge */
273 map_slaves_to_bridge(ifp
, 1, false, ZEBRA_BRIDGE_NO_ACTION
);
277 * Handle Bridge interface delete.
279 void zebra_l2_bridge_del(struct interface
*ifp
)
281 /* Unlink all slaves to this bridge */
282 map_slaves_to_bridge(ifp
, 0, false, ZEBRA_BRIDGE_NO_ACTION
);
285 void zebra_l2if_update_bridge(struct interface
*ifp
, uint8_t chgflags
)
289 map_slaves_to_bridge(ifp
, 1, true, chgflags
);
293 * Update L2 info for a VLAN interface. Only relevant parameter is the
294 * VLAN Id and this cannot change.
296 void zebra_l2_vlanif_update(struct interface
*ifp
,
297 struct zebra_l2info_vlan
*vlan_info
)
299 struct zebra_if
*zif
;
304 /* Copy over the L2 information. */
305 memcpy(&zif
->l2info
.vl
, vlan_info
, sizeof(*vlan_info
));
309 * Update L2 info for a GRE interface. This is called upon interface
310 * addition as well as update. Upon add/update, need to inform
311 * clients about GRE information.
313 void zebra_l2_greif_add_update(struct interface
*ifp
,
314 struct zebra_l2info_gre
*gre_info
, int add
)
316 struct zebra_if
*zif
;
317 struct in_addr old_vtep_ip
;
323 memcpy(&zif
->l2info
.gre
, gre_info
, sizeof(*gre_info
));
327 old_vtep_ip
= zif
->l2info
.gre
.vtep_ip
;
328 if (IPV4_ADDR_SAME(&old_vtep_ip
, &gre_info
->vtep_ip
))
331 zif
->l2info
.gre
.vtep_ip
= gre_info
->vtep_ip
;
335 * Update L2 info for a VxLAN interface. This is called upon interface
336 * addition as well as update. Upon add, need to invoke the VNI create
337 * function. Upon update, the params of interest are the local tunnel
338 * IP and VLAN mapping, but the latter is handled separately.
340 void zebra_l2_vxlanif_add_update(struct interface
*ifp
,
341 struct zebra_l2info_vxlan
*vxlan_info
, int add
)
343 struct zebra_if
*zif
;
344 struct in_addr old_vtep_ip
;
345 uint16_t chgflags
= 0;
351 memcpy(&zif
->l2info
.vxl
, vxlan_info
, sizeof(*vxlan_info
));
352 zebra_evpn_vl_vxl_ref(zif
->l2info
.vxl
.access_vlan
, zif
);
353 zebra_vxlan_if_add(ifp
);
357 old_vtep_ip
= zif
->l2info
.vxl
.vtep_ip
;
359 if (!IPV4_ADDR_SAME(&old_vtep_ip
, &vxlan_info
->vtep_ip
)) {
360 chgflags
|= ZEBRA_VXLIF_LOCAL_IP_CHANGE
;
361 zif
->l2info
.vxl
.vtep_ip
= vxlan_info
->vtep_ip
;
364 if (!IPV4_ADDR_SAME(&zif
->l2info
.vxl
.mcast_grp
,
365 &vxlan_info
->mcast_grp
)) {
366 chgflags
|= ZEBRA_VXLIF_MCAST_GRP_CHANGE
;
367 zif
->l2info
.vxl
.mcast_grp
= vxlan_info
->mcast_grp
;
371 zebra_vxlan_if_update(ifp
, chgflags
);
375 * Handle change to VLAN to VNI mapping.
377 void zebra_l2_vxlanif_update_access_vlan(struct interface
*ifp
,
378 vlanid_t access_vlan
)
380 struct zebra_if
*zif
;
381 vlanid_t old_access_vlan
;
386 old_access_vlan
= zif
->l2info
.vxl
.access_vlan
;
387 if (old_access_vlan
== access_vlan
)
390 zif
->l2info
.vxl
.access_vlan
= access_vlan
;
392 zebra_evpn_vl_vxl_deref(old_access_vlan
, zif
);
393 zebra_evpn_vl_vxl_ref(zif
->l2info
.vxl
.access_vlan
, zif
);
394 zebra_vxlan_if_update(ifp
, ZEBRA_VXLIF_VLAN_CHANGE
);
398 * Handle VxLAN interface delete.
400 void zebra_l2_vxlanif_del(struct interface
*ifp
)
402 struct zebra_if
*zif
;
407 zebra_evpn_vl_vxl_deref(zif
->l2info
.vxl
.access_vlan
, zif
);
408 zebra_vxlan_if_del(ifp
);
412 * Map or unmap interface from bridge.
413 * NOTE: It is currently assumped that an interface has to be unmapped
414 * from a bridge before it can be mapped to another bridge.
416 void zebra_l2if_update_bridge_slave(struct interface
*ifp
,
417 ifindex_t bridge_ifindex
, ns_id_t ns_id
,
420 struct zebra_if
*zif
;
421 ifindex_t old_bridge_ifindex
;
423 struct zebra_vrf
*zvrf
;
428 zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
432 if (zif
->zif_type
== ZEBRA_IF_VXLAN
433 && chgflags
!= ZEBRA_BRIDGE_NO_ACTION
) {
434 if (ZEBRA_BRIDGE_MASTER_MAC_CHANGE
)
435 zebra_vxlan_if_update(ifp
,
436 ZEBRA_VXLIF_MASTER_MAC_CHANGE
);
437 if (ZEBRA_BRIDGE_MASTER_UP
)
438 zebra_vxlan_if_update(ifp
, ZEBRA_VXLIF_MASTER_CHANGE
);
440 old_bridge_ifindex
= zif
->brslave_info
.bridge_ifindex
;
441 old_ns_id
= zif
->brslave_info
.ns_id
;
442 if (old_bridge_ifindex
== bridge_ifindex
&&
443 old_ns_id
== zif
->brslave_info
.ns_id
)
446 zif
->brslave_info
.ns_id
= ns_id
;
447 zif
->brslave_info
.bridge_ifindex
= bridge_ifindex
;
448 /* Set up or remove link with master */
449 if (bridge_ifindex
!= IFINDEX_INTERNAL
) {
450 zebra_l2_map_slave_to_bridge(&zif
->brslave_info
, zvrf
->zns
);
451 /* In the case of VxLAN, invoke the handler for EVPN. */
452 if (zif
->zif_type
== ZEBRA_IF_VXLAN
)
453 zebra_vxlan_if_update(ifp
, ZEBRA_VXLIF_MASTER_CHANGE
);
455 zebra_evpn_es_local_br_port_update(zif
);
456 } else if (old_bridge_ifindex
!= IFINDEX_INTERNAL
) {
458 * In the case of VxLAN, invoke the handler for EVPN.
459 * Note that this should be done *prior*
460 * to unmapping the interface from the bridge.
462 if (zif
->zif_type
== ZEBRA_IF_VXLAN
)
463 zebra_vxlan_if_update(ifp
, ZEBRA_VXLIF_MASTER_CHANGE
);
465 zebra_evpn_es_local_br_port_update(zif
);
466 zebra_l2_unmap_slave_from_bridge(&zif
->brslave_info
);
470 void zebra_l2if_update_bond_slave(struct interface
*ifp
, ifindex_t bond_ifindex
,
473 struct zebra_if
*zif
;
474 ifindex_t old_bond_ifindex
;
476 struct zebra_l2info_bondslave
*bond_mbr
;
481 old_bypass
= !!(zif
->flags
& ZIF_FLAG_LACP_BYPASS
);
482 if (old_bypass
!= new_bypass
) {
483 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
484 zlog_debug("bond-mbr %s lacp bypass changed to %s",
485 zif
->ifp
->name
, new_bypass
? "on" : "off");
488 zif
->flags
|= ZIF_FLAG_LACP_BYPASS
;
490 zif
->flags
&= ~ZIF_FLAG_LACP_BYPASS
;
492 bond_mbr
= &zif
->bondslave_info
;
493 if (bond_mbr
->bond_if
) {
494 struct zebra_if
*bond_zif
= bond_mbr
->bond_if
->info
;
496 zebra_l2_bond_lacp_bypass_eval(bond_zif
);
500 old_bond_ifindex
= zif
->bondslave_info
.bond_ifindex
;
501 if (old_bond_ifindex
== bond_ifindex
)
504 zif
->bondslave_info
.bond_ifindex
= bond_ifindex
;
506 /* Set up or remove link with master */
507 if (bond_ifindex
!= IFINDEX_INTERNAL
)
508 zebra_l2_map_slave_to_bond(zif
, ifp
->vrf_id
);
509 else if (old_bond_ifindex
!= IFINDEX_INTERNAL
)
510 zebra_l2_unmap_slave_from_bond(zif
);
513 void zebra_vlan_bitmap_compute(struct interface
*ifp
,
514 uint32_t vid_start
, uint16_t vid_end
)
517 struct zebra_if
*zif
;
519 zif
= (struct zebra_if
*)ifp
->info
;
522 for (vid
= vid_start
; vid
<= vid_end
; ++vid
)
523 bf_set_bit(zif
->vlan_bitmap
, vid
);
526 void zebra_vlan_mbr_re_eval(struct interface
*ifp
, bitfield_t old_vlan_bitmap
)
529 struct zebra_if
*zif
;
531 zif
= (struct zebra_if
*)ifp
->info
;
534 if (!bf_cmp(zif
->vlan_bitmap
, old_vlan_bitmap
))
538 bf_for_each_set_bit(zif
->vlan_bitmap
, vid
, IF_VLAN_BITMAP_MAX
) {
539 /* if not already set create new reference */
540 if (!bf_test_index(old_vlan_bitmap
, vid
))
541 zebra_evpn_vl_mbr_ref(vid
, zif
);
543 /* also clear from the old vlan bitmap */
544 bf_release_index(old_vlan_bitmap
, vid
);
547 /* any bits remaining in the old vlan bitmap are stale references */
548 bf_for_each_set_bit(old_vlan_bitmap
, vid
, IF_VLAN_BITMAP_MAX
) {
549 zebra_evpn_vl_mbr_deref(vid
, zif
);