Skip to content

Commit

Permalink
implement vxlan-stitch
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed Aug 29, 2023
1 parent e087b28 commit aa614a6
Show file tree
Hide file tree
Showing 12 changed files with 402 additions and 70 deletions.
6 changes: 6 additions & 0 deletions links/endpoint_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ type EndpointBridge struct {
EndpointGeneric
}

func NewEndpointBridge(eg *EndpointGeneric) *EndpointBridge {
return &EndpointBridge{
EndpointGeneric: *eg,
}
}

func (e *EndpointBridge) Verify(p *VerifyLinkParams) error {
var errs []error
err := CheckEndpointUniqueness(e)
Expand Down
6 changes: 6 additions & 0 deletions links/endpoint_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ type EndpointHost struct {
EndpointGeneric
}

func NewEndpointHost(eg *EndpointGeneric) *EndpointHost {
return &EndpointHost{
EndpointGeneric: *eg,
}
}

func (e *EndpointHost) Verify(_ *VerifyLinkParams) error {
errs := []error{}
err := CheckEndpointUniqueness(e)
Expand Down
8 changes: 7 additions & 1 deletion links/endpoint_macvlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ type EndpointMacVlan struct {
EndpointGeneric
}

// Verify verifies the veth based deployment pre-conditions.
func NewEndpointMacVlan(eg *EndpointGeneric) *EndpointMacVlan {
return &EndpointMacVlan{
EndpointGeneric: *eg,
}
}

// Verify verifies the veth based deployment pre-conditions
func (e *EndpointMacVlan) Verify(_ *VerifyLinkParams) error {
return CheckEndpointExists(e)
}
12 changes: 3 additions & 9 deletions links/endpoint_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,11 @@ func (er *EndpointRaw) Resolve(params *ResolveParams, l Link) (Endpoint, error)

switch node.GetLinkEndpointType() {
case LinkEndpointTypeBridge:
e = &EndpointBridge{
EndpointGeneric: *genericEndpoint,
}
e = NewEndpointBridge(genericEndpoint)
case LinkEndpointTypeHost:
e = &EndpointHost{
EndpointGeneric: *genericEndpoint,
}
e = NewEndpointHost(genericEndpoint)
case LinkEndpointTypeVeth:
e = &EndpointVeth{
EndpointGeneric: *genericEndpoint,
}
e = NewEndpointVeth(genericEndpoint)
}

// also add the endpoint to the node
Expand Down
6 changes: 6 additions & 0 deletions links/endpoint_veth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ type EndpointVeth struct {
EndpointGeneric
}

func NewEndpointVeth(eg *EndpointGeneric) *EndpointVeth {
return &EndpointVeth{
EndpointGeneric: *eg,
}
}

// Verify verifies the veth based deployment pre-conditions.
func (e *EndpointVeth) Verify(_ *VerifyLinkParams) error {
return CheckEndpointUniqueness(e)
Expand Down
25 changes: 20 additions & 5 deletions links/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ type LinkDefinition struct {
type LinkType string

const (
LinkTypeVEth LinkType = "veth"
LinkTypeMgmtNet LinkType = "mgmt-net"
LinkTypeMacVLan LinkType = "macvlan"
LinkTypeHost LinkType = "host"
LinkTypeVxlan LinkType = "vxlan"
LinkTypeVEth LinkType = "veth"
LinkTypeMgmtNet LinkType = "mgmt-net"
LinkTypeMacVLan LinkType = "macvlan"
LinkTypeHost LinkType = "host"
LinkTypeVxlan LinkType = "vxlan"
LinkTypeVxlanStitch LinkType = "vxlan-stitch"

// LinkTypeBrief is a link definition where link types
// are encoded in the endpoint definition as string and allow users
Expand All @@ -65,6 +66,8 @@ func parseLinkType(s string) (LinkType, error) {
return LinkTypeBrief, nil
case string(LinkTypeVxlan):
return LinkTypeVxlan, nil
case string(LinkTypeVxlanStitch):
return LinkTypeVxlanStitch, nil
default:
return "", fmt.Errorf("unable to parse %q as LinkType", s)
}
Expand Down Expand Up @@ -159,6 +162,18 @@ func (ld *LinkDefinition) UnmarshalYAML(unmarshal func(interface{}) error) error
if err != nil {
return err
}
l.LinkVxlanRaw.LinkType = LinkTypeVxlan
ld.Link = &l.LinkVxlanRaw
case LinkTypeVxlanStitch:
var l struct {
Type string `yaml:"type"`
LinkVxlanRaw `yaml:",inline"`
}
err := unmarshal(&l)
if err != nil {
return err
}
l.LinkVxlanRaw.LinkType = LinkTypeVxlanStitch
ld.Link = &l.LinkVxlanRaw
case LinkTypeBrief:
// brief link's endpoint format
Expand Down
2 changes: 1 addition & 1 deletion links/link_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (r *LinkHostRaw) Resolve(params *ResolveParams) (Link, error) {
return nil, err
}
// set the end point in the link
link.Endpoints = []Endpoint{ep, hostEp}
link.endpoints = []Endpoint{ep, hostEp}

// add the link to the endpoints node
hostEp.GetNode().AddLink(link)
Expand Down
2 changes: 1 addition & 1 deletion links/link_mgmt-net.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r *LinkMgmtNetRaw) Resolve(params *ResolveParams) (Link, error) {
return nil, err
}

link.Endpoints = []Endpoint{bridgeEp, contEp}
link.endpoints = []Endpoint{bridgeEp, contEp}

// add link to respective endpoint nodes
bridgeEp.GetNode().AddLink(link)
Expand Down
31 changes: 18 additions & 13 deletions links/link_veth.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ func (r *LinkVEthRaw) Resolve(params *ResolveParams) (Link, error) {
}

// create LinkVEth struct
l := &LinkVEth{
LinkCommonParams: r.LinkCommonParams,
Endpoints: make([]Endpoint, 0, 2),
}
l := NewLinkVEth()
l.LinkCommonParams = r.LinkCommonParams

// resolve raw endpoints (epr) to endpoints (ep)
for _, epr := range r.Endpoints {
Expand All @@ -62,7 +60,7 @@ func (r *LinkVEthRaw) Resolve(params *ResolveParams) (Link, error) {
return nil, err
}
// add endpoint to the link endpoints
l.Endpoints = append(l.Endpoints, ep)
l.endpoints = append(l.endpoints, ep)
// add link to endpoint node
ep.GetNode().AddLink(l)
}
Expand Down Expand Up @@ -90,12 +88,19 @@ func linkVEthRawFromLinkBriefRaw(lb *LinkBriefRaw) (*LinkVEthRaw, error) {

type LinkVEth struct {
LinkCommonParams
Endpoints []Endpoint
endpoints []Endpoint

deploymentState LinkDeploymentState
deployMutex sync.Mutex
}

func NewLinkVEth() *LinkVEth {
return &LinkVEth{
endpoints: make([]Endpoint, 0, 2),
deploymentState: LinkDeploymentStateNotDeployed,
}
}

func (*LinkVEth) GetType() LinkType {
return LinkTypeVEth
}
Expand All @@ -120,11 +125,11 @@ func (l *LinkVEth) Deploy(ctx context.Context) error {
// build the netlink.Veth struct for the link provisioning
linkA := &netlink.Veth{
LinkAttrs: netlink.LinkAttrs{
Name: l.Endpoints[0].GetRandIfaceName(),
Name: l.endpoints[0].GetRandIfaceName(),
MTU: l.MTU,
// Mac address is set later on
},
PeerName: l.Endpoints[1].GetRandIfaceName(),
PeerName: l.endpoints[1].GetRandIfaceName(),
// PeerMac address is set later on
}

Expand All @@ -135,13 +140,13 @@ func (l *LinkVEth) Deploy(ctx context.Context) error {
}

// retrieve the netlink.Link for the B / Peer side of the link
linkB, err := netlink.LinkByName(l.Endpoints[1].GetRandIfaceName())
linkB, err := netlink.LinkByName(l.endpoints[1].GetRandIfaceName())
if err != nil {
return err
}

// once veth pair is created, disable tx offload for the veth pair
for _, ep := range l.Endpoints {
for _, ep := range l.endpoints {
if err := utils.EthtoolTXOff(ep.GetRandIfaceName()); err != nil {
return err
}
Expand All @@ -154,8 +159,8 @@ func (l *LinkVEth) Deploy(ctx context.Context) error {
for idx, link := range []netlink.Link{linkA, linkB} {
// if the node is a regular namespace node
// add link to node, rename, set mac and Up
err = l.Endpoints[idx].GetNode().AddLinkToContainer(ctx, link,
SetNameMACAndUpInterface(link, l.Endpoints[idx]))
err = l.endpoints[idx].GetNode().AddLinkToContainer(ctx, link,
SetNameMACAndUpInterface(link, l.endpoints[idx]))
if err != nil {
return err
}
Expand All @@ -172,5 +177,5 @@ func (*LinkVEth) Remove(_ context.Context) error {
}

func (l *LinkVEth) GetEndpoints() []Endpoint {
return l.Endpoints
return l.endpoints
}
14 changes: 6 additions & 8 deletions links/link_veth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestLinkVEthRaw_Resolve(t *testing.T) {
Labels: map[string]string{"foo": "bar"},
Vars: map[string]any{"foo": "bar"},
},
Endpoints: []Endpoint{
endpoints: []Endpoint{
&EndpointVeth{
EndpointGeneric: EndpointGeneric{
Node: fn1,
Expand Down Expand Up @@ -183,15 +183,13 @@ func TestLinkVEthRaw_Resolve(t *testing.T) {
t.Errorf("LinkVEthRaw.Resolve() LinkCommonParams diff = %s", d)
}

for i, e := range l.Endpoints {
if e.(*EndpointVeth).IfaceName != tt.want.Endpoints[i].(*EndpointVeth).IfaceName {
t.Errorf("LinkVEthRaw.Resolve() EndpointVeth got %s, want %s",
e.(*EndpointVeth).IfaceName, tt.want.Endpoints[i].(*EndpointVeth).IfaceName)
for i, e := range l.endpoints {
if e.(*EndpointVeth).IfaceName != tt.want.endpoints[i].(*EndpointVeth).IfaceName {
t.Errorf("LinkVEthRaw.Resolve() EndpointVeth got %s, want %s", e.(*EndpointVeth).IfaceName, tt.want.endpoints[i].(*EndpointVeth).IfaceName)
}

if e.(*EndpointVeth).Node != tt.want.Endpoints[i].(*EndpointVeth).Node {
t.Errorf("LinkVEthRaw.Resolve() EndpointVeth got %s, want %s",
e.(*EndpointVeth).Node, tt.want.Endpoints[i].(*EndpointVeth).Node)
if e.(*EndpointVeth).Node != tt.want.endpoints[i].(*EndpointVeth).Node {
t.Errorf("LinkVEthRaw.Resolve() EndpointVeth got %s, want %s", e.(*EndpointVeth).Node, tt.want.endpoints[i].(*EndpointVeth).Node)
}
}
})
Expand Down
Loading

0 comments on commit aa614a6

Please sign in to comment.