Help us shape the future of Go! Take the 2026 Go Developer Survey

sriovnet

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 17 Imported by: 12

README

License Go Report Card Build Test Coverage Status

sriovnet

Go library to configure SRIOV networking devices

Local build and test

You can use go get command:

go get github.com/k8snetworkplumbingwg/sriovnet

Example:

package main

import (
    "fmt"

    "github.com/k8snetworkplumbingwg/sriovnet"
)

func main() {
	var vfList[10] *sriovnet.VfObj

	err1 := sriovnet.EnableSriov("ib0")
	if err1 != nil {
		return
	}

	handle, err2 := sriovnet.GetPfNetdevHandle("ib0")
	if err2 != nil {
		return
	}
	err3 := sriovnet.ConfigVfs(handle, false)
	if err3 != nil {
		return
	}
	for i := 0; i < 10; i++ {
		vfList[i], _ = sriovnet.AllocateVf(handle)
	}
	for _, vf := range handle.List {
		fmt.Printf("after allocation vf = %v\n", vf)
	}
	for i := 0; i < 10; i++ {
		if vfList[i] == nil {
			continue
		}
		sriovnet.FreeVf(handle, vfList[i])
	}
	for _, vf := range handle.List {
		fmt.Printf("after free vf = %v\n", vf)
	}
}

Documentation

Overview

Copyright 2023 NVIDIA CORPORATION &

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	NetSysDir = "/sys/class/net"
	PciSysDir = "/sys/bus/pci/devices"
	AuxSysDir = "/sys/bus/auxiliary/devices"
)
View Source
const (
	PORT_FLAVOUR_PHYSICAL = iota
	PORT_FLAVOUR_CPU
	PORT_FLAVOUR_DSA
	PORT_FLAVOUR_PCI_PF
	PORT_FLAVOUR_PCI_VF
	PORT_FLAVOUR_VIRTUAL
	PORT_FLAVOUR_UNUSED
	PORT_FLAVOUR_PCI_SF
	PORT_FLAVOUR_UNKNOWN = 0xffff
)

Keep things consistent with netlink lib constants nolint:revive

Variables

View Source
var (
	// ErrRepresentorNotFound is returned when a representor is not found.
	ErrRepresentorNotFound = errors.New("representor not found")
	// ErrMultipleRepresentorsFound is returned when multiple representors are found.
	ErrMultipleRepresentorsFound = errors.New("multiple representors found")
)
View Source
var (
	ErrDeviceNotFound = errors.New("device not found")
)

Functions

func BindVf

func BindVf(handle *PfNetdevHandle, vf *VfObj) error

func ConfigVfs

func ConfigVfs(handle *PfNetdevHandle, privileged bool) error

func DisableSriov

func DisableSriov(pfNetdevName string) error

func EnableSriov

func EnableSriov(pfNetdevName string) error

func FreeVf

func FreeVf(_ *PfNetdevHandle, vf *VfObj)

func FreeVfByNetdevName

func FreeVfByNetdevName(handle *PfNetdevHandle, vfIndex int) error

func GetAuxNetDevicesFromPci

func GetAuxNetDevicesFromPci(pciAddr string) ([]string, error)

GetAuxNetDevicesFromPci returns a list of auxiliary devices names for the specified PCI network device

func GetAuxSFDevByPciAndSFIndex added in v1.3.0

func GetAuxSFDevByPciAndSFIndex(pciAddress string, sfIndex uint32) (string, error)

GetAuxSFDevByPciAndSFIndex returns auxiliary SF device name which is associated with the given parent PCI address and SF index. returns error if an error occurred. returns ErrDeviceNotFound error if device is not found.

func GetDefaultPKeyFromPci added in v1.3.0

func GetDefaultPKeyFromPci(pciAddress string) (string, error)

GetDefaultPKeyFromPci returns the index0 PKey for the IB PCI device

func GetNetDevicesFromAux

func GetNetDevicesFromAux(auxDev string) ([]string, error)

GetNetDevicesFromAux gets auxiliary device name (e.g 'mlx5_core.sf.2') and returns the correlate netdevice

func GetNetDevicesFromPci

func GetNetDevicesFromPci(pciAddress string) ([]string, error)

GetNetDevicesFromPci gets a PCI address (e.g '0000:03:00.1') and returns the correlate list of netdevices

func GetPKeyByIndexFromPci added in v1.3.0

func GetPKeyByIndexFromPci(pciAddress string, index int) (string, error)

GetPKeyByIndexFromPci returns the PKey stored under given index for the IB PCI device

func GetPciFromNetDevice

func GetPciFromNetDevice(name string) (string, error)

GetPciFromNetDevice returns the PCI address associated with a network device name

func GetPfIndexByVfPciAddress added in v1.3.0

func GetPfIndexByVfPciAddress(vfPciAddress string) (int, error)

GetPfIndexByVfPciAddress gets the PF index that's associated with a VF PCI address (e.g '0000:03:00.4')

func GetPfPciFromAux

func GetPfPciFromAux(auxDev string) (string, error)

GetPfPciFromAux retrieves the parent PF PCI address of the provided auxiliary device in D.T.f format

func GetPfPciFromVfPci

func GetPfPciFromVfPci(vfPciAddress string) (string, error)

GetPfPciFromVfPci retrieves the parent PF PCI address of the provided VF PCI address in D:B:D.f format

func GetPfRepresentorDPU deprecated added in v1.3.0

func GetPfRepresentorDPU(pfID string) (string, error)

GetPfRepresentorDPU returns PF representor on DPU for a host PF identified by its ID.

Deprecated: use GetPfRepresentorFromPortParams instead.

func GetPfRepresentorFromPortParams added in v1.3.0

func GetPfRepresentorFromPortParams(pp *RepresentorPortParams) (string, error)

GetPfRepresentorFromPortParams returns the PF representor netdev name for a given port parameters

func GetPortIndexFromRepresentor

func GetPortIndexFromRepresentor(repNetDev string) (int, error)

GetPortIndexFromRepresentor finds the index of a representor from its network device name. Supports VF and SF. For multiple port flavors, the same ID could be returned, i.e.

pf0vf10 and pf0sf10

will return the same port ID. To further differentiate the ports, use GetRepresentorPortFlavour

func GetRepresentorPeerMacAddress

func GetRepresentorPeerMacAddress(netdev string) (net.HardwareAddr, error)

GetRepresentorPeerMacAddress returns the MAC address of the peer netdev associated with the given representor netdev Note:

This method functionality is currently supported only on DPUs.
Currently only netdev representors with PORT_FLAVOUR_PCI_PF are supported

func GetSfIndexByAuxDev

func GetSfIndexByAuxDev(auxDev string) (int, error)

GetSfIndexByAuxDev gets a SF device name (e.g 'mlx5_core.sf.2') and returns the correlate SF index.

func GetSfRepresentor

func GetSfRepresentor(uplink string, sfNum int) (string, error)

GetSfRepresentor returns the SF representor netdev name for a given uplink and sfIndex. uplink can be a PCI address or a netdev name. This function assumes that only a single local SF representor exists with the given sfNum for a given uplink.

func GetSfRepresentorDPU deprecated

func GetSfRepresentorDPU(pfID, sfIndex string) (string, error)

GetSfRepresentorDPU returns SF representor on DPU for a host SF identified by pfID and sfIndex

Deprecated: use GetSfRepresentorFromPortParams instead.

func GetSfRepresentorFromPortParams added in v1.3.0

func GetSfRepresentorFromPortParams(pp *RepresentorPortParams, sfIndex uint32) (string, error)

GetSfRepresentorFromPortParams returns the SF representor netdev name for a given port parameters and SF index

func GetUplinkRepresentor

func GetUplinkRepresentor(pciAddress string) (string, error)

GetUplinkRepresentor gets a VF or PF PCI address (e.g '0000:03:00.4') and returns the uplink represntor netdev name for that VF or PF.

func GetUplinkRepresentorFromAux

func GetUplinkRepresentorFromAux(auxDev string) (string, error)

GetUplinkRepresentorFromAux gets auxiliary device name (e.g 'mlx5_core.sf.2') and returns the uplink representor netdev name for device.

func GetVfDefaultMacAddr

func GetVfDefaultMacAddr(vfNetdevName string) (string, error)

func GetVfIndexByPciAddress

func GetVfIndexByPciAddress(vfPciAddress string) (int, error)

GetVfIndexByPciAddress gets a VF PCI address (e.g '0000:03:00.4') and returns the correlate VF index.

func GetVfNetdevName

func GetVfNetdevName(handle *PfNetdevHandle, vf *VfObj) string

func GetVfPciDevList

func GetVfPciDevList(pfNetdevName string) ([]string, error)

func GetVfRepresentor

func GetVfRepresentor(uplink string, vfIndex int) (string, error)

GetVfRepresentor returns the VF representor netdev name for a given uplink and vfIndex. uplink can be a PCI address or a netdev name. This function assumes that only a single local VF representor exists with the given vfIndex for a given uplink.

func GetVfRepresentorDPU deprecated

func GetVfRepresentorDPU(pfID, vfIndex string) (string, error)

GetVfRepresentorDPU returns VF representor on DPU for a host VF identified by pfID and vfIndex

Deprecated: use GetVfRepresentorFromPortParams instead.

func GetVfRepresentorFromPortParams added in v1.3.0

func GetVfRepresentorFromPortParams(pp *RepresentorPortParams, vfIndex uint32) (string, error)

GetVfRepresentorFromPortParams returns the VF representor netdev name for a given port parameters and VF index

func IsSriovEnabled

func IsSriovEnabled(netdevName string) bool

func IsSriovSupported

func IsSriovSupported(netdevName string) bool

func IsVfPciVfioBound

func IsVfPciVfioBound(pciAddr string) bool

func SetPFLinkUp

func SetPFLinkUp(pfNetdevName string) error

func SetRepresentorPeerMacAddress

func SetRepresentorPeerMacAddress(netdev string, mac net.HardwareAddr) error

SetRepresentorPeerMacAddress sets the given MAC addresss of the peer netdev associated with the given representor netdev. Note: This method functionality is currently supported only for DPUs. Currently only netdev representors with PORT_FLAVOUR_PCI_VF are supported

func SetVfDefaultGUID

func SetVfDefaultGUID(handle *PfNetdevHandle, vf *VfObj) error

func SetVfDefaultMacAddress

func SetVfDefaultMacAddress(handle *PfNetdevHandle, vf *VfObj) error

func SetVfPrivileged

func SetVfPrivileged(handle *PfNetdevHandle, vf *VfObj, privileged bool) error

func SetVfVlan

func SetVfVlan(handle *PfNetdevHandle, vf *VfObj, vlan int) error

func UnbindVf

func UnbindVf(handle *PfNetdevHandle, vf *VfObj) error

Types

type PfNetdevHandle

type PfNetdevHandle struct {
	PfNetdevName string

	List []*VfObj
	// contains filtered or unexported fields
}

func GetPfNetdevHandle

func GetPfNetdevHandle(pfNetdevName string) (*PfNetdevHandle, error)

type PortFlavour

type PortFlavour uint16

func GetRepresentorPortFlavour

func GetRepresentorPortFlavour(netdev string) (PortFlavour, error)

GetRepresentorPortFlavour returns the representor port flavour Note: this method does not support old representor names used by old kernels e.g <vf_num> and will return PORT_FLAVOUR_UNKNOWN for such cases.

type RepresentorPortParams added in v1.3.0

type RepresentorPortParams struct {
	// The PCI device address on which the representor is anchored
	ECPF string
	// The controller number
	ControllerNumber uint32
	// The PF number
	PFNumber uint16
}

RepresentorPortParams contains the base port parameters for locating representors.

func GetPFRepresentorPortParamsFromMAC added in v1.3.0

func GetPFRepresentorPortParamsFromMAC(mac net.HardwareAddr) (*RepresentorPortParams, error)

GetPFRepresentorPortParamsFromMAC returns the representor port parameters from the provided MAC address.

Note: This function will work properly only when MAC addresses are unique in the system. If multiple ports have the same MAC address, the function will return error.

func GetRepresentorPortParamsFromVUID added in v1.3.0

func GetRepresentorPortParamsFromVUID(vuid string) (*RepresentorPortParams, error)

GetRepresentorPortParamsFromVUID returns the representor port parameters from the provided VUID. a VUID is a unique identifier of a specific NIC function/vport for a given PF, the vuid may be extracted from the device PCI VPD (VU keyword) example: [VU] Vendor specific: e4092a71f9c1f0118000b45cb5355194MLNXS0D0F0

Note: this function relies on the doca_caps CLI tool to be present in the system. if the application is running in a container, the tool may be mounted from the host. to override the default doca_caps path set the SRIOVNET_DOCA_CAPS_BIN environment variable to the desired path.

type VfObj

type VfObj struct {
	Index      int
	PciAddress string
	Bound      bool
	Allocated  bool
}

func AllocateVf

func AllocateVf(handle *PfNetdevHandle) (*VfObj, error)

func AllocateVfByMacAddress

func AllocateVfByMacAddress(handle *PfNetdevHandle, vfMacAddress string) (*VfObj, error)

type VfObject

type VfObject struct {
	NetdevName string
	PCIDevName string
}

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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