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