Skip to content

Commit

Permalink
update inject client with vlan sub interface case
Browse files Browse the repository at this point in the history
Signed-off-by: Periyasamy Palanisamy <[email protected]>
  • Loading branch information
pperiyasamy committed Oct 1, 2021
1 parent 2c2b60a commit 2760bf2
Showing 1 changed file with 83 additions and 2 deletions.
85 changes: 83 additions & 2 deletions pkg/kernel/networkservice/inject/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,21 @@ func move(ctx context.Context, conn *networkservice.Connection, isClient, isMove
}

ifName := mech.GetInterfaceName()
vlanID := mech.GetVLAN()
if !isMoveBack {
err = moveToContNetNS(vfConfig, ifName, hostNetNS, contNetNS)
if vlanID == 0 {
err = moveToContNetNS(vfConfig, ifName, hostNetNS, contNetNS)
} else {
err = moveAndCreateVLANSubIfInContNetNS(vfConfig, ifName, vlanID, hostNetNS, contNetNS)
}
vfConfig.ContNetNS = contNetNS
} else {
err = moveToHostNetNS(vfConfig, ifName, hostNetNS, contNetNS)
if vlanID == 0 {
err = moveToHostNetNS(vfConfig, ifName, hostNetNS, contNetNS)
} else {
err = deleteVlanSubInterface(vfConfig.VFInterfaceName, ifName, vlanID, hostNetNS, contNetNS)
// TODO: move out VF into host netns when the last client is deleted.
}
}
if err != nil {
// link may not be available at this stage for cases like veth pair (might be deleted in previous chain element itself)
Expand Down Expand Up @@ -136,6 +146,18 @@ func moveToContNetNS(vfConfig *vfconfig.VFConfig, ifName string, hostNetNS, cont
return
}

func moveAndCreateVLANSubIfInContNetNS(vfConfig *vfconfig.VFConfig, ifName string, vlanID uint32,
hostNetNS, contNetNS netns.NsHandle) (err error) {
link, _ := kernellink.FindHostDevice("", vfConfig.VFInterfaceName, contNetNS)
if link == nil {
err = moveInterfaceToAnotherNamespace(vfConfig.VFInterfaceName, hostNetNS, hostNetNS, contNetNS)
if err == nil {
err = createVlanSubInterface(vfConfig.VFInterfaceName, ifName, vlanID, hostNetNS, contNetNS)
}
}
return
}

func moveToHostNetNS(vfConfig *vfconfig.VFConfig, ifName string, hostNetNS, contNetNS netns.NsHandle) (err error) {
if vfConfig != nil && vfConfig.VFInterfaceName != ifName {
link, _ := kernellink.FindHostDevice(vfConfig.VFPCIAddress, vfConfig.VFInterfaceName, hostNetNS)
Expand All @@ -161,3 +183,62 @@ func moveToHostNetNS(vfConfig *vfconfig.VFConfig, ifName string, hostNetNS, cont
}
return
}

func createVlanSubInterface(parentIfName, subIfName string, vlanID uint32, curNetNS, targetNetNS netns.NsHandle) error {
return nshandle.RunIn(curNetNS, targetNetNS, func() error {
if interfaceExists(subIfName) {
return nil
}
// get the parent link to attach a vlan subinterface
parentLink, err := netlink.LinkByName(parentIfName)
if err != nil {
return err
}
vlanLink := &netlink.Vlan{
LinkAttrs: netlink.LinkAttrs{
Name: subIfName,
ParentIndex: parentLink.Attrs().Index,
},
VlanId: int(vlanID),
}
// create the subinterface
if err := netlink.LinkAdd(vlanLink); err != nil {
return err
}
// Bring the new netlink iface up
if err := netlink.LinkSetUp(vlanLink); err != nil {
return err
}
return nil
})
}

func deleteVlanSubInterface(parentIfName, subIfName string, vlanID uint32, curNetNS, targetNetNS netns.NsHandle) error {
return nshandle.RunIn(curNetNS, targetNetNS, func() error {
if !interfaceExists(subIfName) {
return nil
}
// get the parent link to attach a vlan subinterface
parentLink, err := netlink.LinkByName(parentIfName)
if err != nil {
return nil
}
vlanLink := &netlink.Vlan{
LinkAttrs: netlink.LinkAttrs{
Name: subIfName,
ParentIndex: parentLink.Attrs().Index,
},
VlanId: int(vlanID),
}
// delete the subinterface
if err := netlink.LinkDel(vlanLink); err != nil {
return err
}
return nil
})
}

func interfaceExists(ifName string) bool {
_, err := netlink.LinkByName(ifName)
return err == nil
}

0 comments on commit 2760bf2

Please sign in to comment.