github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/network/network.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package network
     5  
     6  import (
     7  	"fmt"
     8  	"net"
     9  	"sort"
    10  
    11  	"github.com/juju/loggo"
    12  )
    13  
    14  var logger = loggo.GetLogger("juju.network")
    15  
    16  // TODO(dimitern): Remove this once we use spaces as per the model.
    17  const (
    18  	// Id of the default public juju network
    19  	DefaultPublic = "juju-public"
    20  
    21  	// Id of the default private juju network
    22  	DefaultPrivate = "juju-private"
    23  )
    24  
    25  // Id defines a provider-specific network id.
    26  type Id string
    27  
    28  // SubnetInfo describes the bare minimum information for a subnet,
    29  // which the provider knows about but juju might not yet.
    30  type SubnetInfo struct {
    31  	// CIDR of the network, in 123.45.67.89/24 format. Can be empty if
    32  	// unknown.
    33  	CIDR string
    34  
    35  	// ProviderId is a provider-specific network id. This the only
    36  	// required field.
    37  	ProviderId Id
    38  
    39  	// VLANTag needs to be between 1 and 4094 for VLANs and 0 for
    40  	// normal networks. It's defined by IEEE 802.1Q standard, and used
    41  	// to define a VLAN network. For more information, see:
    42  	// http://en.wikipedia.org/wiki/IEEE_802.1Q.
    43  	VLANTag int
    44  
    45  	// AllocatableIPLow and AllocatableIPHigh describe the allocatable
    46  	// portion of the subnet. The provider will only permit allocation
    47  	// between these limits. If they are empty then none of the subnet is
    48  	// allocatable.
    49  	AllocatableIPLow  net.IP
    50  	AllocatableIPHigh net.IP
    51  }
    52  
    53  // InterfaceConfigType defines valid network interface configuration
    54  // types. See interfaces(5) for details
    55  type InterfaceConfigType string
    56  
    57  const (
    58  	ConfigUnknown InterfaceConfigType = ""
    59  	ConfigDHCP    InterfaceConfigType = "dhcp"
    60  	ConfigStatic  InterfaceConfigType = "static"
    61  	ConfigManual  InterfaceConfigType = "manual"
    62  	// add others when needed
    63  )
    64  
    65  // InterfaceInfo describes a single network interface available on an
    66  // instance. For providers that support networks, this will be
    67  // available at StartInstance() time.
    68  type InterfaceInfo struct {
    69  	// DeviceIndex specifies the order in which the network interface
    70  	// appears on the host. The primary interface has an index of 0.
    71  	DeviceIndex int
    72  
    73  	// MACAddress is the network interface's hardware MAC address
    74  	// (e.g. "aa:bb:cc:dd:ee:ff").
    75  	MACAddress string
    76  
    77  	// CIDR of the network, in 123.45.67.89/24 format.
    78  	CIDR string
    79  
    80  	// NetworkName is juju-internal name of the network.
    81  	NetworkName string
    82  
    83  	// ProviderId is a provider-specific NIC id.
    84  	ProviderId Id
    85  
    86  	// ProviderSubnetId is the provider-specific id for the associated
    87  	// subnet.
    88  	ProviderSubnetId Id
    89  
    90  	// VLANTag needs to be between 1 and 4094 for VLANs and 0 for
    91  	// normal networks. It's defined by IEEE 802.1Q standard.
    92  	VLANTag int
    93  
    94  	// InterfaceName is the raw OS-specific network device name (e.g.
    95  	// "eth1", even for a VLAN eth1.42 virtual interface).
    96  	InterfaceName string
    97  
    98  	// Disabled is true when the interface needs to be disabled on the
    99  	// machine, e.g. not to configure it.
   100  	Disabled bool
   101  
   102  	// NoAutoStart is true when the interface should not be configured
   103  	// to start automatically on boot. By default and for
   104  	// backwards-compatibility, interfaces are configured to
   105  	// auto-start.
   106  	NoAutoStart bool
   107  
   108  	// ConfigType determines whether the interface should be
   109  	// configured via DHCP, statically, manually, etc. See
   110  	// interfaces(5) for more information.
   111  	ConfigType InterfaceConfigType
   112  
   113  	// Address contains an optional static IP address to configure for
   114  	// this network interface. The subnet mask to set will be inferred
   115  	// from the CIDR value.
   116  	Address Address
   117  
   118  	// DNSServers contains an optional list of IP addresses and/or
   119  	// hostnames to configure as DNS servers for this network
   120  	// interface.
   121  	DNSServers []Address
   122  
   123  	// Gateway address, if set, defines the default gateway to
   124  	// configure for this network interface. For containers this
   125  	// usually is (one of) the host address(es).
   126  	GatewayAddress Address
   127  
   128  	// ExtraConfig can contain any valid setting and its value allowed
   129  	// inside an "iface" section of a interfaces(5) config file, e.g.
   130  	// "up", "down", "mtu", etc.
   131  	ExtraConfig map[string]string
   132  }
   133  
   134  type interfaceInfoSlice []InterfaceInfo
   135  
   136  func (s interfaceInfoSlice) Len() int      { return len(s) }
   137  func (s interfaceInfoSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
   138  func (s interfaceInfoSlice) Less(i, j int) bool {
   139  	iface1 := s[i]
   140  	iface2 := s[j]
   141  	return iface1.DeviceIndex < iface2.DeviceIndex
   142  }
   143  
   144  // SortInterfaceInfo sorts a slice of InterfaceInfo on DeviceIndex in ascending
   145  // order.
   146  func SortInterfaceInfo(interfaces []InterfaceInfo) {
   147  	sort.Sort(interfaceInfoSlice(interfaces))
   148  }
   149  
   150  // ActualInterfaceName returns raw interface name for raw interface (e.g. "eth0") and
   151  // virtual interface name for virtual interface (e.g. "eth0.42")
   152  func (i *InterfaceInfo) ActualInterfaceName() string {
   153  	if i.VLANTag > 0 {
   154  		return fmt.Sprintf("%s.%d", i.InterfaceName, i.VLANTag)
   155  	}
   156  	return i.InterfaceName
   157  }
   158  
   159  // IsVirtual returns true when the interface is a virtual device, as
   160  // opposed to a physical device (e.g. a VLAN or a network alias)
   161  func (i *InterfaceInfo) IsVirtual() bool {
   162  	return i.VLANTag > 0
   163  }
   164  
   165  // IsVLAN returns true when the interface is a VLAN interface.
   166  func (i *InterfaceInfo) IsVLAN() bool {
   167  	return i.VLANTag > 0
   168  }
   169  
   170  // PreferIPv6Getter will be implemented by both the environment and agent
   171  // config.
   172  type PreferIPv6Getter interface {
   173  	PreferIPv6() bool
   174  }
   175  
   176  // InitializeFromConfig needs to be called once after the environment
   177  // or agent configuration is available to configure networking
   178  // settings.
   179  func InitializeFromConfig(config PreferIPv6Getter) {
   180  	globalPreferIPv6 = config.PreferIPv6()
   181  	logger.Infof("setting prefer-ipv6 to %v", globalPreferIPv6)
   182  }