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
)
56 struct interface
*ifp
;
57 struct zebra_vrf
*zvrf
;
60 zvrf
= zebra_vrf_lookup_by_id(br_if
->vrf_id
);
64 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
65 FOR_ALL_INTERFACES (vrf
, ifp
) {
67 struct zebra_l2info_brslave
*br_slave
;
69 if (ifp
->ifindex
== IFINDEX_INTERNAL
|| !ifp
->info
)
71 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp
))
74 /* NOTE: This assumes 'zebra_l2info_brslave' is the
76 * for any L2 interface.
78 zif
= (struct zebra_if
*)ifp
->info
;
79 br_slave
= &zif
->brslave_info
;
82 if (br_slave
->bridge_ifindex
== br_if
->ifindex
&&
83 br_slave
->ns_id
== zns
->ns_id
)
84 br_slave
->br_if
= br_if
;
86 if (br_slave
->br_if
== br_if
)
87 br_slave
->br_if
= NULL
;
93 /* Public functions */
94 void zebra_l2_map_slave_to_bridge(struct zebra_l2info_brslave
*br_slave
,
97 struct interface
*br_if
;
99 /* TODO: Handle change of master */
101 br_if
= if_lookup_by_index_per_ns(zebra_ns_lookup(zns
->ns_id
),
102 br_slave
->bridge_ifindex
);
104 br_slave
->br_if
= br_if
;
107 void zebra_l2_unmap_slave_from_bridge(struct zebra_l2info_brslave
*br_slave
)
109 br_slave
->br_if
= NULL
;
112 /* If any of the bond members are in bypass state the bond is placed
115 static void zebra_l2_bond_lacp_bypass_eval(struct zebra_if
*bond_zif
)
117 struct listnode
*node
;
118 struct zebra_if
*bond_mbr
;
119 bool old_bypass
= !!(bond_zif
->flags
& ZIF_FLAG_LACP_BYPASS
);
120 bool new_bypass
= false;
122 if (bond_zif
->bond_info
.mbr_zifs
) {
123 for (ALL_LIST_ELEMENTS_RO(bond_zif
->bond_info
.mbr_zifs
, node
,
125 if (bond_mbr
->flags
& ZIF_FLAG_LACP_BYPASS
) {
132 if (old_bypass
== new_bypass
)
135 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
136 zlog_debug("bond %s lacp bypass changed to %s",
137 bond_zif
->ifp
->name
, new_bypass
? "on" : "off");
140 bond_zif
->flags
|= ZIF_FLAG_LACP_BYPASS
;
142 bond_zif
->flags
&= ~ZIF_FLAG_LACP_BYPASS
;
144 if (bond_zif
->es_info
.es
)
145 zebra_evpn_es_bypass_update(bond_zif
->es_info
.es
, bond_zif
->ifp
,
149 /* Returns true if member was newly linked to bond */
150 void zebra_l2_map_slave_to_bond(struct zebra_if
*zif
, vrf_id_t vrf_id
)
152 struct interface
*bond_if
;
153 struct zebra_if
*bond_zif
;
154 struct zebra_l2info_bondslave
*bond_slave
= &zif
->bondslave_info
;
156 bond_if
= if_lookup_by_index_all_vrf(bond_slave
->bond_ifindex
);
157 if (bond_if
== bond_slave
->bond_if
)
160 /* unlink the slave from the old master */
161 zebra_l2_unmap_slave_from_bond(zif
);
163 /* If the bond is present and ready link the bond-member
166 if (bond_if
&& (bond_zif
= bond_if
->info
)) {
167 if (bond_zif
->bond_info
.mbr_zifs
) {
168 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
169 zlog_debug("bond mbr %s linked to %s",
170 zif
->ifp
->name
, bond_if
->name
);
171 bond_slave
->bond_if
= bond_if
;
172 /* link the slave to the new bond master */
173 listnode_add(bond_zif
->bond_info
.mbr_zifs
, zif
);
174 /* inherit protodown flags from the es-bond */
175 if (zebra_evpn_is_es_bond(bond_if
))
176 zebra_evpn_mh_update_protodown_bond_mbr(
177 zif
, false /*clear*/, __func__
);
178 zebra_l2_bond_lacp_bypass_eval(bond_zif
);
181 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
182 zlog_debug("bond mbr %s link to bond skipped",
187 void zebra_l2_unmap_slave_from_bond(struct zebra_if
*zif
)
189 struct zebra_l2info_bondslave
*bond_slave
= &zif
->bondslave_info
;
190 struct zebra_if
*bond_zif
;
192 if (!bond_slave
->bond_if
) {
193 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
194 zlog_debug("bond mbr %s unlink from bond skipped",
199 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
200 zlog_debug("bond mbr %s un-linked from %s", zif
->ifp
->name
,
201 bond_slave
->bond_if
->name
);
203 /* unlink the slave from the bond master */
204 bond_zif
= bond_slave
->bond_if
->info
;
205 /* clear protodown flags */
206 if (zebra_evpn_is_es_bond(bond_zif
->ifp
))
207 zebra_evpn_mh_update_protodown_bond_mbr(zif
, true /*clear*/,
209 listnode_delete(bond_zif
->bond_info
.mbr_zifs
, zif
);
210 bond_slave
->bond_if
= NULL
;
211 zebra_l2_bond_lacp_bypass_eval(bond_zif
);
214 void zebra_l2if_update_bond(struct interface
*ifp
, bool add
)
216 struct zebra_if
*zif
;
217 struct zebra_l2info_bond
*bond
;
221 bond
= &zif
->bond_info
;
224 if (!bond
->mbr_zifs
) {
225 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
226 zlog_debug("bond %s mbr list create",
228 bond
->mbr_zifs
= list_new();
231 struct listnode
*node
;
232 struct listnode
*nnode
;
233 struct zebra_if
*bond_mbr
;
238 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
239 zlog_debug("bond %s mbr list delete", ifp
->name
);
240 for (ALL_LIST_ELEMENTS(bond
->mbr_zifs
, node
, nnode
, bond_mbr
))
241 zebra_l2_unmap_slave_from_bond(bond_mbr
);
243 list_delete(&bond
->mbr_zifs
);
248 * Handle Bridge interface add or update. Update relevant info,
249 * map slaves (if any) to the bridge.
251 void zebra_l2_bridge_add_update(struct interface
*ifp
,
252 struct zebra_l2info_bridge
*bridge_info
,
255 struct zebra_if
*zif
;
260 /* Copy over the L2 information. */
261 memcpy(&zif
->l2info
.br
, bridge_info
, sizeof(*bridge_info
));
263 /* Link all slaves to this bridge */
264 map_slaves_to_bridge(ifp
, 1);
268 * Handle Bridge interface delete.
270 void zebra_l2_bridge_del(struct interface
*ifp
)
272 /* Unlink all slaves to this bridge */
273 map_slaves_to_bridge(ifp
, 0);
277 * Update L2 info for a VLAN interface. Only relevant parameter is the
278 * VLAN Id and this cannot change.
280 void zebra_l2_vlanif_update(struct interface
*ifp
,
281 struct zebra_l2info_vlan
*vlan_info
)
283 struct zebra_if
*zif
;
288 /* Copy over the L2 information. */
289 memcpy(&zif
->l2info
.vl
, vlan_info
, sizeof(*vlan_info
));
293 * Update L2 info for a GRE interface. This is called upon interface
294 * addition as well as update. Upon add/update, need to inform
295 * clients about GRE information.
297 void zebra_l2_greif_add_update(struct interface
*ifp
,
298 struct zebra_l2info_gre
*gre_info
, int add
)
300 struct zebra_if
*zif
;
301 struct in_addr old_vtep_ip
;
307 memcpy(&zif
->l2info
.gre
, gre_info
, sizeof(*gre_info
));
311 old_vtep_ip
= zif
->l2info
.gre
.vtep_ip
;
312 if (IPV4_ADDR_SAME(&old_vtep_ip
, &gre_info
->vtep_ip
))
315 zif
->l2info
.gre
.vtep_ip
= gre_info
->vtep_ip
;
319 * Update L2 info for a VxLAN interface. This is called upon interface
320 * addition as well as update. Upon add, need to invoke the VNI create
321 * function. Upon update, the params of interest are the local tunnel
322 * IP and VLAN mapping, but the latter is handled separately.
324 void zebra_l2_vxlanif_add_update(struct interface
*ifp
,
325 struct zebra_l2info_vxlan
*vxlan_info
, int add
)
327 struct zebra_if
*zif
;
328 struct in_addr old_vtep_ip
;
329 uint16_t chgflags
= 0;
335 memcpy(&zif
->l2info
.vxl
, vxlan_info
, sizeof(*vxlan_info
));
336 zebra_evpn_vl_vxl_ref(zif
->l2info
.vxl
.access_vlan
, zif
);
337 zebra_vxlan_if_add(ifp
);
341 old_vtep_ip
= zif
->l2info
.vxl
.vtep_ip
;
343 if (!IPV4_ADDR_SAME(&old_vtep_ip
, &vxlan_info
->vtep_ip
)) {
344 chgflags
|= ZEBRA_VXLIF_LOCAL_IP_CHANGE
;
345 zif
->l2info
.vxl
.vtep_ip
= vxlan_info
->vtep_ip
;
348 if (!IPV4_ADDR_SAME(&zif
->l2info
.vxl
.mcast_grp
,
349 &vxlan_info
->mcast_grp
)) {
350 chgflags
|= ZEBRA_VXLIF_MCAST_GRP_CHANGE
;
351 zif
->l2info
.vxl
.mcast_grp
= vxlan_info
->mcast_grp
;
355 zebra_vxlan_if_update(ifp
, chgflags
);
359 * Handle change to VLAN to VNI mapping.
361 void zebra_l2_vxlanif_update_access_vlan(struct interface
*ifp
,
362 vlanid_t access_vlan
)
364 struct zebra_if
*zif
;
365 vlanid_t old_access_vlan
;
370 old_access_vlan
= zif
->l2info
.vxl
.access_vlan
;
371 if (old_access_vlan
== access_vlan
)
374 zif
->l2info
.vxl
.access_vlan
= access_vlan
;
376 zebra_evpn_vl_vxl_deref(old_access_vlan
, zif
);
377 zebra_evpn_vl_vxl_ref(zif
->l2info
.vxl
.access_vlan
, zif
);
378 zebra_vxlan_if_update(ifp
, ZEBRA_VXLIF_VLAN_CHANGE
);
382 * Handle VxLAN interface delete.
384 void zebra_l2_vxlanif_del(struct interface
*ifp
)
386 struct zebra_if
*zif
;
391 zebra_evpn_vl_vxl_deref(zif
->l2info
.vxl
.access_vlan
, zif
);
392 zebra_vxlan_if_del(ifp
);
396 * Map or unmap interface from bridge.
397 * NOTE: It is currently assumped that an interface has to be unmapped
398 * from a bridge before it can be mapped to another bridge.
400 void zebra_l2if_update_bridge_slave(struct interface
*ifp
,
401 ifindex_t bridge_ifindex
,
404 struct zebra_if
*zif
;
405 ifindex_t old_bridge_ifindex
;
407 struct zebra_vrf
*zvrf
;
412 zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
416 old_bridge_ifindex
= zif
->brslave_info
.bridge_ifindex
;
417 old_ns_id
= zif
->brslave_info
.ns_id
;
418 if (old_bridge_ifindex
== bridge_ifindex
&&
419 old_ns_id
== zif
->brslave_info
.ns_id
)
422 zif
->brslave_info
.ns_id
= ns_id
;
423 zif
->brslave_info
.bridge_ifindex
= bridge_ifindex
;
424 /* Set up or remove link with master */
425 if (bridge_ifindex
!= IFINDEX_INTERNAL
) {
426 zebra_l2_map_slave_to_bridge(&zif
->brslave_info
, zvrf
->zns
);
427 /* In the case of VxLAN, invoke the handler for EVPN. */
428 if (zif
->zif_type
== ZEBRA_IF_VXLAN
)
429 zebra_vxlan_if_update(ifp
, ZEBRA_VXLIF_MASTER_CHANGE
);
431 zebra_evpn_es_local_br_port_update(zif
);
432 } else if (old_bridge_ifindex
!= IFINDEX_INTERNAL
) {
434 * In the case of VxLAN, invoke the handler for EVPN.
435 * Note that this should be done *prior*
436 * to unmapping the interface from the bridge.
438 if (zif
->zif_type
== ZEBRA_IF_VXLAN
)
439 zebra_vxlan_if_update(ifp
, ZEBRA_VXLIF_MASTER_CHANGE
);
441 zebra_evpn_es_local_br_port_update(zif
);
442 zebra_l2_unmap_slave_from_bridge(&zif
->brslave_info
);
446 void zebra_l2if_update_bond_slave(struct interface
*ifp
, ifindex_t bond_ifindex
,
449 struct zebra_if
*zif
;
450 ifindex_t old_bond_ifindex
;
452 struct zebra_l2info_bondslave
*bond_mbr
;
457 old_bypass
= !!(zif
->flags
& ZIF_FLAG_LACP_BYPASS
);
458 if (old_bypass
!= new_bypass
) {
459 if (IS_ZEBRA_DEBUG_EVPN_MH_ES
|| IS_ZEBRA_DEBUG_EVENT
)
460 zlog_debug("bond-mbr %s lacp bypass changed to %s",
461 zif
->ifp
->name
, new_bypass
? "on" : "off");
464 zif
->flags
|= ZIF_FLAG_LACP_BYPASS
;
466 zif
->flags
&= ~ZIF_FLAG_LACP_BYPASS
;
468 bond_mbr
= &zif
->bondslave_info
;
469 if (bond_mbr
->bond_if
) {
470 struct zebra_if
*bond_zif
= bond_mbr
->bond_if
->info
;
472 zebra_l2_bond_lacp_bypass_eval(bond_zif
);
476 old_bond_ifindex
= zif
->bondslave_info
.bond_ifindex
;
477 if (old_bond_ifindex
== bond_ifindex
)
480 zif
->bondslave_info
.bond_ifindex
= bond_ifindex
;
482 /* Set up or remove link with master */
483 if (bond_ifindex
!= IFINDEX_INTERNAL
)
484 zebra_l2_map_slave_to_bond(zif
, ifp
->vrf_id
);
485 else if (old_bond_ifindex
!= IFINDEX_INTERNAL
)
486 zebra_l2_unmap_slave_from_bond(zif
);
489 void zebra_vlan_bitmap_compute(struct interface
*ifp
,
490 uint32_t vid_start
, uint16_t vid_end
)
493 struct zebra_if
*zif
;
495 zif
= (struct zebra_if
*)ifp
->info
;
498 for (vid
= vid_start
; vid
<= vid_end
; ++vid
)
499 bf_set_bit(zif
->vlan_bitmap
, vid
);
502 void zebra_vlan_mbr_re_eval(struct interface
*ifp
, bitfield_t old_vlan_bitmap
)
505 struct zebra_if
*zif
;
507 zif
= (struct zebra_if
*)ifp
->info
;
510 if (!bf_cmp(zif
->vlan_bitmap
, old_vlan_bitmap
))
514 bf_for_each_set_bit(zif
->vlan_bitmap
, vid
, IF_VLAN_BITMAP_MAX
) {
515 /* if not already set create new reference */
516 if (!bf_test_index(old_vlan_bitmap
, vid
))
517 zebra_evpn_vl_mbr_ref(vid
, zif
);
519 /* also clear from the old vlan bitmap */
520 bf_release_index(old_vlan_bitmap
, vid
);
523 /* any bits remaining in the old vlan bitmap are stale references */
524 bf_for_each_set_bit(old_vlan_bitmap
, vid
, IF_VLAN_BITMAP_MAX
) {
525 zebra_evpn_vl_mbr_deref(vid
, zif
);