Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
persist: merge "network.json"
Browse files Browse the repository at this point in the history
Merge "network.json" into "persist.json" so that new store can manage
network part.

Signed-off-by: Wei Zhang <zhangwei555@huawei.com>
  • Loading branch information
WeiZhang555 committed Jul 22, 2019
1 parent 84bc9a2 commit e747833
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 38 deletions.
50 changes: 50 additions & 0 deletions virtcontainers/bridgedmacvlan_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"

"github.com/containernetworking/plugins/pkg/ns"
persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
)

// BridgedMacvlanEndpoint represents a macvlan endpoint that is bridged to the VM
Expand Down Expand Up @@ -115,3 +116,52 @@ func (endpoint *BridgedMacvlanEndpoint) HotAttach(h hypervisor) error {
func (endpoint *BridgedMacvlanEndpoint) HotDetach(h hypervisor, netNsCreated bool, netNsPath string) error {
return fmt.Errorf("BridgedMacvlanEndpoint does not support Hot detach")
}

func (endpoint *BridgedMacvlanEndpoint) save() (s persistapi.NetworkEndpoint) {
s.Type = string(endpoint.Type())
s.BridgedMacvlan = &persistapi.BridgedMacvlanEndpoint{
NetPair: persistapi.NetworkInterfacePair{
TapInterface: persistapi.TapInterface{
ID: endpoint.NetPair.TapInterface.ID,
Name: endpoint.NetPair.TapInterface.Name,
TAPIface: persistapi.NetworkInterface{
Name: endpoint.NetPair.TapInterface.TAPIface.Name,
HardAddr: endpoint.NetPair.TapInterface.TAPIface.HardAddr,
Addrs: endpoint.NetPair.TapInterface.TAPIface.Addrs,
},
},
VirtIface: persistapi.NetworkInterface{
Name: endpoint.NetPair.VirtIface.Name,
HardAddr: endpoint.NetPair.VirtIface.HardAddr,
Addrs: endpoint.NetPair.VirtIface.Addrs,
},
NetInterworkingModel: int(endpoint.NetPair.NetInterworkingModel),
},
}
return
}

func (endpoint *BridgedMacvlanEndpoint) load(s persistapi.NetworkEndpoint) {
endpoint.EndpointType = BridgedMacvlanEndpointType

if s.BridgedMacvlan != nil {
iface := s.BridgedMacvlan
endpoint.NetPair = NetworkInterfacePair{
TapInterface: TapInterface{
ID: iface.NetPair.TapInterface.ID,
Name: iface.NetPair.TapInterface.Name,
TAPIface: NetworkInterface{
Name: iface.NetPair.TapInterface.TAPIface.Name,
HardAddr: iface.NetPair.TapInterface.TAPIface.HardAddr,
Addrs: iface.NetPair.TapInterface.TAPIface.Addrs,
},
},
VirtIface: NetworkInterface{
Name: iface.NetPair.VirtIface.Name,
HardAddr: iface.NetPair.VirtIface.HardAddr,
Addrs: iface.NetPair.VirtIface.Addrs,
},
NetInterworkingModel: NetInterworkingModel(iface.NetPair.NetInterworkingModel),
}
}
}
5 changes: 5 additions & 0 deletions virtcontainers/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package virtcontainers

import (
"fmt"

persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
)

// Endpoint represents a physical or virtual network interface.
Expand All @@ -24,6 +26,9 @@ type Endpoint interface {
Detach(netNsCreated bool, netNsPath string) error
HotAttach(h hypervisor) error
HotDetach(h hypervisor, netNsCreated bool, netNsPath string) error

save() persistapi.NetworkEndpoint
load(persistapi.NetworkEndpoint)
}

// EndpointType identifies the type of the network endpoint.
Expand Down
10 changes: 6 additions & 4 deletions virtcontainers/fc.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,13 @@ func (fc *firecracker) fcInit(timeout int) error {

// Fetch sandbox network to be able to access it from the sandbox structure.
var networkNS NetworkNamespace
if err := fc.store.Load(store.Network, &networkNS); err == nil {
if networkNS.NetNsPath == "" {
fc.Logger().WithField("NETWORK NAMESPACE NULL", networkNS).Warn()
if fc.store != nil {
if err := fc.store.Load(store.Network, &networkNS); err == nil {
if networkNS.NetNsPath == "" {
fc.Logger().WithField("NETWORK NAMESPACE NULL", networkNS).Warn()
}
fc.netNSPath = networkNS.NetNsPath
}
fc.netNSPath = networkNS.NetNsPath
}

err := os.MkdirAll(fc.jailerRoot, store.DirMode)
Expand Down
50 changes: 50 additions & 0 deletions virtcontainers/ipvlan_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"

"github.com/containernetworking/plugins/pkg/ns"
persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
)

// IPVlanEndpoint represents a ipvlan endpoint that is bridged to the VM
Expand Down Expand Up @@ -118,3 +119,52 @@ func (endpoint *IPVlanEndpoint) HotAttach(h hypervisor) error {
func (endpoint *IPVlanEndpoint) HotDetach(h hypervisor, netNsCreated bool, netNsPath string) error {
return fmt.Errorf("IPVlanEndpoint does not support Hot detach")
}

func (endpoint *IPVlanEndpoint) save() (s persistapi.NetworkEndpoint) {
s.Type = string(endpoint.Type())
s.IPVlan = &persistapi.IPVlanEndpoint{
NetPair: persistapi.NetworkInterfacePair{
TapInterface: persistapi.TapInterface{
ID: endpoint.NetPair.TapInterface.ID,
Name: endpoint.NetPair.TapInterface.Name,
TAPIface: persistapi.NetworkInterface{
Name: endpoint.NetPair.TapInterface.TAPIface.Name,
HardAddr: endpoint.NetPair.TapInterface.TAPIface.HardAddr,
Addrs: endpoint.NetPair.TapInterface.TAPIface.Addrs,
},
},
VirtIface: persistapi.NetworkInterface{
Name: endpoint.NetPair.VirtIface.Name,
HardAddr: endpoint.NetPair.VirtIface.HardAddr,
Addrs: endpoint.NetPair.VirtIface.Addrs,
},
NetInterworkingModel: int(endpoint.NetPair.NetInterworkingModel),
},
}
return
}

func (endpoint *IPVlanEndpoint) load(s persistapi.NetworkEndpoint) {
endpoint.EndpointType = IPVlanEndpointType

if s.IPVlan != nil {
iface := s.IPVlan
endpoint.NetPair = NetworkInterfacePair{
TapInterface: TapInterface{
ID: iface.NetPair.TapInterface.ID,
Name: iface.NetPair.TapInterface.Name,
TAPIface: NetworkInterface{
Name: iface.NetPair.TapInterface.TAPIface.Name,
HardAddr: iface.NetPair.TapInterface.TAPIface.HardAddr,
Addrs: iface.NetPair.TapInterface.TAPIface.Addrs,
},
},
VirtIface: NetworkInterface{
Name: iface.NetPair.VirtIface.Name,
HardAddr: iface.NetPair.VirtIface.HardAddr,
Addrs: iface.NetPair.VirtIface.Addrs,
},
NetInterworkingModel: NetInterworkingModel(iface.NetPair.NetInterworkingModel),
}
}
}
16 changes: 16 additions & 0 deletions virtcontainers/macvtap_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package virtcontainers
import (
"fmt"
"os"

persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
)

// MacvtapEndpoint represents a macvtap endpoint
Expand Down Expand Up @@ -102,3 +104,17 @@ func (endpoint *MacvtapEndpoint) SetPciAddr(pciAddr string) {
func (endpoint *MacvtapEndpoint) NetworkPair() *NetworkInterfacePair {
return nil
}

func (endpoint *MacvtapEndpoint) save() (s persistapi.NetworkEndpoint) {
s.Type = string(endpoint.Type())
s.Macvtap = &persistapi.MacvtapEndpoint{
PCIAddr: endpoint.PCIAddr,
}
return
}
func (endpoint *MacvtapEndpoint) load(s persistapi.NetworkEndpoint) {
endpoint.EndpointType = MacvtapEndpointType
if s.Macvtap != nil {
endpoint.PCIAddr = s.Macvtap.PCIAddr
}
}
45 changes: 45 additions & 0 deletions virtcontainers/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ func (s *Sandbox) dumpAgent(ss *persistapi.SandboxState) {
}
}

func (s *Sandbox) dumpNetwork(ss *persistapi.SandboxState) {
ss.Network = persistapi.NetworkInfo{
NetNsPath: s.networkNS.NetNsPath,
NetmonPID: s.networkNS.NetmonPID,
NetNsCreated: s.networkNS.NetNsCreated,
}
for _, e := range s.networkNS.Endpoints {
ss.Network.Endpoints = append(ss.Network.Endpoints, e.save())
}
}

func (s *Sandbox) Save() error {
var (
ss = persistapi.SandboxState{}
Expand All @@ -173,6 +184,7 @@ func (s *Sandbox) Save() error {
s.dumpProcess(cs)
s.dumpMounts(cs)
s.dumpAgent(&ss)
s.dumpNetwork(&ss)

if err := s.newStore.ToDisk(ss, cs); err != nil {
return err
Expand Down Expand Up @@ -248,6 +260,38 @@ func (c *Container) loadContProcess(cs persistapi.ContainerState) {
}
}

func (s *Sandbox) loadNetwork(netInfo persistapi.NetworkInfo) {
s.networkNS = NetworkNamespace{
NetNsPath: netInfo.NetNsPath,
NetmonPID: netInfo.NetmonPID,
NetNsCreated: netInfo.NetNsCreated,
}

for _, e := range netInfo.Endpoints {
var ep Endpoint
switch EndpointType(e.Type) {
case PhysicalEndpointType:
ep = &PhysicalEndpoint{}
case VethEndpointType:
ep = &VethEndpoint{}
case VhostUserEndpointType:
ep = &VhostUserEndpoint{}
case BridgedMacvlanEndpointType:
ep = &BridgedMacvlanEndpoint{}
case MacvtapEndpointType:
ep = &MacvtapEndpoint{}
case TapEndpointType:
ep = &TapEndpoint{}
case IPVlanEndpointType:
ep = &IPVlanEndpoint{}
default:
continue
}
ep.load(e)
s.networkNS.Endpoints = append(s.networkNS.Endpoints, ep)
}
}

// Restore will restore sandbox data from persist file on disk
func (s *Sandbox) Restore() error {
ss, _, err := s.newStore.FromDisk(s.id)
Expand All @@ -259,6 +303,7 @@ func (s *Sandbox) Restore() error {
s.loadHypervisor(ss.HypervisorState)
s.loadDevices(ss.Devices)
s.loadAgent(ss.AgentState)
s.loadNetwork(ss.Network)
return nil
}

Expand Down
85 changes: 72 additions & 13 deletions virtcontainers/persist/api/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,86 @@

package persistapi

import (
"github.com/vishvananda/netlink"
)

// ============= sandbox level resources =============

type NetworkInterface struct {
Name string
HardAddr string
Addrs []netlink.Addr
}

// TapInterface defines a tap interface
type TapInterface struct {
ID string
Name string
TAPIface NetworkInterface
// remove VMFds and VhostFds
}

// NetworkInterfacePair defines a pair between VM and virtual network interfaces.
type NetworkInterfacePair struct {
TapInterface
VirtIface NetworkInterface
NetInterworkingModel int
}

type PhysicalEndpoint struct {
BDF string
Driver string
VendorDeviceID string
}

type MacvtapEndpoint struct {
// This is for showing information.
// Remove this field won't impact anything.
PCIAddr string
}

type TapEndpoint struct {
TapInterface TapInterface
}

type BridgedMacvlanEndpoint struct {
NetPair NetworkInterfacePair
}

type VethEndpoint struct {
NetPair NetworkInterfacePair
}

type IPVlanEndpoint struct {
NetPair NetworkInterfacePair
}

type VhostUserEndpoint struct {
// This is for showing information.
// Remove these fields won't impact anything.
IfaceName string
PCIAddr string
}

// NetworkEndpoint contains network interface information
type NetworkEndpoint struct {
Type string

// ID used to pass the netdev option to qemu
ID string

// Name of the interface
Name string

// Index of interface
Index int
// One and only one of these below are not nil according to Type.
Physical *PhysicalEndpoint `json:",omitempty"`
Veth *VethEndpoint `json:",omitempty"`
VhostUser *VhostUserEndpoint `json:",omitempty"`
BridgedMacvlan *BridgedMacvlanEndpoint `json:",omitempty"`
Macvtap *MacvtapEndpoint `json:",omitempty"`
Tap *TapEndpoint `json:",omitempty"`
IPVlan *IPVlanEndpoint `json:",omitempty"`
}

// NetworkInfo contains network information of sandbox
type NetworkInfo struct {
NetNsPath string
NetmonPID int
NetNsCreated bool
InterworkingModel string
Endpoints []NetworkEndpoint
NetNsPath string
NetmonPID int
NetNsCreated bool
Endpoints []NetworkEndpoint
}
20 changes: 20 additions & 0 deletions virtcontainers/physical_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/kata-containers/runtime/virtcontainers/device/config"
"github.com/kata-containers/runtime/virtcontainers/device/drivers"
persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api"
"github.com/safchain/ethtool"
)

Expand Down Expand Up @@ -200,3 +201,22 @@ func bindNICToVFIO(endpoint *PhysicalEndpoint) error {
func bindNICToHost(endpoint *PhysicalEndpoint) error {
return drivers.BindDevicetoHost(endpoint.BDF, endpoint.Driver, endpoint.VendorDeviceID)
}

func (endpoint *PhysicalEndpoint) save() (s persistapi.NetworkEndpoint) {
s.Type = string(endpoint.Type())
s.Physical = &persistapi.PhysicalEndpoint{
BDF: endpoint.BDF,
Driver: endpoint.Driver,
VendorDeviceID: endpoint.VendorDeviceID,
}
return
}

func (endpoint *PhysicalEndpoint) load(s persistapi.NetworkEndpoint) {
endpoint.EndpointType = PhysicalEndpointType
if s.Physical != nil {
endpoint.BDF = s.Physical.BDF
endpoint.Driver = s.Physical.Driver
endpoint.VendorDeviceID = s.Physical.VendorDeviceID
}
}
Loading

0 comments on commit e747833

Please sign in to comment.