Skip to content
This repository has been archived by the owner on Aug 3, 2020. It is now read-only.

WIP: Cross environment changes v1 #43

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions backend/vxlan/arp_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package vxlan

import (
logrus "github.com/rancher/rancher-net/log"
"github.com/vishvananda/netlink"
"net"
)

func addARPEntry(intfName string, ip net.IP, mac net.HardwareAddr) error {
logrus.Debugf("vxlan: Adding arp entry ip: %v mac:%v inftName: %v", ip, mac, intfName)

l, err := netlink.LinkByName(intfName)
if err != nil {
return err
}

n := &netlink.Neigh{
IP: ip,
HardwareAddr: mac,
LinkIndex: l.Attrs().Index,
State: netlink.NUD_PERMANENT,
Flags: netlink.NTF_SELF,
}

err = netlink.NeighAppend(n)

if err != nil {
logrus.Errorf("vxlan: Couldn't add neighbor: %v", err)
return err
}

return nil
}

func delARPEntry(intfName string, ip net.IP, mac net.HardwareAddr) error {
logrus.Debugf("vxlan: Deleting arp entry ip: %v mac:%v intfName: %v", ip, mac, intfName)

l, err := netlink.LinkByName(intfName)
if err != nil {
return err
}

n := &netlink.Neigh{
IP: ip,
HardwareAddr: mac,
LinkIndex: l.Attrs().Index,
State: netlink.NUD_PERMANENT,
Flags: netlink.NTF_SELF,
}

err = netlink.NeighDel(n)

if err != nil {
logrus.Errorf("vxlan: Couldn't delete neighbor: %v", err)
return err
}

return nil
}
110 changes: 110 additions & 0 deletions backend/vxlan/peer_vxlan_entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package vxlan

import (
"fmt"
"net"
"strings"

logrus "github.com/rancher/rancher-net/log"
"github.com/rancher/rancher-net/store"
"github.com/vishvananda/netlink"
)

type peerVxlanEntry struct {
intfName string
ip net.IP
ipnet *net.IPNet
vtepMAC net.HardwareAddr
hostIP net.IP
}

// newPeerVxlanEntry creates a new struct representing the Peer
// from the given entry
func newPeerVxlanEntry(intfName string, e store.Entry) (*peerVxlanEntry, error) {
ip, _, err := net.ParseCIDR(e.IpAddress)
if err != nil {
logrus.Errorf("Couldn't parseCIDR for IP: %v", e.IpAddress)
return nil, err
}

ipslash32 := strings.Split(e.IpAddress, "/")[0] + "/32"

ipnet, err := netlink.ParseIPNet(ipslash32)
if err != nil {
logrus.Errorf("Couldn't ParseIPNet for IP: %v", e.IpAddress)
return nil, err
}

vtepMAC, err := getMACAddressForVxlanIP(vxlanMACRange, ip)
if err != nil {
logrus.Errorf("Couldn't get MAC address for IP: %v", ip)
return nil, err
}
hostIP := net.ParseIP(e.HostIpAddress)
if hostIP == nil {
logrus.Errorf("Couldn't parse host IP address")
return nil, fmt.Errorf("Couldn't parse host IP address")
}

return &peerVxlanEntry{intfName, ip, ipnet, vtepMAC, hostIP}, nil
}

func (v *peerVxlanEntry) add() error {
var err error

err = addRoute(v.ipnet, nil, v.intfName)
if err != nil {
return err
}

err = addARPEntry(v.intfName, v.ip, v.vtepMAC)
if err != nil {
return err
}

err = addVxlanForwardingEntry(v.intfName, v.vtepMAC, v.hostIP)
if err != nil {
delARPEntry(v.intfName, v.ip, v.vtepMAC)
return err
}

return nil
}

func (v *peerVxlanEntry) del() error {
var err error

err = delRoute(v.ipnet, nil, v.intfName)
if err != nil {
return err
}

err = delARPEntry(v.intfName, v.ip, v.vtepMAC)
if err != nil {
return err
}

err = deleteVxlanForwardingEntry(v.intfName, v.vtepMAC, v.hostIP)
if err != nil {
return err
}

return nil

}

func (v *peerVxlanEntry) upd() error {
//var err error

//err = v.del()
//if err != nil {
// return err
//}

//err = v.add()
//if err != nil {
// return err
//}

return fmt.Errorf("not updating")
}
107 changes: 107 additions & 0 deletions backend/vxlan/remote_vxlan_entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package vxlan

import (
"fmt"
"net"
"strings"

logrus "github.com/rancher/rancher-net/log"
"github.com/rancher/rancher-net/store"
"github.com/vishvananda/netlink"
)

type remoteVxlanEntry struct {
intfName string
ip net.IP
ipnet *net.IPNet
via net.IP
vtepMAC net.HardwareAddr
}

// newRemoteVxlanEntry create a new struct representing the
// non peer remote entry
// Need to change /16 to /32, else the route is not getting installed
func newRemoteVxlanEntry(intfName string, e store.Entry, peersMapping map[string]net.IP) (*remoteVxlanEntry, error) {
var err error
logrus.Debugf("building remoteVxlanEntry for %v", e)

ip, _, err := net.ParseCIDR(e.IpAddress)
if err != nil {
logrus.Errorf("Couldn't parseCIDR for IP: %v", e.IpAddress)
return nil, err
}

ipslash32 := strings.Split(e.IpAddress, "/")[0] + "/32"

ipnet, err := netlink.ParseIPNet(ipslash32)
if err != nil {
logrus.Errorf("Couldn't ParseIPNet for IP: %v", e.IpAddress)
return nil, err
}

var via net.IP
if e.HostIpAddress != "" {
via = peersMapping[e.HostIpAddress]
} else {
via = nil
}

var vtepMAC net.HardwareAddr
if via != nil {
vtepMAC, err = getMACAddressForVxlanIP(vxlanMACRange, via)
if err != nil {
logrus.Errorf("Couldn't get MAC address for IP: %v", via)
return nil, err
}
} else {
vtepMAC = nil
}
return &remoteVxlanEntry{intfName, ip, ipnet, via, vtepMAC}, nil
}

func (v *remoteVxlanEntry) add() error {
err := addRoute(v.ipnet, nil, v.intfName)
if err != nil {
return err
}

err = addARPEntry(v.intfName, v.ip, v.vtepMAC)
if err != nil {
return err
}

return nil
}

func (v *remoteVxlanEntry) del() error {
err := delRoute(v.ipnet, nil, v.intfName)
if err != nil {
return err
}

err = delARPEntry(v.intfName, v.ip, v.vtepMAC)
if err != nil {
return err
}

return nil
}

func (v *remoteVxlanEntry) upd() error {
//err := updateRoute(v.ipnet, nil, v.intfName)
//if err != nil {
// return err
//}

//err = delARPEntry(v.intfName, v.ip, v.vtepMAC)
//if err != nil {
// return err
//}

//err = addARPEntry(v.intfName, v.ip, v.vtepMAC)
//if err != nil {
// return err
//}

return fmt.Errorf("not updating")
}
77 changes: 77 additions & 0 deletions backend/vxlan/route_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package vxlan

import (
logrus "github.com/rancher/rancher-net/log"
"github.com/vishvananda/netlink"
"net"
)

func addRoute(ip *net.IPNet, via net.IP, intfName string) error {
logrus.Debugf("vxlan: adding route: %v via %v intfName: %v", ip, via, intfName)
r := &netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
Dst: ip,
}

if via != nil {
r.Gw = via
}

if intfName != "" {
l, err := findVxlanInterface(intfName)
if err != nil {
logrus.Errorf("Couldn't find link by name: %v", intfName)
return err
}
r.LinkIndex = l.Attrs().Index
}

err := netlink.RouteAdd(r)
if err != nil {
logrus.Errorf("vxlan: error adding route: %v", err)
return err
}

return nil
}

func updateRoute(ip *net.IPNet, via net.IP, intfName string) error {
logrus.Debugf("vxlan: updating route: %v via %v intfName: %v", ip, via, intfName)

err := delRoute(ip, nil, intfName)
if err != nil {
logrus.Errorf("vxlan: error updating route: %v", err)
return err
}

return addRoute(ip, via, intfName)
}

func delRoute(ip *net.IPNet, via net.IP, intfName string) error {
logrus.Debugf("vxlan: deleting route: %v via %v intfName: %v", ip, via, intfName)
r := &netlink.Route{
Scope: netlink.SCOPE_UNIVERSE,
Dst: ip,
}

if via != nil {
r.Gw = via
}

if intfName != "" {
l, err := findVxlanInterface(intfName)
if err != nil {
logrus.Errorf("Couldn't find link by name: %v", intfName)
return err
}
r.LinkIndex = l.Attrs().Index
}

err := netlink.RouteDel(r)
if err != nil {
logrus.Errorf("vxlan: error adding route: %v", err)
return err
}

return nil
}
Loading