gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/bridgedmacvlan_endpoint.go (about) 1 // Copyright (c) 2018 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 6 package virtcontainers 7 8 import ( 9 "fmt" 10 11 "github.com/containernetworking/plugins/pkg/ns" 12 persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" 13 ) 14 15 // BridgedMacvlanEndpoint represents a macvlan endpoint that is bridged to the VM 16 type BridgedMacvlanEndpoint struct { 17 NetPair NetworkInterfacePair 18 EndpointProperties NetworkInfo 19 EndpointType EndpointType 20 PCIAddr string 21 } 22 23 func createBridgedMacvlanNetworkEndpoint(idx int, ifName string, interworkingModel NetInterworkingModel) (*BridgedMacvlanEndpoint, error) { 24 if idx < 0 { 25 return &BridgedMacvlanEndpoint{}, fmt.Errorf("invalid network endpoint index: %d", idx) 26 } 27 28 netPair, err := createNetworkInterfacePair(idx, ifName, interworkingModel) 29 if err != nil { 30 return nil, err 31 } 32 33 endpoint := &BridgedMacvlanEndpoint{ 34 NetPair: netPair, 35 EndpointType: BridgedMacvlanEndpointType, 36 } 37 if ifName != "" { 38 endpoint.NetPair.VirtIface.Name = ifName 39 } 40 41 return endpoint, nil 42 } 43 44 // Properties returns properties of the interface. 45 func (endpoint *BridgedMacvlanEndpoint) Properties() NetworkInfo { 46 return endpoint.EndpointProperties 47 } 48 49 // Name returns name of the veth interface in the network pair. 50 func (endpoint *BridgedMacvlanEndpoint) Name() string { 51 return endpoint.NetPair.VirtIface.Name 52 } 53 54 // HardwareAddr returns the mac address that is assigned to the tap interface 55 // in th network pair. 56 func (endpoint *BridgedMacvlanEndpoint) HardwareAddr() string { 57 return endpoint.NetPair.TAPIface.HardAddr 58 } 59 60 // Type identifies the endpoint as a virtual endpoint. 61 func (endpoint *BridgedMacvlanEndpoint) Type() EndpointType { 62 return endpoint.EndpointType 63 } 64 65 // SetProperties sets the properties for the endpoint. 66 func (endpoint *BridgedMacvlanEndpoint) SetProperties(properties NetworkInfo) { 67 endpoint.EndpointProperties = properties 68 } 69 70 // PciAddr returns the PCI address of the endpoint. 71 func (endpoint *BridgedMacvlanEndpoint) PciAddr() string { 72 return endpoint.PCIAddr 73 } 74 75 // SetPciAddr sets the PCI address of the endpoint. 76 func (endpoint *BridgedMacvlanEndpoint) SetPciAddr(pciAddr string) { 77 endpoint.PCIAddr = pciAddr 78 } 79 80 // NetworkPair returns the network pair of the endpoint. 81 func (endpoint *BridgedMacvlanEndpoint) NetworkPair() *NetworkInterfacePair { 82 return &endpoint.NetPair 83 } 84 85 // Attach for virtual endpoint bridges the network pair and adds the 86 // tap interface of the network pair to the hypervisor. 87 func (endpoint *BridgedMacvlanEndpoint) Attach(h hypervisor) error { 88 if err := xConnectVMNetwork(endpoint, h); err != nil { 89 networkLogger().WithError(err).Error("Error bridging virtual ep") 90 return err 91 } 92 93 return h.addDevice(endpoint, netDev) 94 } 95 96 // Detach for the virtual endpoint tears down the tap and bridge 97 // created for the veth interface. 98 func (endpoint *BridgedMacvlanEndpoint) Detach(netNsCreated bool, netNsPath string) error { 99 // The network namespace would have been deleted at this point 100 // if it has not been created by virtcontainers. 101 if !netNsCreated { 102 return nil 103 } 104 105 return doNetNS(netNsPath, func(_ ns.NetNS) error { 106 return xDisconnectVMNetwork(endpoint) 107 }) 108 } 109 110 // HotAttach for physical endpoint not supported yet 111 func (endpoint *BridgedMacvlanEndpoint) HotAttach(h hypervisor) error { 112 return fmt.Errorf("BridgedMacvlanEndpoint does not support Hot attach") 113 } 114 115 // HotDetach for physical endpoint not supported yet 116 func (endpoint *BridgedMacvlanEndpoint) HotDetach(h hypervisor, netNsCreated bool, netNsPath string) error { 117 return fmt.Errorf("BridgedMacvlanEndpoint does not support Hot detach") 118 } 119 120 func (endpoint *BridgedMacvlanEndpoint) save() persistapi.NetworkEndpoint { 121 netpair := saveNetIfPair(&endpoint.NetPair) 122 123 return persistapi.NetworkEndpoint{ 124 Type: string(endpoint.Type()), 125 BridgedMacvlan: &persistapi.BridgedMacvlanEndpoint{ 126 NetPair: *netpair, 127 }, 128 } 129 } 130 131 func (endpoint *BridgedMacvlanEndpoint) load(s persistapi.NetworkEndpoint) { 132 endpoint.EndpointType = BridgedMacvlanEndpointType 133 134 if s.BridgedMacvlan != nil { 135 netpair := loadNetIfPair(&s.BridgedMacvlan.NetPair) 136 endpoint.NetPair = *netpair 137 } 138 }