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