-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: forrestchen <forrestchen@tencent.com>
- Loading branch information
1 parent
cbc450c
commit 42e6273
Showing
12 changed files
with
219 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net" | ||
"runtime" | ||
|
||
"github.com/containernetworking/cni/pkg/skel" | ||
"github.com/containernetworking/cni/pkg/types" | ||
t020 "github.com/containernetworking/cni/pkg/types/020" | ||
"github.com/containernetworking/cni/pkg/version" | ||
"github.com/vishvananda/netlink" | ||
"tkestack.io/galaxy/cni/ipam" | ||
"tkestack.io/galaxy/pkg/network" | ||
"tkestack.io/galaxy/pkg/network/vlan" | ||
"tkestack.io/galaxy/pkg/utils" | ||
) | ||
|
||
func init() { | ||
// this ensures that main runs only on main thread (thread group leader). | ||
// since namespace ops (unshare, setns) are done for a single thread, we | ||
// must ensure that the goroutine does not jump from OS thread to thread | ||
runtime.LockOSThread() | ||
} | ||
|
||
func main() { | ||
skel.PluginMain(cmdAdd, cmdDel, version.Legacy) | ||
} | ||
|
||
func cmdDel(args *skel.CmdArgs) error { | ||
if err := utils.DeleteAllVeth(args.Netns); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func cmdAdd(args *skel.CmdArgs) error { | ||
conf := vlan.NetConf{} | ||
if err := json.Unmarshal(args.StdinData, &conf); err != nil { | ||
return fmt.Errorf("conf error: %v", err) | ||
} | ||
vlanIds, results, err := ipam.Allocate(conf.IPAM.Type, args) | ||
if err != nil { | ||
return fmt.Errorf("allocate failed: %v", err) | ||
} | ||
if err := utils.UnSetArpIgnore("all"); err != nil { | ||
return err | ||
} | ||
if err := utils.EnableNonlocalBind(); err != nil { | ||
return err | ||
} | ||
ifName := args.IfName | ||
ifIndex := 0 | ||
for i := range vlanIds { | ||
vlanId := vlanIds[i] | ||
result, err := t020.GetResult(results[i]) | ||
if err != nil { | ||
return fmt.Errorf("result convert failed: %v", err) | ||
} | ||
device := conf.Device | ||
// fixme: make route configurable | ||
if i != 0 { | ||
result.IP4.Routes = []types.Route{{ | ||
Dst: net.IPNet{ | ||
IP: result.IP4.IP.IP.Mask(result.IP4.IP.Mask), | ||
Mask: result.IP4.IP.Mask, | ||
}, | ||
}} | ||
} | ||
var masterDevice netlink.Link | ||
if masterDevice, err = vlan.SetupVlanInPureMode(device, vlanId); err != nil { | ||
return fmt.Errorf("failed setup vlan: %v", err) | ||
} | ||
suffix := "" | ||
if i != 0 { | ||
suffix = fmt.Sprintf("-%d", i+1) | ||
ifIndex++ | ||
args.IfName = fmt.Sprintf("eth%d", ifIndex) | ||
if args.IfName == ifName { | ||
ifIndex++ | ||
args.IfName = fmt.Sprintf("eth%d", ifIndex) | ||
} | ||
} | ||
v4Addr, err := netlink.AddrList(masterDevice, netlink.FAMILY_V4) | ||
if err != nil { | ||
return fmt.Errorf("error getting ipv4 address %v", err) | ||
} | ||
filteredAddr := network.FilterLoopbackAddr(v4Addr) | ||
var src net.IP | ||
if len(filteredAddr) > 0 { | ||
src = filteredAddr[0].IP | ||
} | ||
if err := utils.VethConnectsHostWithContainer(result, args, "", suffix, src); err != nil { | ||
return fmt.Errorf("veth connect failed: %v", err) | ||
} | ||
utils.SendGratuitousARP(masterDevice.Attrs().Name, result.IP4.IP.IP.String(), "", conf.GratuitousArpRequest) | ||
} | ||
args.IfName = ifName | ||
result, _ := t020.GetResult(results[0]) | ||
return result.Print() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package vlan | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/vishvananda/netlink" | ||
"tkestack.io/galaxy/pkg/utils" | ||
) | ||
|
||
// SetupVlan is used in pure veth mode, in this mode, vlan will not attach to bridge device | ||
func SetupVlanInPureMode(ethDevice string, vlanId uint16) (netlink.Link, error) { | ||
if vlanId == 0 { | ||
device, err := netlink.LinkByName(ethDevice) | ||
if err != nil { | ||
return nil, fmt.Errorf("device %v not found", err) | ||
} | ||
if err := utils.UnSetArpIgnore(ethDevice); err != nil { | ||
return nil, err | ||
} | ||
if err := utils.SetProxyArp(ethDevice); err != nil { | ||
return nil, err | ||
} | ||
return device, nil | ||
} | ||
vlanDevice, err := findVlanDevice(vlanId) | ||
if err != nil { | ||
return nil, fmt.Errorf("find vlan device failed: %v", err) | ||
} | ||
if err = netlink.LinkSetUp(vlanDevice); err != nil { | ||
return nil, fmt.Errorf("failed set vlan %v up: %v", vlanDevice.Attrs().Name, err) | ||
} | ||
if err := utils.UnSetArpIgnore(vlanDevice.Attrs().Name); err != nil { | ||
return nil, err | ||
} | ||
return vlanDevice, utils.SetProxyArp(vlanDevice.Attrs().Name) | ||
} | ||
|
||
func findVlanDevice(vlanID uint16) (netlink.Link, error) { | ||
links, err := netlink.LinkList() | ||
if err != nil { | ||
return nil, err | ||
} | ||
var result netlink.Link | ||
for _, link := range links { | ||
if link.Type() == "vlan" { | ||
if vlan, ok := link.(*netlink.Vlan); !ok { | ||
return nil, fmt.Errorf("vlan device type case error: %T", link) | ||
} else { | ||
if vlan.VlanId == int(vlanID) { | ||
if result == nil { | ||
result = link | ||
} else { | ||
return nil, fmt.Errorf("found 2 device with same vlanID %d: %s, %s", vlanID, result.Attrs().Name, vlan.Name) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if result == nil { | ||
return nil, fmt.Errorf("no vlan device with vlanID %d", vlanID) | ||
} | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters