runtime: add a new API for Network interface

Add GetEndpointsNum API for Network Interface to get the number of
network endpoints. This is used for caculate the number of pcie root
port for QemuVirt.

Signed-off-by: Jianyong Wu <jianyong.wu@arm.com>
This commit is contained in:
Jianyong Wu
2023-08-30 09:56:40 +00:00
parent d90d1c5c10
commit 28a41e1d16
3 changed files with 28 additions and 0 deletions

View File

@@ -226,6 +226,9 @@ type Network interface {
// SetEndpoints sets a sandbox's network endpoints.
SetEndpoints([]Endpoint)
// GetEndpoints number of sandbox's network endpoints.
GetEndpointsNum() (int, error)
}
func generateVCNetworkStructures(ctx context.Context, network Network) ([]*pbTypes.Interface, []*pbTypes.Route, []*pbTypes.ARPNeighbor, error) {

View File

@@ -92,6 +92,10 @@ func (n *DarwinNetwork) SetEndpoints(endpoints []Endpoint) {
n.eps = endpoints
}
func (n *DarwinNetwork) GetEndpointsNum() (int, error) {
return 0, nil
}
func validGuestRoute(route netlink.Route) bool {
return true
}

View File

@@ -291,6 +291,27 @@ func (n *LinuxNetwork) endpointAlreadyAdded(netInfo *NetworkInfo) bool {
return false
}
func (n *LinuxNetwork) GetEndpointsNum() (int, error) {
netnsHandle, err := netns.GetFromPath(n.netNSPath)
if err != nil {
return 0, err
}
defer netnsHandle.Close()
netlinkHandle, err := netlink.NewHandleAt(netnsHandle)
if err != nil {
return 0, err
}
defer netlinkHandle.Close()
linkList, err := netlinkHandle.LinkList()
if err != nil {
return 0, err
}
return len(linkList), nil
}
// Scan the networking namespace through netlink and then:
// 1. Create the endpoints for the relevant interfaces found there.
// 2. Attach them to the VM.