]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_l2.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / zebra / zebra_l2.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Zebra Layer-2 interface handling code
4 * Copyright (C) 2016, 2017 Cumulus Networks, Inc.
5 */
6
7 #include <zebra.h>
8
9 #include "if.h"
10 #include "prefix.h"
11 #include "table.h"
12 #include "memory.h"
13 #include "log.h"
14 #include "linklist.h"
15 #include "stream.h"
16 #include "hash.h"
17 #include "jhash.h"
18
19 #include "zebra/rib.h"
20 #include "zebra/rt.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"
33
34 /* definitions */
35
36 /* static function declarations */
37
38 /* Private functions */
39 static void map_slaves_to_bridge(struct interface *br_if, int link,
40 bool update_slave, uint8_t chgflags)
41 {
42 struct vrf *vrf;
43 struct interface *ifp;
44 struct zebra_vrf *zvrf;
45 struct zebra_ns *zns;
46
47 zvrf = br_if->vrf->info;
48 assert(zvrf);
49 zns = zvrf->zns;
50 assert(zns);
51 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
52 FOR_ALL_INTERFACES (vrf, ifp) {
53 struct zebra_if *zif;
54 struct zebra_l2info_brslave *br_slave;
55
56 if (ifp->ifindex == IFINDEX_INTERNAL || !ifp->info)
57 continue;
58 if (!IS_ZEBRA_IF_BRIDGE_SLAVE(ifp))
59 continue;
60
61 /* NOTE: This assumes 'zebra_l2info_brslave' is the
62 * first field
63 * for any L2 interface.
64 */
65 zif = (struct zebra_if *)ifp->info;
66 br_slave = &zif->brslave_info;
67
68 if (link) {
69 if (br_slave->bridge_ifindex == br_if->ifindex
70 && br_slave->ns_id == zns->ns_id) {
71 br_slave->br_if = br_if;
72 if (update_slave) {
73 zebra_l2if_update_bridge_slave(
74 ifp,
75 br_slave->bridge_ifindex,
76 br_slave->ns_id,
77 chgflags);
78 }
79 }
80 } else {
81 if (br_slave->br_if == br_if)
82 br_slave->br_if = NULL;
83 }
84 }
85 }
86 }
87
88 /* Public functions */
89 void zebra_l2_map_slave_to_bridge(struct zebra_l2info_brslave *br_slave,
90 struct zebra_ns *zns)
91 {
92 struct interface *br_if;
93
94 /* TODO: Handle change of master */
95 assert(zns);
96 br_if = if_lookup_by_index_per_ns(zebra_ns_lookup(zns->ns_id),
97 br_slave->bridge_ifindex);
98 if (br_if)
99 br_slave->br_if = br_if;
100 }
101
102 void zebra_l2_unmap_slave_from_bridge(struct zebra_l2info_brslave *br_slave)
103 {
104 br_slave->br_if = NULL;
105 }
106
107 /* If any of the bond members are in bypass state the bond is placed
108 * in bypass state
109 */
110 static void zebra_l2_bond_lacp_bypass_eval(struct zebra_if *bond_zif)
111 {
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;
116
117 if (bond_zif->bond_info.mbr_zifs) {
118 for (ALL_LIST_ELEMENTS_RO(bond_zif->bond_info.mbr_zifs, node,
119 bond_mbr)) {
120 if (bond_mbr->flags & ZIF_FLAG_LACP_BYPASS) {
121 new_bypass = true;
122 break;
123 }
124 }
125 }
126
127 if (old_bypass == new_bypass)
128 return;
129
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");
133
134 if (new_bypass)
135 bond_zif->flags |= ZIF_FLAG_LACP_BYPASS;
136 else
137 bond_zif->flags &= ~ZIF_FLAG_LACP_BYPASS;
138
139 if (bond_zif->es_info.es)
140 zebra_evpn_es_bypass_update(bond_zif->es_info.es, bond_zif->ifp,
141 new_bypass);
142 }
143
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)
146 {
147 struct interface *bond_if;
148 struct zebra_if *bond_zif;
149 struct zebra_l2info_bondslave *bond_slave = &zif->bondslave_info;
150
151 bond_if = if_lookup_by_index(bond_slave->bond_ifindex, vrf_id);
152 if (bond_if == bond_slave->bond_if)
153 return;
154
155 /* unlink the slave from the old master */
156 zebra_l2_unmap_slave_from_bond(zif);
157
158 /* If the bond is present and ready link the bond-member
159 * to it
160 */
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);
174 }
175 } else {
176 if (IS_ZEBRA_DEBUG_EVPN_MH_ES || IS_ZEBRA_DEBUG_EVENT)
177 zlog_debug("bond mbr %s link to bond skipped",
178 zif->ifp->name);
179 }
180 }
181
182 void zebra_l2_unmap_slave_from_bond(struct zebra_if *zif)
183 {
184 struct zebra_l2info_bondslave *bond_slave = &zif->bondslave_info;
185 struct zebra_if *bond_zif;
186
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",
190 zif->ifp->name);
191 return;
192 }
193
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);
197
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*/,
203 __func__);
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);
207 }
208
209 void zebra_l2if_update_bond(struct interface *ifp, bool add)
210 {
211 struct zebra_if *zif;
212 struct zebra_l2info_bond *bond;
213
214 zif = ifp->info;
215 assert(zif);
216 bond = &zif->bond_info;
217
218 if (add) {
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",
222 ifp->name);
223 bond->mbr_zifs = list_new();
224 }
225 } else {
226 struct listnode *node;
227 struct listnode *nnode;
228 struct zebra_if *bond_mbr;
229
230 if (!bond->mbr_zifs)
231 return;
232
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);
237
238 list_delete(&bond->mbr_zifs);
239 }
240 }
241
242 /*
243 * Handle Bridge interface add or update. Update relevant info,
244 * map slaves (if any) to the bridge.
245 */
246 void zebra_l2_bridge_add_update(struct interface *ifp,
247 struct zebra_l2info_bridge *bridge_info,
248 int add)
249 {
250 struct zebra_if *zif;
251 struct zebra_l2_bridge_if *br;
252
253 zif = ifp->info;
254 assert(zif);
255
256 br = BRIDGE_FROM_ZEBRA_IF(zif);
257 br->vlan_aware = bridge_info->bridge.vlan_aware;
258 zebra_l2_bridge_if_add(ifp);
259
260 /* Link all slaves to this bridge */
261 map_slaves_to_bridge(ifp, 1, false, ZEBRA_BRIDGE_NO_ACTION);
262 }
263
264 /*
265 * Handle Bridge interface delete.
266 */
267 void zebra_l2_bridge_del(struct interface *ifp)
268 {
269 zebra_l2_bridge_if_del(ifp);
270
271 /* Unlink all slaves to this bridge */
272 map_slaves_to_bridge(ifp, 0, false, ZEBRA_BRIDGE_NO_ACTION);
273 }
274
275 void zebra_l2if_update_bridge(struct interface *ifp, uint8_t chgflags)
276 {
277 if (!chgflags)
278 return;
279 map_slaves_to_bridge(ifp, 1, true, chgflags);
280 }
281
282 /*
283 * Update L2 info for a VLAN interface. Only relevant parameter is the
284 * VLAN Id and this cannot change.
285 */
286 void zebra_l2_vlanif_update(struct interface *ifp,
287 struct zebra_l2info_vlan *vlan_info)
288 {
289 struct zebra_if *zif;
290
291 zif = ifp->info;
292 assert(zif);
293
294 /* Copy over the L2 information. */
295 memcpy(&zif->l2info.vl, vlan_info, sizeof(*vlan_info));
296 }
297
298 /*
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.
302 */
303 void zebra_l2_greif_add_update(struct interface *ifp,
304 struct zebra_l2info_gre *gre_info, int add)
305 {
306 struct zebra_if *zif;
307 struct in_addr old_vtep_ip;
308
309 zif = ifp->info;
310 assert(zif);
311
312 if (add) {
313 memcpy(&zif->l2info.gre, gre_info, sizeof(*gre_info));
314 return;
315 }
316
317 old_vtep_ip = zif->l2info.gre.vtep_ip;
318 if (IPV4_ADDR_SAME(&old_vtep_ip, &gre_info->vtep_ip))
319 return;
320
321 zif->l2info.gre.vtep_ip = gre_info->vtep_ip;
322 }
323
324 /*
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.
329 */
330 void zebra_l2_vxlanif_add_update(struct interface *ifp,
331 struct zebra_l2info_vxlan *vxlan_info, int add)
332 {
333 struct zebra_if *zif;
334 uint16_t chgflags = 0;
335 struct zebra_vxlan_if_update_ctx ctx;
336
337 zif = ifp->info;
338 assert(zif);
339
340 if (add) {
341 memcpy(&zif->l2info.vxl, vxlan_info, sizeof(*vxlan_info));
342 zebra_vxlan_if_add(ifp);
343 return;
344 }
345
346 memset(&ctx, 0, sizeof(ctx));
347 ctx.old_vtep_ip = zif->l2info.vxl.vtep_ip;
348
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;
352 }
353
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;
361 }
362 }
363
364 if (chgflags) {
365 ctx.chgflags = chgflags;
366 zebra_vxlan_if_update(ifp, &ctx);
367 }
368 }
369
370 /*
371 * Handle change to VLAN to VNI mapping.
372 */
373 void zebra_l2_vxlanif_update_access_vlan(struct interface *ifp,
374 vlanid_t access_vlan)
375 {
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;
380
381
382 zif = ifp->info;
383 assert(zif);
384
385 /* This would be called only in non svd case */
386 assert(IS_ZEBRA_VXLAN_IF_VNI(zif));
387
388 old_access_vlan = zif->l2info.vxl.vni_info.vni.access_vlan;
389 ;
390 if (old_access_vlan == access_vlan)
391 return;
392
393 memset(&ctx, 0, sizeof(ctx));
394 vni = zebra_vxlan_if_vni_find(zif, 0);
395 ctx.old_vni = *vni;
396 ctx.chgflags = ZEBRA_VXLIF_VLAN_CHANGE;
397 vni->access_vlan = access_vlan;
398
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);
402 }
403
404 /*
405 * Handle VxLAN interface delete.
406 */
407 void zebra_l2_vxlanif_del(struct interface *ifp)
408 {
409 struct zebra_if *zif;
410
411 zif = ifp->info;
412 assert(zif);
413
414 zebra_vxlan_if_del(ifp);
415 }
416
417 /*
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.
421 */
422 void zebra_l2if_update_bridge_slave(struct interface *ifp,
423 ifindex_t bridge_ifindex, ns_id_t ns_id,
424 uint8_t chgflags)
425 {
426 struct zebra_if *zif;
427 ifindex_t old_bridge_ifindex;
428 ns_id_t old_ns_id;
429 struct zebra_vrf *zvrf;
430 struct zebra_vxlan_if_update_ctx ctx;
431
432 memset(&ctx, 0, sizeof(ctx));
433
434 zif = ifp->info;
435 assert(zif);
436
437 zvrf = ifp->vrf->info;
438 if (!zvrf)
439 return;
440
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);
446 }
447 if (chgflags & ZEBRA_BRIDGE_MASTER_UP) {
448 ctx.chgflags = ZEBRA_VXLIF_MASTER_CHANGE;
449 zebra_vxlan_if_update(ifp, &ctx);
450 }
451 }
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)
456 return;
457
458 ctx.chgflags = ZEBRA_VXLIF_MASTER_CHANGE;
459
460
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);
469 if (zif->es_info.es)
470 zebra_evpn_es_local_br_port_update(zif);
471 } else if (old_bridge_ifindex != IFINDEX_INTERNAL) {
472 /*
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.
476 */
477 if (zif->zif_type == ZEBRA_IF_VXLAN)
478 zebra_vxlan_if_update(ifp, &ctx);
479 if (zif->es_info.es)
480 zebra_evpn_es_local_br_port_update(zif);
481 zebra_l2_unmap_slave_from_bridge(&zif->brslave_info);
482 }
483 }
484
485 void zebra_l2if_update_bond_slave(struct interface *ifp, ifindex_t bond_ifindex,
486 bool new_bypass)
487 {
488 struct zebra_if *zif;
489 ifindex_t old_bond_ifindex;
490 bool old_bypass;
491 struct zebra_l2info_bondslave *bond_mbr;
492
493 zif = ifp->info;
494 assert(zif);
495
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");
501
502 if (new_bypass)
503 zif->flags |= ZIF_FLAG_LACP_BYPASS;
504 else
505 zif->flags &= ~ZIF_FLAG_LACP_BYPASS;
506
507 bond_mbr = &zif->bondslave_info;
508 if (bond_mbr->bond_if) {
509 struct zebra_if *bond_zif = bond_mbr->bond_if->info;
510
511 zebra_l2_bond_lacp_bypass_eval(bond_zif);
512 }
513 }
514
515 old_bond_ifindex = zif->bondslave_info.bond_ifindex;
516 if (old_bond_ifindex == bond_ifindex)
517 return;
518
519 zif->bondslave_info.bond_ifindex = bond_ifindex;
520
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);
526 }
527
528 void zebra_vlan_bitmap_compute(struct interface *ifp,
529 uint32_t vid_start, uint16_t vid_end)
530 {
531 uint32_t vid;
532 struct zebra_if *zif;
533
534 zif = (struct zebra_if *)ifp->info;
535 assert(zif);
536
537 for (vid = vid_start; vid <= vid_end; ++vid)
538 bf_set_bit(zif->vlan_bitmap, vid);
539 }
540
541 void zebra_vlan_mbr_re_eval(struct interface *ifp, bitfield_t old_vlan_bitmap)
542 {
543 uint32_t vid;
544 struct zebra_if *zif;
545
546 zif = (struct zebra_if *)ifp->info;
547 assert(zif);
548
549 if (!bf_cmp(zif->vlan_bitmap, old_vlan_bitmap))
550 /* no change */
551 return;
552
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);
557
558 /* also clear from the old vlan bitmap */
559 bf_release_index(old_vlan_bitmap, vid);
560 }
561
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);
565 }
566 }