gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/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 persistapi "github.com/kata-containers/runtime/virtcontainers/persist/api" 12 ) 13 14 // Endpoint represents a physical or virtual network interface. 15 type Endpoint interface { 16 Properties() NetworkInfo 17 Name() string 18 HardwareAddr() string 19 Type() EndpointType 20 PciAddr() string 21 NetworkPair() *NetworkInterfacePair 22 23 SetProperties(NetworkInfo) 24 SetPciAddr(string) 25 Attach(hypervisor) error 26 Detach(netNsCreated bool, netNsPath string) error 27 HotAttach(h hypervisor) error 28 HotDetach(h hypervisor, netNsCreated bool, netNsPath string) error 29 30 save() persistapi.NetworkEndpoint 31 load(persistapi.NetworkEndpoint) 32 } 33 34 // EndpointType identifies the type of the network endpoint. 35 type EndpointType string 36 37 const ( 38 // PhysicalEndpointType is the physical network interface. 39 PhysicalEndpointType EndpointType = "physical" 40 41 // VethEndpointType is the virtual network interface. 42 VethEndpointType EndpointType = "virtual" 43 44 // VhostUserEndpointType is the vhostuser network interface. 45 VhostUserEndpointType EndpointType = "vhost-user" 46 47 // BridgedMacvlanEndpointType is macvlan network interface. 48 BridgedMacvlanEndpointType EndpointType = "macvlan" 49 50 // MacvtapEndpointType is macvtap network interface. 51 MacvtapEndpointType EndpointType = "macvtap" 52 53 // TapEndpointType is tap network interface. 54 TapEndpointType EndpointType = "tap" 55 56 // TuntapEndpointType is a tap network interface. 57 TuntapEndpointType EndpointType = "tuntap" 58 59 // IPVlanEndpointType is ipvlan network interface. 60 IPVlanEndpointType EndpointType = "ipvlan" 61 ) 62 63 // Set sets an endpoint type based on the input string. 64 func (endpointType *EndpointType) Set(value string) error { 65 switch value { 66 case "physical": 67 *endpointType = PhysicalEndpointType 68 return nil 69 case "virtual": 70 *endpointType = VethEndpointType 71 return nil 72 case "vhost-user": 73 *endpointType = VhostUserEndpointType 74 return nil 75 case "macvlan": 76 *endpointType = BridgedMacvlanEndpointType 77 return nil 78 case "macvtap": 79 *endpointType = MacvtapEndpointType 80 return nil 81 case "tap": 82 *endpointType = TapEndpointType 83 return nil 84 case "tuntap": 85 *endpointType = TuntapEndpointType 86 return nil 87 case "ipvlan": 88 *endpointType = IPVlanEndpointType 89 return nil 90 default: 91 return fmt.Errorf("Unknown endpoint type %s", value) 92 } 93 } 94 95 // String converts an endpoint type to a string. 96 func (endpointType *EndpointType) String() string { 97 switch *endpointType { 98 case PhysicalEndpointType: 99 return string(PhysicalEndpointType) 100 case VethEndpointType: 101 return string(VethEndpointType) 102 case VhostUserEndpointType: 103 return string(VhostUserEndpointType) 104 case BridgedMacvlanEndpointType: 105 return string(BridgedMacvlanEndpointType) 106 case MacvtapEndpointType: 107 return string(MacvtapEndpointType) 108 case TapEndpointType: 109 return string(TapEndpointType) 110 case TuntapEndpointType: 111 return string(TuntapEndpointType) 112 case IPVlanEndpointType: 113 return string(IPVlanEndpointType) 114 default: 115 return "" 116 } 117 } 118 119 func saveTapIf(tapif *TapInterface) *persistapi.TapInterface { 120 if tapif == nil { 121 return nil 122 } 123 124 return &persistapi.TapInterface{ 125 ID: tapif.ID, 126 Name: tapif.Name, 127 TAPIface: persistapi.NetworkInterface{ 128 Name: tapif.TAPIface.Name, 129 HardAddr: tapif.TAPIface.HardAddr, 130 Addrs: tapif.TAPIface.Addrs, 131 }, 132 } 133 } 134 135 func loadTapIf(tapif *persistapi.TapInterface) *TapInterface { 136 if tapif == nil { 137 return nil 138 } 139 140 return &TapInterface{ 141 ID: tapif.ID, 142 Name: tapif.Name, 143 TAPIface: NetworkInterface{ 144 Name: tapif.TAPIface.Name, 145 HardAddr: tapif.TAPIface.HardAddr, 146 Addrs: tapif.TAPIface.Addrs, 147 }, 148 } 149 } 150 151 func saveNetIfPair(pair *NetworkInterfacePair) *persistapi.NetworkInterfacePair { 152 if pair == nil { 153 return nil 154 } 155 156 epVirtIf := pair.VirtIface 157 158 tapif := saveTapIf(&pair.TapInterface) 159 160 virtif := persistapi.NetworkInterface{ 161 Name: epVirtIf.Name, 162 HardAddr: epVirtIf.HardAddr, 163 Addrs: epVirtIf.Addrs, 164 } 165 166 return &persistapi.NetworkInterfacePair{ 167 TapInterface: *tapif, 168 VirtIface: virtif, 169 NetInterworkingModel: int(pair.NetInterworkingModel), 170 } 171 } 172 173 func loadNetIfPair(pair *persistapi.NetworkInterfacePair) *NetworkInterfacePair { 174 if pair == nil { 175 return nil 176 } 177 178 savedVirtIf := pair.VirtIface 179 180 tapif := loadTapIf(&pair.TapInterface) 181 182 virtif := NetworkInterface{ 183 Name: savedVirtIf.Name, 184 HardAddr: savedVirtIf.HardAddr, 185 Addrs: savedVirtIf.Addrs, 186 } 187 188 return &NetworkInterfacePair{ 189 TapInterface: *tapif, 190 VirtIface: virtif, 191 NetInterworkingModel: NetInterworkingModel(pair.NetInterworkingModel), 192 } 193 } 194 195 func saveTuntapIf(tuntapif *TuntapInterface) *persistapi.TuntapInterface { 196 if tuntapif == nil { 197 return nil 198 } 199 200 return &persistapi.TuntapInterface{ 201 Name: tuntapif.Name, 202 TAPIface: persistapi.NetworkInterface{ 203 Name: tuntapif.TAPIface.Name, 204 HardAddr: tuntapif.TAPIface.HardAddr, 205 Addrs: tuntapif.TAPIface.Addrs, 206 }, 207 } 208 } 209 210 func loadTuntapIf(tuntapif *persistapi.TuntapInterface) *TuntapInterface { 211 if tuntapif == nil { 212 return nil 213 } 214 215 return &TuntapInterface{ 216 Name: tuntapif.Name, 217 TAPIface: NetworkInterface{ 218 Name: tuntapif.TAPIface.Name, 219 HardAddr: tuntapif.TAPIface.HardAddr, 220 Addrs: tuntapif.TAPIface.Addrs, 221 }, 222 } 223 }