Skip to content

Commit

Permalink
fix(vrf): create bridge and vxlan only if VNI value is not empty
Browse files Browse the repository at this point in the history
Fixes #105

Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
  • Loading branch information
glimchb committed Aug 9, 2023
1 parent 54491ab commit 220c315
Showing 1 changed file with 46 additions and 41 deletions.
87 changes: 46 additions & 41 deletions pkg/evpn/vrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <routing-table-number> 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
}

Check warning on line 99 in pkg/evpn/vrf.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/vrf.go#L92-L99

Added lines #L92 - L99 were not covered by tests
// 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
}

Check warning on line 104 in pkg/evpn/vrf.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/vrf.go#L101-L104

Added lines #L101 - L104 were not covered by tests
// 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
}

Check warning on line 109 in pkg/evpn/vrf.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/vrf.go#L106-L109

Added lines #L106 - L109 were not covered by tests
// 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
}

Check warning on line 114 in pkg/evpn/vrf.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/vrf.go#L111-L114

Added lines #L111 - L114 were not covered by tests
// 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
}

Check warning on line 124 in pkg/evpn/vrf.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/vrf.go#L116-L124

Added lines #L116 - L124 were not covered by tests
// 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
}

Check warning on line 129 in pkg/evpn/vrf.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/vrf.go#L126-L129

Added lines #L126 - L129 were not covered by tests
// 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
}

Check warning on line 134 in pkg/evpn/vrf.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/vrf.go#L131-L134

Added lines #L131 - L134 were not covered by tests
}
response := proto.Clone(in.Vrf).(*pb.Vrf)
response.Status = &pb.VrfStatus{LocalAs: 4, RoutingTable: tableID, Rmac: mac}
Expand Down

0 comments on commit 220c315

Please sign in to comment.