github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/libnetwork/drivers/ipvlan/ipvlan_network.go (about)

     1  package ipvlan
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Sirupsen/logrus"
     7  	"github.com/docker/docker/pkg/parsers/kernel"
     8  	"github.com/docker/docker/pkg/stringid"
     9  	"github.com/docker/libnetwork/driverapi"
    10  	"github.com/docker/libnetwork/netlabel"
    11  	"github.com/docker/libnetwork/options"
    12  	"github.com/docker/libnetwork/osl"
    13  	"github.com/docker/libnetwork/types"
    14  )
    15  
    16  // CreateNetwork the network for the specified driver type
    17  func (d *driver) CreateNetwork(nid string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
    18  	defer osl.InitOSContext()()
    19  	kv, err := kernel.GetKernelVersion()
    20  	if err != nil {
    21  		return fmt.Errorf("Failed to check kernel version for %s driver support: %v", ipvlanType, err)
    22  	}
    23  	// ensure Kernel version is >= v4.2 for ipvlan support
    24  	if kv.Kernel < ipvlanKernelVer || (kv.Kernel == ipvlanKernelVer && kv.Major < ipvlanMajorVer) {
    25  		return fmt.Errorf("kernel version failed to meet the minimum ipvlan kernel requirement of %d.%d, found %d.%d.%d",
    26  			ipvlanKernelVer, ipvlanMajorVer, kv.Kernel, kv.Major, kv.Minor)
    27  	}
    28  	// reject a null v4 network
    29  	if len(ipV4Data) == 0 || ipV4Data[0].Pool.String() == "0.0.0.0/0" {
    30  		return fmt.Errorf("ipv4 pool is empty")
    31  	}
    32  	// parse and validate the config and bind to networkConfiguration
    33  	config, err := parseNetworkOptions(nid, option)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	config.ID = nid
    38  	err = config.processIPAM(nid, ipV4Data, ipV6Data)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	// verify the ipvlan mode from -o ipvlan_mode option
    43  	switch config.IpvlanMode {
    44  	case "", modeL2:
    45  		// default to ipvlan L2 mode if -o ipvlan_mode is empty
    46  		config.IpvlanMode = modeL2
    47  	case modeL3:
    48  		config.IpvlanMode = modeL3
    49  	default:
    50  		return fmt.Errorf("requested ipvlan mode '%s' is not valid, 'l2' mode is the ipvlan driver default", config.IpvlanMode)
    51  	}
    52  	// loopback is not a valid parent link
    53  	if config.Parent == "lo" {
    54  		return fmt.Errorf("loopback interface is not a valid %s parent link", ipvlanType)
    55  	}
    56  	// if parent interface not specified, create a dummy type link to use named dummy+net_id
    57  	if config.Parent == "" {
    58  		config.Parent = getDummyName(stringid.TruncateID(config.ID))
    59  		// empty parent and --internal are handled the same. Set here to update k/v
    60  		config.Internal = true
    61  	}
    62  	err = d.createNetwork(config)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	// update persistent db, rollback on fail
    67  	err = d.storeUpdate(config)
    68  	if err != nil {
    69  		d.deleteNetwork(config.ID)
    70  		logrus.Debugf("encoutered an error rolling back a network create for %s : %v", config.ID, err)
    71  		return err
    72  	}
    73  
    74  	return nil
    75  }
    76  
    77  // createNetwork is used by new network callbacks and persistent network cache
    78  func (d *driver) createNetwork(config *configuration) error {
    79  	networkList := d.getNetworks()
    80  	for _, nw := range networkList {
    81  		if config.Parent == nw.config.Parent {
    82  			return fmt.Errorf("network %s is already using parent interface %s",
    83  				getDummyName(stringid.TruncateID(nw.config.ID)), config.Parent)
    84  		}
    85  	}
    86  	if !parentExists(config.Parent) {
    87  		// if the --internal flag is set, create a dummy link
    88  		if config.Internal {
    89  			err := createDummyLink(config.Parent, getDummyName(stringid.TruncateID(config.ID)))
    90  			if err != nil {
    91  				return err
    92  			}
    93  			config.CreatedSlaveLink = true
    94  			// notify the user in logs they have limited comunicatins
    95  			if config.Parent == getDummyName(stringid.TruncateID(config.ID)) {
    96  				logrus.Debugf("Empty -o parent= and --internal flags limit communications to other containers inside of network: %s",
    97  					config.Parent)
    98  			}
    99  		} else {
   100  			// if the subinterface parent_iface.vlan_id checks do not pass, return err.
   101  			//  a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10'
   102  			err := createVlanLink(config.Parent)
   103  			if err != nil {
   104  				return err
   105  			}
   106  			// if driver created the networks slave link, record it for future deletion
   107  			config.CreatedSlaveLink = true
   108  		}
   109  	}
   110  	n := &network{
   111  		id:        config.ID,
   112  		driver:    d,
   113  		endpoints: endpointTable{},
   114  		config:    config,
   115  	}
   116  	// add the *network
   117  	d.addNetwork(n)
   118  
   119  	return nil
   120  }
   121  
   122  // DeleteNetwork the network for the specified driver type
   123  func (d *driver) DeleteNetwork(nid string) error {
   124  	defer osl.InitOSContext()()
   125  	n := d.network(nid)
   126  	if n == nil {
   127  		return fmt.Errorf("network id %s not found", nid)
   128  	}
   129  	// if the driver created the slave interface, delete it, otherwise leave it
   130  	if ok := n.config.CreatedSlaveLink; ok {
   131  		// if the interface exists, only delete if it matches iface.vlan or dummy.net_id naming
   132  		if ok := parentExists(n.config.Parent); ok {
   133  			// only delete the link if it is named the net_id
   134  			if n.config.Parent == getDummyName(stringid.TruncateID(nid)) {
   135  				err := delDummyLink(n.config.Parent)
   136  				if err != nil {
   137  					logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v",
   138  						n.config.Parent, err)
   139  				}
   140  			} else {
   141  				// only delete the link if it matches iface.vlan naming
   142  				err := delVlanLink(n.config.Parent)
   143  				if err != nil {
   144  					logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v",
   145  						n.config.Parent, err)
   146  				}
   147  			}
   148  		}
   149  	}
   150  	// delete the *network
   151  	d.deleteNetwork(nid)
   152  	// delete the network record from persistent cache
   153  	err := d.storeDelete(n.config)
   154  	if err != nil {
   155  		return fmt.Errorf("error deleting deleting id %s from datastore: %v", nid, err)
   156  	}
   157  	return nil
   158  }
   159  
   160  // parseNetworkOptions parse docker network options
   161  func parseNetworkOptions(id string, option options.Generic) (*configuration, error) {
   162  	var (
   163  		err    error
   164  		config = &configuration{}
   165  	)
   166  	// parse generic labels first
   167  	if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
   168  		if config, err = parseNetworkGenericOptions(genData); err != nil {
   169  			return nil, err
   170  		}
   171  	}
   172  	// setting the parent to "" will trigger an isolated network dummy parent link
   173  	if _, ok := option[netlabel.Internal]; ok {
   174  		config.Internal = true
   175  		// empty --parent= and --internal are handled the same.
   176  		config.Parent = ""
   177  	}
   178  	return config, nil
   179  }
   180  
   181  // parseNetworkGenericOptions parse generic driver docker network options
   182  func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
   183  	var (
   184  		err    error
   185  		config *configuration
   186  	)
   187  	switch opt := data.(type) {
   188  	case *configuration:
   189  		config = opt
   190  	case map[string]string:
   191  		config = &configuration{}
   192  		err = config.fromOptions(opt)
   193  	case options.Generic:
   194  		var opaqueConfig interface{}
   195  		if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
   196  			config = opaqueConfig.(*configuration)
   197  		}
   198  	default:
   199  		err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
   200  	}
   201  	return config, err
   202  }
   203  
   204  // fromOptions binds the generic options to networkConfiguration to cache
   205  func (config *configuration) fromOptions(labels map[string]string) error {
   206  	for label, value := range labels {
   207  		switch label {
   208  		case parentOpt:
   209  			// parse driver option '-o parent'
   210  			config.Parent = value
   211  		case driverModeOpt:
   212  			// parse driver option '-o ipvlan_mode'
   213  			config.IpvlanMode = value
   214  		}
   215  	}
   216  	return nil
   217  }
   218  
   219  // processIPAM parses v4 and v6 IP information and binds it to the network configuration
   220  func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
   221  	if len(ipamV4Data) > 0 {
   222  		for _, ipd := range ipamV4Data {
   223  			s := &ipv4Subnet{
   224  				SubnetIP: ipd.Pool.String(),
   225  				GwIP:     ipd.Gateway.String(),
   226  			}
   227  			config.Ipv4Subnets = append(config.Ipv4Subnets, s)
   228  		}
   229  	}
   230  	if len(ipamV6Data) > 0 {
   231  		for _, ipd := range ipamV6Data {
   232  			s := &ipv6Subnet{
   233  				SubnetIP: ipd.Pool.String(),
   234  				GwIP:     ipd.Gateway.String(),
   235  			}
   236  			config.Ipv6Subnets = append(config.Ipv6Subnets, s)
   237  		}
   238  	}
   239  	return nil
   240  }