Version: v0.0.0-...-9649b88 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2020 License: Apache-2.0 Imports: 27 Imported by: 0




View Source
const MaxRetryTimesOfFindLinkByName int = 10
View Source
const PCIPATH = "/sys/bus/pci"
View Source
const SysNetPath = "/sys/class/net"


View Source
var AttachVethToPod = func(netNs string, port *manager.Port, vethName string) error {
	pod := NewTempPod()
	nameSpace4Pod := &implement.NameSpace{}
	netLink4Pod := &implement.NetLink{}

	vmNs, _ := pod.NameSpace.Get()
	defer vmNs.Close()

	vm := NewMachine(vmNs)
	netLink4Machine := &implement.NetLink{}
	pid, errN := NetNSToPID(netNs)
	if errN != nil {
		klog.Errorf("Error moving to netns. Err: %v", errN)
		return errN
	container := NewContainer(pid)
	netLink4Container := &implement.NetLink{}
	netNs4Container := &implement.NetNs{}

	err := pod.BindLinkByVethName(container, port, vm, vethName, vmNs)
	if err != nil {
		return fmt.Errorf("%v:bind-AddSelectedIntfToPod:pod.bindlink(containerID, port, vfLink)error", err)

	return nil
View Source
var DelOvsBrInterfaces = func(brName string, ifs []string) {
	klog.Infof("DelOvsBrInterfaces: delete %v from %s START", ifs, brName)
	for _, ifName := range ifs {
		err := DelVethFromOvs(brName, ifName)
		if err != nil {
			klog.Errorf("DelOvsBrInterfaces: DelVethFromOvs %s from %s FAILED, error: %v", brName, ifName, err)
		klog.Infof("DelOvsBrInterfaces: DelVethFromOvs %s from %s SUCC", brName, ifName)

	klog.Infof("DelOvsBrInterfaces: delete %v from %s FINI", ifs, brName)
View Source
var DockerExec = func(containerId, program, para string) error {
	dockerCtl, _ := exec.LookPath("docker")
	dockerPsArgs := []string{"exec", containerId, program, string(para)}
	output, err := exec.Command(dockerCtl, dockerPsArgs...).CombinedOutput()
	if err != nil || strings.Contains(strings.ToUpper(string(output)), "ERROR") {
		klog.Infof("DockerExec-err:output :%v", string(output))
		param := " docker exec " + containerId + " " + program + " " + para
		klog.Errorf("ERROR:" + dockerCtl + param +
			"\n OUTPUT:" + string(output) +
			"\n CAUSE:" + err.Error())
		return fmt.Errorf("%v:dockerExec:exec.command() ERROR", err)
	klog.Infof("DockerExec-suc:output :%v", string(output))
	return nil
View Source
var GetC0ImageName = func() string {
	agtCtx := cni.GetGlobalContext()
	var c0ImageName = ""
	for i := 0; i < 3; i++ {
		key := dbaccessor.GetKeyOfImageNameForC0(agtCtx.ClusterID, agtCtx.HostIP)
		c0ImageName, _ = adapter.ReadLeafFromDb(key)
		if c0ImageName != "" {
			klog.Infof("C0 image is %v", c0ImageName)
		time.Sleep(10 * time.Second)
	if c0ImageName == "" {
		klog.Errorf("GetC0ContainerIdByImageName: GetC0ContainerIdByImageName error : c0 image empty!")
	return c0ImageName
View Source
var GetContainerIDByName = func() (s string, e error) {
	var c0ContainerID string
	c0ImageName := GetC0ImageName()
	if c0ImageName == "" {
		return "", errors.New("getContainerIdByName:Get c0 image error")
	for i := 0; i <= 2; i++ {
		c0PodJSON, err := GetC0PodInfo()
		if err != nil {
			return "", err
		containersList, errList := c0PodJSON.GetObjectArray("status", "containerStatuses")
		if errList != nil {
			return "", errList
		for _, container := range containersList {
			imageName, _ := container.GetString("image")
			if imageName != c0ImageName {
			_, errRunning := container.GetObject("state", "running")
			if errRunning != nil {
				klog.Warningf("GetContainerIdByName: C0 container is not running")
			c0ContainerID, _ = container.GetString("containerID")
			c0ContainerID = strings.TrimPrefix(c0ContainerID, "docker://")
			return c0ContainerID, nil
		time.Sleep(5 * time.Second)
	return "", errors.New("getContainerIdByName:Get c0 id error")
View Source
var GetEthtoolOutputFunc func(ethName string) ([]byte, error)
View Source
var GetLspciOutputFunc func(args []string) ([]byte, error)
View Source
var GetPidFromDockerConf = func(containerId string) (string, error) {
	containerDir, err := GetContainerDir()
	if err != nil {
		klog.Errorf("GetPidFromDockerConf:GetContainerDir error! %v", err)
		return "", err
	dockerConfPath := containerDir + "/" + containerId
	fileList, err := ioutil.ReadDir(dockerConfPath)
	if err != nil {
		klog.Errorf("GetPidFromDockerConf:ReadDir error! %v", err)
		return "", err
	dockerConfFile := dockerConfPath + "/" + "config.json"
	for _, f := range fileList {
		rxConf := regexp.MustCompile(`^conf.*\.json$`)
		if rxConf.MatchString(f.Name()) {
			dockerConfFile = dockerConfPath + "/" + f.Name()
	dockerConf, err := ioutil.ReadFile(dockerConfFile)
	if err != nil {
		klog.Errorf("GetPidFromDockerConf:ReadFile error! %v", err)
		return "", err
	dockerConfJSON, errJSON := jason.NewObjectFromBytes(dockerConf)
	if errJSON != nil {
		klog.Errorf("GetPidFromDockerConf:NewObjectFromBytes error! %v", errJSON)
		return "", errJSON
	pidInt, errInt := dockerConfJSON.GetInt64("State", "Pid")
	klog.Infof("GetPidFromDockerConf:pid is %v", pidInt)
	if errInt != nil {
		klog.Errorf("GetPidFromDockerConf:GetInt64 error! %v", errJSON)
		return "", errInt
	pidStr := strconv.FormatInt(pidInt, 10)
	return pidStr, nil


View Source
var ListNousedOvsBrInterfaces = func(ports []string) []string {
	nonusedIfs := make([]string, 0)
	for _, portName := range ports {

		if isInterfaceDetached(portName) {
			nonusedIfs = append(nonusedIfs, portName)
	return nonusedIfs
View Source
var Nsenter = func(pid, program, para string) error {
	nsenterCtl, _ := exec.LookPath("nsenter")
	nsenterArgs := []string{"-t", pid, "--mount", "--uts", "--ipc", "--net", "--pid", program, para}
	output, err := exec.Command(nsenterCtl, nsenterArgs...).CombinedOutput()
	if err != nil {
		param := " nsenter -t  " + pid + " --mount --uts --ipc --net --pid " + program + " " + para
		klog.Errorf("ERROR:" + nsenterCtl + param +
			"\n OUTPUT:" + string(output) +
			"\n CAUSE:" + err.Error())
		return fmt.Errorf("%v:Nsenter:exec.command() ERROR", err)
	return nil
View Source
var RouteAddFunc func(route *netlink.Route) error


func ActiveVethPort

func ActiveVethPort(vethPort string) error

func AddVethToOvsWithVlanID

func AddVethToOvsWithVlanID(ovsBridge, vethPort, vlanID string) error

func CreateRoute

func CreateRoute(linkIndex int, dst *net.IPNet, gw net.IP) *netlink.Route

func DeactivateVethPort

func DeactivateVethPort(vethPort string) error

func DelNousedOvsBrInterfaces

func DelNousedOvsBrInterfaces(brName string) error

func DelNousedOvsBrInterfacesLoop

func DelNousedOvsBrInterfacesLoop(brName string)

func DelOvsBrsNousedInterfacesLoop

func DelOvsBrsNousedInterfacesLoop()

func DelVethFromOvs

func DelVethFromOvs(ovsBridge, vethPort string) error

func DestroyResidualBrintIntfcs

func DestroyResidualBrintIntfcs()

func GetBusInfo

func GetBusInfo(ethName string) (string, error)

func GetC0PodInfo

func GetC0PodInfo() (*jason.Object, error)

func GetContainerDir

func GetContainerDir() (string, error)

func GetDockerInfoByKey

func GetDockerInfoByKey(key []string) ([]string, error)

func GetLspciOutput

func GetLspciOutput(args []string) ([]byte, error)

func GetOVSList

func GetOVSList() (string, error)

func GetPciID

func GetPciID(busInfo string) (string, error)

func NetNSToPID

func NetNSToPID(ns string) (int, error)

func RouteAdd

func RouteAdd(route *netlink.Route) error

func SumOfVirtioNetPci

func SumOfVirtioNetPci() int


type Container

type Container struct {
	ID        string
	Pid       int
	NameSpace netns.NsHandle
	Link      *Link

	NetLink base.NetLink
	NetNs   base.NetNs

func NewContainer

func NewContainer(ID int) *Container
func (self *Container) BindLink(link *Link) bool
func (self *Container) SetNetLink(netlink base.NetLink)

func (*Container) SetNetNs

func (self *Container) SetNetNs(netns base.NetNs)

type Dpdknic

type Dpdknic struct {
	BusInfo    string      `json:"bus_info"`
	Name       string      `json:"name"`
	IP         string      `json:"ip"`
	Mask       string      `json:"mask"`
	Gateway    string      `json:"gateway"`
	Mac        string      `json:"mac"`
	Function   string      `json:"function"`
	Accelerate string      `json:"accelerate"`
	BusInfos   []string    `json:"bus_infos"`
	BondMode   string      `json:"bond_type"`
	Metadata   interface{} `json:"metadata"`

func BuildNormalNic

func BuildNormalNic(paasPort *manager.Port, netFunc string) (*Dpdknic, error)
type Link struct {
	Netlink base.NetLink
	IsVf    bool
	Pf      string
func NewLink(link netlink.Link) *Link

func (*Link) GetName

func (self *Link) GetName() string

func (*Link) SetAddr

func (self *Link) SetAddr(addr *netlink.Addr) bool

func (*Link) SetDown

func (self *Link) SetDown() bool

func (*Link) SetDynamicName

func (self *Link) SetDynamicName() bool

func (*Link) SetMac

func (self *Link) SetMac(mac string) bool

func (*Link) SetMtu

func (self *Link) SetMtu(mtu string) bool

func (*Link) SetName

func (self *Link) SetName(name string)

func (*Link) SetUp

func (self *Link) SetUp() bool

type LinkRepository

type LinkRepository struct {
	Links []*Link

func NewLinkRepository

func NewLinkRepository() *LinkRepository

func (*LinkRepository) AddRange

func (self *LinkRepository) AddRange(links []netlink.Link)

func (*LinkRepository) FindLinkByMac

func (self *LinkRepository) FindLinkByMac(mac string) *Link

func (*LinkRepository) FindLinkByName

func (self *LinkRepository) FindLinkByName(name string) *Link

func (*LinkRepository) FindLinkByPci

func (self *LinkRepository) FindLinkByPci(pci string) *Link

To find link by pci

func (*LinkRepository) FindVfLinkByName

func (self *LinkRepository) FindVfLinkByName(name string) *Link

func (*LinkRepository) GetPciEthMap

func (self *LinkRepository) GetPciEthMap(line string) (string, string, error)

func (*LinkRepository) Update

func (self *LinkRepository) Update(links []netlink.Link)

type Machine

type Machine struct {
	NameSpace netns.NsHandle
	LinkRepo  *LinkRepository

	NetLink base.NetLink

func NewMachine

func NewMachine(ns netns.NsHandle) *Machine

func (*Machine) FindLinkByMac

func (self *Machine) FindLinkByMac(mac string) *Link

func (*Machine) FindLinkByName

func (self *Machine) FindLinkByName(name string) *Link

func (*Machine) FindLinkByPci

func (self *Machine) FindLinkByPci(pci string) *Link

func (*Machine) FindVfLinkByName

func (self *Machine) FindVfLinkByName(name string) *Link
func (self *Machine) GetCurrentNsLinks() []netlink.Link
func (self *Machine) SetNetLink(nl base.NetLink)
func (self *Machine) UpdateLinks()

type Nicdevs

type Nicdevs struct {
	Dpdknics []Dpdknic `json:"nic_dev"`
	MTU      string    `json:"mtu"`

type Pci

type Pci struct {
	ID      string
	Path    string
	BusInfo string

	Writer base.Writer

func NewPci

func NewPci(ID, path, busInfo string) *Pci

func (*Pci) SetWriter

func (self *Pci) SetWriter(writer base.Writer)

func (*Pci) Unbind

func (self *Pci) Unbind() error

func (*Pci) UnbindPath

func (self *Pci) UnbindPath() string

type Pod

type Pod struct {
	Name    string `json:"name"`
	ID      string `json:"id"`
	K8sns   string `json:"k8sns"`
	Podtype string `json:"podtype"`

	LinkRepo *LinkRepository

	NameSpace base.NameSpace
	NetLink   base.NetLink

func NewPod

func NewPod(name string, id string, k8sns string, podType string) *Pod

func NewTempPod

func NewTempPod() *Pod

func (*Pod) AddLinkToContainer

func (self *Pod) AddLinkToContainer(container *Container, vfLink *Link, port *manager.Port, vmNs netns.NsHandle) error

func (*Pod) BindLinkByVethName

func (self *Pod) BindLinkByVethName(container *Container, port *manager.Port, vm *Machine, vethName string, vmNs netns.NsHandle) error
func (self *Pod) DeleteLink(container *Container, portMac string) error

func (*Pod) FindLinkByMac

func (self *Pod) FindLinkByMac(mac string) *Link
func (self *Pod) GetCurrentNsLinks() []netlink.Link

func (*Pod) SetNameSpace

func (self *Pod) SetNameSpace(ns base.NameSpace)
func (self *Pod) SetNetLink(netlink base.NetLink)
func (self *Pod) UpdateLinks()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL