From 220c31563f61b0895b994d7cb26566a06b03e22a Mon Sep 17 00:00:00 2001 From: Boris Glimcher Date: Thu, 10 Aug 2023 01:46:21 +0300 Subject: [PATCH] fix(vrf): create bridge and vxlan only if VNI value is not empty Fixes #105 Signed-off-by: Boris Glimcher --- pkg/evpn/vrf.go | 87 ++++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/pkg/evpn/vrf.go b/pkg/evpn/vrf.go index aacf25f5..a2884b35 100644 --- a/pkg/evpn/vrf.go +++ b/pkg/evpn/vrf.go @@ -81,52 +81,57 @@ func (s *Server) CreateVrf(_ context.Context, in *pb.CreateVrfRequest) (*pb.Vrf, // TODO: Add low-prio default route. Otherwise a miss leads to lookup in the next higher table // Example: ip route add throw default table proto evpn-gw-br metric 9999 - // Example: ip link add br100 type bridge - bridgeName := fmt.Sprintf("br%d", in.Vrf.Spec.Vni) - bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: bridgeName}} - if err := netlink.LinkAdd(bridge); err != nil { - fmt.Printf("Failed to create Bridge link: %v", err) - return nil, err - } - // Example: ip link set br100 master blue addrgenmode none - if err := netlink.LinkSetMaster(bridge, vrf); err != nil { - fmt.Printf("Failed to add Bridge to VRF: %v", err) - return nil, err - } - // Example: ip link set br100 addr aa:bb:cc:00:00:02 + // generate random mac, since it is not part of user facing API mac, err := generateRandMAC() if err != nil { fmt.Printf("Failed to generate random MAC: %v", err) return nil, err } - if err := netlink.LinkSetHardwareAddr(bridge, mac); err != nil { - fmt.Printf("Failed to set MAC on Bridge link: %v", err) - return nil, err - } - // Example: ip link set br100 up - if err := netlink.LinkSetUp(bridge); err != nil { - fmt.Printf("Failed to up Bridge link: %v", err) - return nil, err - } - // Example: ip link add vni100 type vxlan local 10.0.0.4 dstport 4789 id 100 nolearning - vxlanName := fmt.Sprintf("vni%d", in.Vrf.Spec.Vni) - myip := make(net.IP, 4) - binary.BigEndian.PutUint32(myip, in.Vrf.Spec.VtepIpPrefix.Addr.GetV4Addr()) - // TODO: take Port from proto instead of hard-coded - vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(in.Vrf.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip} - if err := netlink.LinkAdd(vxlan); err != nil { - fmt.Printf("Failed to create Vxlan link: %v", err) - return nil, err - } - // Example: ip link set vni100 master br100 addrgenmode none - if err := netlink.LinkSetMaster(vxlan, bridge); err != nil { - fmt.Printf("Failed to add Vxlan to bridge: %v", err) - return nil, err - } - // Example: ip link set vni100 up - if err := netlink.LinkSetUp(vxlan); err != nil { - fmt.Printf("Failed to up Vxlan link: %v", err) - return nil, err + + // create bridge and vxlan only if VNI value is not empty + if in.Vrf.Spec.Vni > 0 { + // Example: ip link add br100 type bridge + bridgeName := fmt.Sprintf("br%d", in.Vrf.Spec.Vni) + bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: bridgeName}} + if err := netlink.LinkAdd(bridge); err != nil { + fmt.Printf("Failed to create Bridge link: %v", err) + return nil, err + } + // Example: ip link set br100 master blue addrgenmode none + if err := netlink.LinkSetMaster(bridge, vrf); err != nil { + fmt.Printf("Failed to add Bridge to VRF: %v", err) + return nil, err + } + // Example: ip link set br100 addr aa:bb:cc:00:00:02 + if err := netlink.LinkSetHardwareAddr(bridge, mac); err != nil { + fmt.Printf("Failed to set MAC on Bridge link: %v", err) + return nil, err + } + // Example: ip link set br100 up + if err := netlink.LinkSetUp(bridge); err != nil { + fmt.Printf("Failed to up Bridge link: %v", err) + return nil, err + } + // Example: ip link add vni100 type vxlan local 10.0.0.4 dstport 4789 id 100 nolearning + vxlanName := fmt.Sprintf("vni%d", in.Vrf.Spec.Vni) + myip := make(net.IP, 4) + binary.BigEndian.PutUint32(myip, in.Vrf.Spec.VtepIpPrefix.Addr.GetV4Addr()) + // TODO: take Port from proto instead of hard-coded + vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(in.Vrf.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip} + if err := netlink.LinkAdd(vxlan); err != nil { + fmt.Printf("Failed to create Vxlan link: %v", err) + return nil, err + } + // Example: ip link set vni100 master br100 addrgenmode none + if err := netlink.LinkSetMaster(vxlan, bridge); err != nil { + fmt.Printf("Failed to add Vxlan to bridge: %v", err) + return nil, err + } + // Example: ip link set vni100 up + if err := netlink.LinkSetUp(vxlan); err != nil { + fmt.Printf("Failed to up Vxlan link: %v", err) + return nil, err + } } response := proto.Clone(in.Vrf).(*pb.Vrf) response.Status = &pb.VrfStatus{LocalAs: 4, RoutingTable: tableID, Rmac: mac}