- Notes at: https://gist.github.com/nerdalert/c0363c15d20986633fda
Signed-off-by: Brent Salisbury <brent@docker.com>
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,86 @@ |
| 0 |
+package ipvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "net" |
|
| 4 |
+ "sync" |
|
| 5 |
+ |
|
| 6 |
+ "github.com/Sirupsen/logrus" |
|
| 7 |
+ "github.com/docker/libnetwork/datastore" |
|
| 8 |
+ "github.com/docker/libnetwork/discoverapi" |
|
| 9 |
+ "github.com/docker/libnetwork/driverapi" |
|
| 10 |
+ "github.com/docker/libnetwork/osl" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+const ( |
|
| 14 |
+ vethLen = 7 |
|
| 15 |
+ containerVethPrefix = "eth" |
|
| 16 |
+ vethPrefix = "veth" |
|
| 17 |
+ ipvlanType = "ipvlan" // driver type name |
|
| 18 |
+ modeL2 = "l2" // ipvlan mode l2 is the default |
|
| 19 |
+ modeL3 = "l3" // ipvlan L3 mode |
|
| 20 |
+ hostIfaceOpt = "host_iface" // host interface -o host_iface |
|
| 21 |
+ modeOpt = "_mode" // ipvlan mode ux opt suffix |
|
| 22 |
+) |
|
| 23 |
+ |
|
| 24 |
+var driverModeOpt = ipvlanType + modeOpt // mode --option ipvlan_mode |
|
| 25 |
+ |
|
| 26 |
+type endpointTable map[string]*endpoint |
|
| 27 |
+ |
|
| 28 |
+type networkTable map[string]*network |
|
| 29 |
+ |
|
| 30 |
+type driver struct {
|
|
| 31 |
+ networks networkTable |
|
| 32 |
+ sync.Once |
|
| 33 |
+ sync.Mutex |
|
| 34 |
+ store datastore.DataStore |
|
| 35 |
+} |
|
| 36 |
+ |
|
| 37 |
+type endpoint struct {
|
|
| 38 |
+ id string |
|
| 39 |
+ mac net.HardwareAddr |
|
| 40 |
+ addr *net.IPNet |
|
| 41 |
+ addrv6 *net.IPNet |
|
| 42 |
+ srcName string |
|
| 43 |
+} |
|
| 44 |
+ |
|
| 45 |
+type network struct {
|
|
| 46 |
+ id string |
|
| 47 |
+ sbox osl.Sandbox |
|
| 48 |
+ endpoints endpointTable |
|
| 49 |
+ driver *driver |
|
| 50 |
+ config *configuration |
|
| 51 |
+ sync.Mutex |
|
| 52 |
+} |
|
| 53 |
+ |
|
| 54 |
+// Init initializes and registers the libnetwork ipvlan driver |
|
| 55 |
+func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
|
|
| 56 |
+ if err := kernelSupport(ipvlanType); err != nil {
|
|
| 57 |
+ logrus.Warnf("encountered issues loading the ipvlan kernel module: %v", err)
|
|
| 58 |
+ } |
|
| 59 |
+ c := driverapi.Capability{
|
|
| 60 |
+ DataScope: datastore.LocalScope, |
|
| 61 |
+ } |
|
| 62 |
+ d := &driver{
|
|
| 63 |
+ networks: networkTable{},
|
|
| 64 |
+ } |
|
| 65 |
+ d.initStore(config) |
|
| 66 |
+ return dc.RegisterDriver(ipvlanType, d, c) |
|
| 67 |
+} |
|
| 68 |
+ |
|
| 69 |
+func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
|
|
| 70 |
+ return make(map[string]interface{}, 0), nil
|
|
| 71 |
+} |
|
| 72 |
+ |
|
| 73 |
+func (d *driver) Type() string {
|
|
| 74 |
+ return ipvlanType |
|
| 75 |
+} |
|
| 76 |
+ |
|
| 77 |
+// DiscoverNew is a notification for a new discovery event. |
|
| 78 |
+func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
|
|
| 79 |
+ return nil |
|
| 80 |
+} |
|
| 81 |
+ |
|
| 82 |
+// DiscoverDelete is a notification for a discovery delete event. |
|
| 83 |
+func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
|
|
| 84 |
+ return nil |
|
| 85 |
+} |
| 0 | 86 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,80 @@ |
| 0 |
+package ipvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/docker/libnetwork/driverapi" |
|
| 6 |
+ "github.com/docker/libnetwork/netlabel" |
|
| 7 |
+ "github.com/docker/libnetwork/netutils" |
|
| 8 |
+ "github.com/docker/libnetwork/types" |
|
| 9 |
+ "github.com/vishvananda/netlink" |
|
| 10 |
+) |
|
| 11 |
+ |
|
| 12 |
+// CreateEndpoint assigns the mac, ip and endpoint id for the new container |
|
| 13 |
+func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, |
|
| 14 |
+ epOptions map[string]interface{}) error {
|
|
| 15 |
+ |
|
| 16 |
+ if err := validateID(nid, eid); err != nil {
|
|
| 17 |
+ return err |
|
| 18 |
+ } |
|
| 19 |
+ n, err := d.getNetwork(nid) |
|
| 20 |
+ if err != nil {
|
|
| 21 |
+ return fmt.Errorf("network id %q not found", nid)
|
|
| 22 |
+ } |
|
| 23 |
+ if ifInfo.MacAddress() != nil {
|
|
| 24 |
+ return fmt.Errorf("%s interfaces do not support custom mac address assigment", ipvlanType)
|
|
| 25 |
+ } |
|
| 26 |
+ ep := &endpoint{
|
|
| 27 |
+ id: eid, |
|
| 28 |
+ addr: ifInfo.Address(), |
|
| 29 |
+ addrv6: ifInfo.AddressIPv6(), |
|
| 30 |
+ mac: ifInfo.MacAddress(), |
|
| 31 |
+ } |
|
| 32 |
+ if ep.addr == nil {
|
|
| 33 |
+ return fmt.Errorf("create endpoint was not passed an IP address")
|
|
| 34 |
+ } |
|
| 35 |
+ if ep.mac == nil {
|
|
| 36 |
+ ep.mac = netutils.GenerateMACFromIP(ep.addr.IP) |
|
| 37 |
+ if err := ifInfo.SetMacAddress(ep.mac); err != nil {
|
|
| 38 |
+ return err |
|
| 39 |
+ } |
|
| 40 |
+ } |
|
| 41 |
+ // disallow port mapping -p |
|
| 42 |
+ if opt, ok := epOptions[netlabel.PortMap]; ok {
|
|
| 43 |
+ if _, ok := opt.([]types.PortBinding); ok {
|
|
| 44 |
+ if len(opt.([]types.PortBinding)) > 0 {
|
|
| 45 |
+ return fmt.Errorf("%s driver does not support port mappings", ipvlanType)
|
|
| 46 |
+ } |
|
| 47 |
+ } |
|
| 48 |
+ } |
|
| 49 |
+ // disallow port exposure --expose |
|
| 50 |
+ if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
|
|
| 51 |
+ if _, ok := opt.([]types.TransportPort); ok {
|
|
| 52 |
+ if len(opt.([]types.TransportPort)) > 0 {
|
|
| 53 |
+ return fmt.Errorf("%s driver does not support port exposures", ipvlanType)
|
|
| 54 |
+ } |
|
| 55 |
+ } |
|
| 56 |
+ } |
|
| 57 |
+ n.addEndpoint(ep) |
|
| 58 |
+ |
|
| 59 |
+ return nil |
|
| 60 |
+} |
|
| 61 |
+ |
|
| 62 |
+// DeleteEndpoint remove the endpoint and associated netlink interface |
|
| 63 |
+func (d *driver) DeleteEndpoint(nid, eid string) error {
|
|
| 64 |
+ if err := validateID(nid, eid); err != nil {
|
|
| 65 |
+ return err |
|
| 66 |
+ } |
|
| 67 |
+ n := d.network(nid) |
|
| 68 |
+ if n == nil {
|
|
| 69 |
+ return fmt.Errorf("network id %q not found", nid)
|
|
| 70 |
+ } |
|
| 71 |
+ ep := n.endpoint(eid) |
|
| 72 |
+ if ep == nil {
|
|
| 73 |
+ return fmt.Errorf("endpoint id %q not found", eid)
|
|
| 74 |
+ } |
|
| 75 |
+ if link, err := netlink.LinkByName(ep.srcName); err == nil {
|
|
| 76 |
+ netlink.LinkDel(link) |
|
| 77 |
+ } |
|
| 78 |
+ return nil |
|
| 79 |
+} |
| 0 | 80 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,189 @@ |
| 0 |
+package ipvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ "net" |
|
| 5 |
+ |
|
| 6 |
+ "github.com/Sirupsen/logrus" |
|
| 7 |
+ "github.com/docker/libnetwork/driverapi" |
|
| 8 |
+ "github.com/docker/libnetwork/netutils" |
|
| 9 |
+ "github.com/docker/libnetwork/osl" |
|
| 10 |
+ "github.com/docker/libnetwork/types" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+type staticRoute struct {
|
|
| 14 |
+ Destination *net.IPNet |
|
| 15 |
+ RouteType int |
|
| 16 |
+ NextHop net.IP |
|
| 17 |
+} |
|
| 18 |
+ |
|
| 19 |
+const ( |
|
| 20 |
+ defaultV4RouteCidr = "0.0.0.0/0" |
|
| 21 |
+ defaultV6RouteCidr = "::/0" |
|
| 22 |
+) |
|
| 23 |
+ |
|
| 24 |
+// Join method is invoked when a Sandbox is attached to an endpoint. |
|
| 25 |
+func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
|
|
| 26 |
+ defer osl.InitOSContext()() |
|
| 27 |
+ n, err := d.getNetwork(nid) |
|
| 28 |
+ if err != nil {
|
|
| 29 |
+ return err |
|
| 30 |
+ } |
|
| 31 |
+ endpoint := n.endpoint(eid) |
|
| 32 |
+ if endpoint == nil {
|
|
| 33 |
+ return fmt.Errorf("could not find endpoint with id %s", eid)
|
|
| 34 |
+ } |
|
| 35 |
+ // generate a name for the iface that will be renamed to eth0 in the sbox |
|
| 36 |
+ containerIfName, err := netutils.GenerateIfaceName(vethPrefix, vethLen) |
|
| 37 |
+ if err != nil {
|
|
| 38 |
+ return fmt.Errorf("error generating an interface name: %v", err)
|
|
| 39 |
+ } |
|
| 40 |
+ // create the netlink ipvlan interface |
|
| 41 |
+ vethName, err := createIPVlan(containerIfName, n.config.HostIface, n.config.IpvlanMode) |
|
| 42 |
+ if err != nil {
|
|
| 43 |
+ return err |
|
| 44 |
+ } |
|
| 45 |
+ // bind the generated iface name to the endpoint |
|
| 46 |
+ endpoint.srcName = vethName |
|
| 47 |
+ ep := n.endpoint(eid) |
|
| 48 |
+ if ep == nil {
|
|
| 49 |
+ return fmt.Errorf("could not find endpoint with id %s", eid)
|
|
| 50 |
+ } |
|
| 51 |
+ if n.config.IpvlanMode == modeL3 {
|
|
| 52 |
+ // disable gateway services to add a default gw using dev eth0 only |
|
| 53 |
+ jinfo.DisableGatewayService() |
|
| 54 |
+ defaultRoute, err := ifaceGateway(defaultV4RouteCidr) |
|
| 55 |
+ if err != nil {
|
|
| 56 |
+ return err |
|
| 57 |
+ } |
|
| 58 |
+ if err := jinfo.AddStaticRoute(defaultRoute.Destination, defaultRoute.RouteType, defaultRoute.NextHop); err != nil {
|
|
| 59 |
+ return fmt.Errorf("failed to set an ipvlan l3 mode ipv4 default gateway: %v", err)
|
|
| 60 |
+ } |
|
| 61 |
+ logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Ipvlan_Mode: %s, Host_Iface: %s",
|
|
| 62 |
+ ep.addr.IP.String(), n.config.IpvlanMode, n.config.HostIface) |
|
| 63 |
+ // If the endpoint has a v6 address, set a v6 default route |
|
| 64 |
+ if ep.addrv6 != nil {
|
|
| 65 |
+ default6Route, err := ifaceGateway(defaultV6RouteCidr) |
|
| 66 |
+ if err != nil {
|
|
| 67 |
+ return err |
|
| 68 |
+ } |
|
| 69 |
+ if err = jinfo.AddStaticRoute(default6Route.Destination, default6Route.RouteType, default6Route.NextHop); err != nil {
|
|
| 70 |
+ return fmt.Errorf("failed to set an ipvlan l3 mode ipv6 default gateway: %v", err)
|
|
| 71 |
+ } |
|
| 72 |
+ logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Ipvlan_Mode: %s, Host_Iface: %s",
|
|
| 73 |
+ ep.addrv6.IP.String(), n.config.IpvlanMode, n.config.HostIface) |
|
| 74 |
+ } |
|
| 75 |
+ } |
|
| 76 |
+ if n.config.IpvlanMode == modeL2 {
|
|
| 77 |
+ // parse and correlate the endpoint v4 address with the available v4 subnets |
|
| 78 |
+ if len(n.config.Ipv4Subnets) > 0 {
|
|
| 79 |
+ s := n.getSubnetforIPv4(ep.addr) |
|
| 80 |
+ if s == nil {
|
|
| 81 |
+ return fmt.Errorf("could not find a valid ipv4 subnet for endpoint %s", eid)
|
|
| 82 |
+ } |
|
| 83 |
+ v4gw, _, err := net.ParseCIDR(s.GwIP) |
|
| 84 |
+ if err != nil {
|
|
| 85 |
+ return fmt.Errorf("gatway %s is not a valid ipv4 address: %v", s.GwIP, err)
|
|
| 86 |
+ } |
|
| 87 |
+ err = jinfo.SetGateway(v4gw) |
|
| 88 |
+ if err != nil {
|
|
| 89 |
+ return err |
|
| 90 |
+ } |
|
| 91 |
+ logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Host_Iface: %s",
|
|
| 92 |
+ ep.addr.IP.String(), v4gw.String(), n.config.IpvlanMode, n.config.HostIface) |
|
| 93 |
+ } |
|
| 94 |
+ // parse and correlate the endpoint v6 address with the available v6 subnets |
|
| 95 |
+ if len(n.config.Ipv6Subnets) > 0 {
|
|
| 96 |
+ s := n.getSubnetforIPv6(ep.addrv6) |
|
| 97 |
+ if s == nil {
|
|
| 98 |
+ return fmt.Errorf("could not find a valid ipv6 subnet for endpoint %s", eid)
|
|
| 99 |
+ } |
|
| 100 |
+ v6gw, _, err := net.ParseCIDR(s.GwIP) |
|
| 101 |
+ if err != nil {
|
|
| 102 |
+ return fmt.Errorf("gatway %s is not a valid ipv6 address: %v", s.GwIP, err)
|
|
| 103 |
+ } |
|
| 104 |
+ err = jinfo.SetGatewayIPv6(v6gw) |
|
| 105 |
+ if err != nil {
|
|
| 106 |
+ return err |
|
| 107 |
+ } |
|
| 108 |
+ logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Host_Iface: %s",
|
|
| 109 |
+ ep.addrv6.IP.String(), v6gw.String(), n.config.IpvlanMode, n.config.HostIface) |
|
| 110 |
+ } |
|
| 111 |
+ } |
|
| 112 |
+ iNames := jinfo.InterfaceName() |
|
| 113 |
+ err = iNames.SetNames(vethName, containerVethPrefix) |
|
| 114 |
+ if err != nil {
|
|
| 115 |
+ return err |
|
| 116 |
+ } |
|
| 117 |
+ return nil |
|
| 118 |
+} |
|
| 119 |
+ |
|
| 120 |
+// Leave method is invoked when a Sandbox detaches from an endpoint. |
|
| 121 |
+func (d *driver) Leave(nid, eid string) error {
|
|
| 122 |
+ network, err := d.getNetwork(nid) |
|
| 123 |
+ if err != nil {
|
|
| 124 |
+ return err |
|
| 125 |
+ } |
|
| 126 |
+ endpoint, err := network.getEndpoint(eid) |
|
| 127 |
+ if err != nil {
|
|
| 128 |
+ return err |
|
| 129 |
+ } |
|
| 130 |
+ if endpoint == nil {
|
|
| 131 |
+ return fmt.Errorf("could not find endpoint with id %s", eid)
|
|
| 132 |
+ } |
|
| 133 |
+ return nil |
|
| 134 |
+} |
|
| 135 |
+ |
|
| 136 |
+// ifaceGateway returns a static route for either v4/v6 to be set to the container eth0 |
|
| 137 |
+func ifaceGateway(dfNet string) (*staticRoute, error) {
|
|
| 138 |
+ nh, dst, err := net.ParseCIDR(dfNet) |
|
| 139 |
+ if err != nil {
|
|
| 140 |
+ return nil, fmt.Errorf("unable to parse default route %v", err)
|
|
| 141 |
+ } |
|
| 142 |
+ defaultRoute := &staticRoute{
|
|
| 143 |
+ Destination: dst, |
|
| 144 |
+ RouteType: types.CONNECTED, |
|
| 145 |
+ NextHop: nh, |
|
| 146 |
+ } |
|
| 147 |
+ return defaultRoute, nil |
|
| 148 |
+} |
|
| 149 |
+ |
|
| 150 |
+// getSubnetforIPv4 returns the ipv4 subnet to which the given IP belongs |
|
| 151 |
+func (n *network) getSubnetforIPv4(ip *net.IPNet) *ipv4Subnet {
|
|
| 152 |
+ for _, s := range n.config.Ipv4Subnets {
|
|
| 153 |
+ _, snet, err := net.ParseCIDR(s.SubnetIP) |
|
| 154 |
+ if err != nil {
|
|
| 155 |
+ return nil |
|
| 156 |
+ } |
|
| 157 |
+ // first check if the mask lengths are the same |
|
| 158 |
+ i, _ := snet.Mask.Size() |
|
| 159 |
+ j, _ := ip.Mask.Size() |
|
| 160 |
+ if i != j {
|
|
| 161 |
+ continue |
|
| 162 |
+ } |
|
| 163 |
+ if snet.Contains(ip.IP) {
|
|
| 164 |
+ return s |
|
| 165 |
+ } |
|
| 166 |
+ } |
|
| 167 |
+ return nil |
|
| 168 |
+} |
|
| 169 |
+ |
|
| 170 |
+// getSubnetforIPv6 returns the ipv6 subnet to which the given IP belongs |
|
| 171 |
+func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipv6Subnet {
|
|
| 172 |
+ for _, s := range n.config.Ipv6Subnets {
|
|
| 173 |
+ _, snet, err := net.ParseCIDR(s.SubnetIP) |
|
| 174 |
+ if err != nil {
|
|
| 175 |
+ return nil |
|
| 176 |
+ } |
|
| 177 |
+ // first check if the mask lengths are the same |
|
| 178 |
+ i, _ := snet.Mask.Size() |
|
| 179 |
+ j, _ := ip.Mask.Size() |
|
| 180 |
+ if i != j {
|
|
| 181 |
+ continue |
|
| 182 |
+ } |
|
| 183 |
+ if snet.Contains(ip.IP) {
|
|
| 184 |
+ return s |
|
| 185 |
+ } |
|
| 186 |
+ } |
|
| 187 |
+ return nil |
|
| 188 |
+} |
| 0 | 189 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,187 @@ |
| 0 |
+package ipvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/Sirupsen/logrus" |
|
| 6 |
+ "github.com/docker/docker/pkg/stringid" |
|
| 7 |
+ "github.com/docker/libnetwork/driverapi" |
|
| 8 |
+ "github.com/docker/libnetwork/netlabel" |
|
| 9 |
+ "github.com/docker/libnetwork/options" |
|
| 10 |
+ "github.com/docker/libnetwork/types" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+// CreateNetwork the network for the specified driver type |
|
| 14 |
+func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Data, ipV6Data []driverapi.IPAMData) error {
|
|
| 15 |
+ // parse and validate the config and bind to networkConfiguration |
|
| 16 |
+ config, err := parseNetworkOptions(id, option) |
|
| 17 |
+ if err != nil {
|
|
| 18 |
+ return err |
|
| 19 |
+ } |
|
| 20 |
+ config.ID = id |
|
| 21 |
+ err = config.processIPAM(id, ipV4Data, ipV6Data) |
|
| 22 |
+ if err != nil {
|
|
| 23 |
+ return err |
|
| 24 |
+ } |
|
| 25 |
+ // verify the ipvlan mode from -o ipvlan_mode option |
|
| 26 |
+ switch config.IpvlanMode {
|
|
| 27 |
+ case "", modeL2: |
|
| 28 |
+ // default to ipvlan L2 mode if -o ipvlan_mode is empty |
|
| 29 |
+ config.IpvlanMode = modeL2 |
|
| 30 |
+ case modeL3: |
|
| 31 |
+ config.IpvlanMode = modeL3 |
|
| 32 |
+ default: |
|
| 33 |
+ return fmt.Errorf("requested ipvlan mode '%s' is not valid, 'l2' mode is the ipvlan driver default", config.IpvlanMode)
|
|
| 34 |
+ } |
|
| 35 |
+ // user must specify a parent interface on the host to attach the container vif -o host_iface=eth0 |
|
| 36 |
+ if config.HostIface == "" {
|
|
| 37 |
+ return fmt.Errorf("%s requires an interface from the docker host to be specified (usage: -o host_iface=eth)", ipvlanType)
|
|
| 38 |
+ } |
|
| 39 |
+ // loopback is not a valid parent link |
|
| 40 |
+ if config.HostIface == "lo" {
|
|
| 41 |
+ return fmt.Errorf("loopback interface is not a valid %s parent link", ipvlanType)
|
|
| 42 |
+ } |
|
| 43 |
+ err = d.createNetwork(config) |
|
| 44 |
+ if err != nil {
|
|
| 45 |
+ return err |
|
| 46 |
+ } |
|
| 47 |
+ // update persistent db, rollback on fail |
|
| 48 |
+ err = d.storeUpdate(config) |
|
| 49 |
+ if err != nil {
|
|
| 50 |
+ d.deleteNetwork(config.ID) |
|
| 51 |
+ return err |
|
| 52 |
+ } |
|
| 53 |
+ |
|
| 54 |
+ return nil |
|
| 55 |
+} |
|
| 56 |
+ |
|
| 57 |
+// createNetwork is used by new network callbacks and persistent network cache |
|
| 58 |
+func (d *driver) createNetwork(config *configuration) error {
|
|
| 59 |
+ networkList := d.getNetworks() |
|
| 60 |
+ for _, nw := range networkList {
|
|
| 61 |
+ if config.HostIface == nw.config.HostIface {
|
|
| 62 |
+ return fmt.Errorf("network %s is already using host interface %s",
|
|
| 63 |
+ stringid.TruncateID(nw.config.ID), config.HostIface) |
|
| 64 |
+ } |
|
| 65 |
+ } |
|
| 66 |
+ // if the -o host_iface does not exist, attempt to parse a parent_iface.vlan_id |
|
| 67 |
+ if ok := hostIfaceExists(config.HostIface); !ok {
|
|
| 68 |
+ // if the subinterface parent_iface.vlan_id checks do not pass, return err. |
|
| 69 |
+ // a valid example is eth0.10 for a parent iface: eth0 with a vlan id: 10 |
|
| 70 |
+ err := createVlanLink(config.HostIface) |
|
| 71 |
+ if err != nil {
|
|
| 72 |
+ return err |
|
| 73 |
+ } |
|
| 74 |
+ // if driver created the networks slave link, record it for future deletion |
|
| 75 |
+ config.CreatedSlaveLink = true |
|
| 76 |
+ } |
|
| 77 |
+ n := &network{
|
|
| 78 |
+ id: config.ID, |
|
| 79 |
+ driver: d, |
|
| 80 |
+ endpoints: endpointTable{},
|
|
| 81 |
+ config: config, |
|
| 82 |
+ } |
|
| 83 |
+ // add the *network |
|
| 84 |
+ d.addNetwork(n) |
|
| 85 |
+ |
|
| 86 |
+ return nil |
|
| 87 |
+} |
|
| 88 |
+ |
|
| 89 |
+// DeleteNetwork the network for the specified driver type |
|
| 90 |
+func (d *driver) DeleteNetwork(nid string) error {
|
|
| 91 |
+ n := d.network(nid) |
|
| 92 |
+ // if the driver created the slave interface, delete it, otherwise leave it |
|
| 93 |
+ if ok := n.config.CreatedSlaveLink; ok {
|
|
| 94 |
+ // if the interface exists, only delete if it matches iface.vlan naming |
|
| 95 |
+ if ok := hostIfaceExists(n.config.HostIface); ok {
|
|
| 96 |
+ err := delVlanLink(n.config.HostIface) |
|
| 97 |
+ if err != nil {
|
|
| 98 |
+ logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v", n.config.HostIface, err)
|
|
| 99 |
+ } |
|
| 100 |
+ } |
|
| 101 |
+ } |
|
| 102 |
+ // delete the *network |
|
| 103 |
+ d.deleteNetwork(nid) |
|
| 104 |
+ // delete the network record from persistent cache |
|
| 105 |
+ return d.storeDelete(n.config) |
|
| 106 |
+} |
|
| 107 |
+ |
|
| 108 |
+// parseNetworkOptions parse docker network options |
|
| 109 |
+func parseNetworkOptions(id string, option options.Generic) (*configuration, error) {
|
|
| 110 |
+ var ( |
|
| 111 |
+ err error |
|
| 112 |
+ config = &configuration{}
|
|
| 113 |
+ ) |
|
| 114 |
+ // parse generic labels first |
|
| 115 |
+ if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
|
|
| 116 |
+ if config, err = parseNetworkGenericOptions(genData); err != nil {
|
|
| 117 |
+ return nil, err |
|
| 118 |
+ } |
|
| 119 |
+ } |
|
| 120 |
+ // return an error if the unsupported --internal is passed |
|
| 121 |
+ if _, ok := option[netlabel.Internal]; ok {
|
|
| 122 |
+ return nil, fmt.Errorf("--internal option is not supported with the ipvlan driver")
|
|
| 123 |
+ } |
|
| 124 |
+ return config, nil |
|
| 125 |
+} |
|
| 126 |
+ |
|
| 127 |
+// parseNetworkGenericOptions parse generic driver docker network options |
|
| 128 |
+func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
|
|
| 129 |
+ var ( |
|
| 130 |
+ err error |
|
| 131 |
+ config *configuration |
|
| 132 |
+ ) |
|
| 133 |
+ switch opt := data.(type) {
|
|
| 134 |
+ case *configuration: |
|
| 135 |
+ config = opt |
|
| 136 |
+ case map[string]string: |
|
| 137 |
+ config = &configuration{}
|
|
| 138 |
+ err = config.fromOptions(opt) |
|
| 139 |
+ case options.Generic: |
|
| 140 |
+ var opaqueConfig interface{}
|
|
| 141 |
+ if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
|
|
| 142 |
+ config = opaqueConfig.(*configuration) |
|
| 143 |
+ } |
|
| 144 |
+ default: |
|
| 145 |
+ err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
|
|
| 146 |
+ } |
|
| 147 |
+ return config, err |
|
| 148 |
+} |
|
| 149 |
+ |
|
| 150 |
+// fromOptions binds the generic options to networkConfiguration to cache |
|
| 151 |
+func (config *configuration) fromOptions(labels map[string]string) error {
|
|
| 152 |
+ for label, value := range labels {
|
|
| 153 |
+ switch label {
|
|
| 154 |
+ case hostIfaceOpt: |
|
| 155 |
+ // parse driver option '-o host_iface' |
|
| 156 |
+ config.HostIface = value |
|
| 157 |
+ case driverModeOpt: |
|
| 158 |
+ // parse driver option '-o ipvlan_mode' |
|
| 159 |
+ config.IpvlanMode = value |
|
| 160 |
+ } |
|
| 161 |
+ } |
|
| 162 |
+ return nil |
|
| 163 |
+} |
|
| 164 |
+ |
|
| 165 |
+// processIPAM parses v4 and v6 IP information and binds it to the network configuration |
|
| 166 |
+func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
|
|
| 167 |
+ if len(ipamV4Data) > 0 {
|
|
| 168 |
+ for _, ipd := range ipamV4Data {
|
|
| 169 |
+ s := &ipv4Subnet{
|
|
| 170 |
+ SubnetIP: ipd.Pool.String(), |
|
| 171 |
+ GwIP: ipd.Gateway.String(), |
|
| 172 |
+ } |
|
| 173 |
+ config.Ipv4Subnets = append(config.Ipv4Subnets, s) |
|
| 174 |
+ } |
|
| 175 |
+ } |
|
| 176 |
+ if len(ipamV6Data) > 0 {
|
|
| 177 |
+ for _, ipd := range ipamV6Data {
|
|
| 178 |
+ s := &ipv6Subnet{
|
|
| 179 |
+ SubnetIP: ipd.Pool.String(), |
|
| 180 |
+ GwIP: ipd.Gateway.String(), |
|
| 181 |
+ } |
|
| 182 |
+ config.Ipv6Subnets = append(config.Ipv6Subnets, s) |
|
| 183 |
+ } |
|
| 184 |
+ } |
|
| 185 |
+ return nil |
|
| 186 |
+} |
| 0 | 187 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,166 @@ |
| 0 |
+package ipvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bufio" |
|
| 4 |
+ "fmt" |
|
| 5 |
+ "os" |
|
| 6 |
+ "os/exec" |
|
| 7 |
+ "strconv" |
|
| 8 |
+ "strings" |
|
| 9 |
+ |
|
| 10 |
+ "github.com/Sirupsen/logrus" |
|
| 11 |
+ "github.com/docker/libnetwork/osl" |
|
| 12 |
+ "github.com/vishvananda/netlink" |
|
| 13 |
+) |
|
| 14 |
+ |
|
| 15 |
+// Create the ipvlan netlink interface specifying the source name |
|
| 16 |
+func createIPVlan(containerIfName, hostIface, ipVlanMode string) (string, error) {
|
|
| 17 |
+ defer osl.InitOSContext()() |
|
| 18 |
+ // Set the ipvlan mode. Default is L2 mode |
|
| 19 |
+ mode, err := setIPVlanMode(ipVlanMode) |
|
| 20 |
+ if err != nil {
|
|
| 21 |
+ return "", fmt.Errorf("unsupported %s ipvlan mode: %v", ipVlanMode, err)
|
|
| 22 |
+ } |
|
| 23 |
+ // Get the link for the master index (Example: the docker host eth iface) |
|
| 24 |
+ hostEth, err := netlink.LinkByName(hostIface) |
|
| 25 |
+ if err != nil {
|
|
| 26 |
+ return "", fmt.Errorf("the requested host interface %s was not found on the Docker host", hostIface)
|
|
| 27 |
+ } |
|
| 28 |
+ // Create a ipvlan link |
|
| 29 |
+ ipvlan := &netlink.IPVlan{
|
|
| 30 |
+ LinkAttrs: netlink.LinkAttrs{
|
|
| 31 |
+ Name: containerIfName, |
|
| 32 |
+ ParentIndex: hostEth.Attrs().Index, |
|
| 33 |
+ }, |
|
| 34 |
+ Mode: mode, |
|
| 35 |
+ } |
|
| 36 |
+ if err := netlink.LinkAdd(ipvlan); err != nil {
|
|
| 37 |
+ // verbose but will be an issue if you create a macvlan and ipvlan using the same parent iface |
|
| 38 |
+ logrus.Warn("Ensure there are no macvlan networks using the same `-o host_iface` as the ipvlan network.")
|
|
| 39 |
+ return "", fmt.Errorf("Failed to create ipvlan link: %s with the error: %v", ipvlan.Name, err)
|
|
| 40 |
+ } |
|
| 41 |
+ return ipvlan.Attrs().Name, nil |
|
| 42 |
+} |
|
| 43 |
+ |
|
| 44 |
+// setIPVlanMode setter for one of the two ipvlan port types |
|
| 45 |
+func setIPVlanMode(mode string) (netlink.IPVlanMode, error) {
|
|
| 46 |
+ switch mode {
|
|
| 47 |
+ case modeL2: |
|
| 48 |
+ return netlink.IPVLAN_MODE_L2, nil |
|
| 49 |
+ case modeL3: |
|
| 50 |
+ return netlink.IPVLAN_MODE_L3, nil |
|
| 51 |
+ default: |
|
| 52 |
+ return 0, fmt.Errorf("Unknown ipvlan mode: %s", mode)
|
|
| 53 |
+ } |
|
| 54 |
+} |
|
| 55 |
+ |
|
| 56 |
+// validateHostIface check if the specified interface exists in the default namespace |
|
| 57 |
+func hostIfaceExists(ifaceStr string) bool {
|
|
| 58 |
+ _, err := netlink.LinkByName(ifaceStr) |
|
| 59 |
+ if err != nil {
|
|
| 60 |
+ return false |
|
| 61 |
+ } |
|
| 62 |
+ return true |
|
| 63 |
+} |
|
| 64 |
+ |
|
| 65 |
+// kernelSupport for the necessary kernel module for the driver type |
|
| 66 |
+func kernelSupport(networkTpe string) error {
|
|
| 67 |
+ // attempt to load the module, silent if successful or already loaded |
|
| 68 |
+ exec.Command("modprobe", ipvlanType).Run()
|
|
| 69 |
+ f, err := os.Open("/proc/modules")
|
|
| 70 |
+ if err != nil {
|
|
| 71 |
+ return err |
|
| 72 |
+ } |
|
| 73 |
+ defer f.Close() |
|
| 74 |
+ s := bufio.NewScanner(f) |
|
| 75 |
+ for s.Scan() {
|
|
| 76 |
+ if strings.Contains(s.Text(), ipvlanType) {
|
|
| 77 |
+ return nil |
|
| 78 |
+ } |
|
| 79 |
+ } |
|
| 80 |
+ return fmt.Errorf("required kernel module '%s' was not found in /proc/modules, ipvlan requires kernel version >= 3.19", ipvlanType)
|
|
| 81 |
+} |
|
| 82 |
+ |
|
| 83 |
+// createVlanLink parses sub-interfaces and vlan id for creation |
|
| 84 |
+func createVlanLink(ifaceName string) error {
|
|
| 85 |
+ if strings.Contains(ifaceName, ".") {
|
|
| 86 |
+ parentIface, vidInt, err := parseVlan(ifaceName) |
|
| 87 |
+ if err != nil {
|
|
| 88 |
+ return err |
|
| 89 |
+ } |
|
| 90 |
+ // VLAN identifier or VID is a 12-bit field specifying the VLAN to which the frame belongs |
|
| 91 |
+ if vidInt > 4094 || vidInt < 1 {
|
|
| 92 |
+ return fmt.Errorf("vlan id must be between 1-4094, received: %d", vidInt)
|
|
| 93 |
+ } |
|
| 94 |
+ // get the parent link to attach a vlan subinterface |
|
| 95 |
+ hostIface, err := netlink.LinkByName(parentIface) |
|
| 96 |
+ if err != nil {
|
|
| 97 |
+ return fmt.Errorf("failed to find master interface %s on the Docker host: %v", parentIface, err)
|
|
| 98 |
+ } |
|
| 99 |
+ vlanIface := &netlink.Vlan{
|
|
| 100 |
+ LinkAttrs: netlink.LinkAttrs{
|
|
| 101 |
+ Name: ifaceName, |
|
| 102 |
+ ParentIndex: hostIface.Attrs().Index, |
|
| 103 |
+ }, |
|
| 104 |
+ VlanId: vidInt, |
|
| 105 |
+ } |
|
| 106 |
+ // create the subinterface |
|
| 107 |
+ if err := netlink.LinkAdd(vlanIface); err != nil {
|
|
| 108 |
+ return fmt.Errorf("failed to create %s vlan link: %v", vlanIface.Name, err)
|
|
| 109 |
+ } |
|
| 110 |
+ // Bring the new netlink iface up |
|
| 111 |
+ if err := netlink.LinkSetUp(vlanIface); err != nil {
|
|
| 112 |
+ return fmt.Errorf("failed to enable %s the ipvlan netlink link: %v", vlanIface.Name, err)
|
|
| 113 |
+ } |
|
| 114 |
+ logrus.Debugf("Added a vlan tagged netlink subinterface: %s with a vlan id: %d", ifaceName, vidInt)
|
|
| 115 |
+ return nil |
|
| 116 |
+ } |
|
| 117 |
+ return fmt.Errorf("invalid subinterface vlan name %s, example formatting is eth0.10", ifaceName)
|
|
| 118 |
+} |
|
| 119 |
+ |
|
| 120 |
+// verifyVlanDel verifies only sub-interfaces with a vlan id get deleted |
|
| 121 |
+func delVlanLink(ifaceName string) error {
|
|
| 122 |
+ if strings.Contains(ifaceName, ".") {
|
|
| 123 |
+ _, _, err := parseVlan(ifaceName) |
|
| 124 |
+ if err != nil {
|
|
| 125 |
+ return err |
|
| 126 |
+ } |
|
| 127 |
+ // delete the vlan subinterface |
|
| 128 |
+ vlanIface, err := netlink.LinkByName(ifaceName) |
|
| 129 |
+ if err != nil {
|
|
| 130 |
+ return fmt.Errorf("failed to find interface %s on the Docker host : %v", ifaceName, err)
|
|
| 131 |
+ } |
|
| 132 |
+ // verify a parent interface isn't being deleted |
|
| 133 |
+ if vlanIface.Attrs().ParentIndex == 0 {
|
|
| 134 |
+ return fmt.Errorf("interface %s does not appear to be a slave device: %v", ifaceName, err)
|
|
| 135 |
+ } |
|
| 136 |
+ // delete the ipvlan slave device |
|
| 137 |
+ if err := netlink.LinkDel(vlanIface); err != nil {
|
|
| 138 |
+ return fmt.Errorf("failed to delete %s link: %v", ifaceName, err)
|
|
| 139 |
+ } |
|
| 140 |
+ logrus.Debugf("Deleted a vlan tagged netlink subinterface: %s", ifaceName)
|
|
| 141 |
+ } |
|
| 142 |
+ // if the subinterface doesn't parse to iface.vlan_id leave the interface in |
|
| 143 |
+ // place since it could be a user specified name not created by the driver. |
|
| 144 |
+ return nil |
|
| 145 |
+} |
|
| 146 |
+ |
|
| 147 |
+// parseVlan parses and verifies a slave interface name: -o host_iface=eth0.10 |
|
| 148 |
+func parseVlan(ifaceName string) (string, int, error) {
|
|
| 149 |
+ // parse -o host_iface=eth0.10 |
|
| 150 |
+ splitIface := strings.Split(ifaceName, ".") |
|
| 151 |
+ if len(splitIface) != 2 {
|
|
| 152 |
+ return "", 0, fmt.Errorf("required interface name format is: name.vlan_id, ex. eth0.10 for vlan 10, instead received %s", ifaceName)
|
|
| 153 |
+ } |
|
| 154 |
+ parentIface, vidStr := splitIface[0], splitIface[1] |
|
| 155 |
+ // validate type and convert vlan id to int |
|
| 156 |
+ vidInt, err := strconv.Atoi(vidStr) |
|
| 157 |
+ if err != nil {
|
|
| 158 |
+ return "", 0, fmt.Errorf("unable to parse a valid vlan id from: %s (ex. eth0.10 for vlan 10)", vidStr)
|
|
| 159 |
+ } |
|
| 160 |
+ // Check if the interface exists |
|
| 161 |
+ if ok := hostIfaceExists(parentIface); !ok {
|
|
| 162 |
+ return "", 0, fmt.Errorf("-o host_iface parent interface does was not found on the host: %s", parentIface)
|
|
| 163 |
+ } |
|
| 164 |
+ return parentIface, vidInt, nil |
|
| 165 |
+} |
| 0 | 166 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,87 @@ |
| 0 |
+package ipvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "testing" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/vishvananda/netlink" |
|
| 6 |
+) |
|
| 7 |
+ |
|
| 8 |
+// TestValidateLink tests the validateHostIface function |
|
| 9 |
+func TestValidateLink(t *testing.T) {
|
|
| 10 |
+ validIface := "lo" |
|
| 11 |
+ invalidIface := "foo12345" |
|
| 12 |
+ |
|
| 13 |
+ // test a valid parent interface validation |
|
| 14 |
+ if ok := hostIfaceExists(validIface); !ok {
|
|
| 15 |
+ t.Fatalf("failed validating loopback %s", validIface)
|
|
| 16 |
+ } |
|
| 17 |
+ // test a invalid parent interface validation |
|
| 18 |
+ if ok := hostIfaceExists(invalidIface); ok {
|
|
| 19 |
+ t.Fatalf("failed to invalidate interface %s", invalidIface)
|
|
| 20 |
+ } |
|
| 21 |
+} |
|
| 22 |
+ |
|
| 23 |
+// TestValidateSubLink tests valid 802.1q naming convention |
|
| 24 |
+func TestValidateSubLink(t *testing.T) {
|
|
| 25 |
+ validSubIface := "lo.10" |
|
| 26 |
+ invalidSubIface1 := "lo" |
|
| 27 |
+ invalidSubIface2 := "lo:10" |
|
| 28 |
+ invalidSubIface3 := "foo123.456" |
|
| 29 |
+ |
|
| 30 |
+ // test a valid parent_iface.vlan_id |
|
| 31 |
+ _, _, err := parseVlan(validSubIface) |
|
| 32 |
+ if err != nil {
|
|
| 33 |
+ t.Fatalf("failed subinterface validation: %v", err)
|
|
| 34 |
+ } |
|
| 35 |
+ // test a invalid vid with a valid parent link |
|
| 36 |
+ _, _, err = parseVlan(invalidSubIface1) |
|
| 37 |
+ if err == nil {
|
|
| 38 |
+ t.Fatalf("failed subinterface validation test: %s", invalidSubIface1)
|
|
| 39 |
+ } |
|
| 40 |
+ // test a valid vid with a valid parent link with a invalid delimiter |
|
| 41 |
+ _, _, err = parseVlan(invalidSubIface2) |
|
| 42 |
+ if err == nil {
|
|
| 43 |
+ t.Fatalf("failed subinterface validation test: %v", invalidSubIface2)
|
|
| 44 |
+ } |
|
| 45 |
+ // test a invalid parent link with a valid vid |
|
| 46 |
+ _, _, err = parseVlan(invalidSubIface3) |
|
| 47 |
+ if err == nil {
|
|
| 48 |
+ t.Fatalf("failed subinterface validation test: %v", invalidSubIface3)
|
|
| 49 |
+ } |
|
| 50 |
+} |
|
| 51 |
+ |
|
| 52 |
+// TestSetIPVlanMode tests the ipvlan mode setter |
|
| 53 |
+func TestSetIPVlanMode(t *testing.T) {
|
|
| 54 |
+ // test ipvlan l2 mode |
|
| 55 |
+ mode, err := setIPVlanMode(modeL2) |
|
| 56 |
+ if err != nil {
|
|
| 57 |
+ t.Fatalf("error parsing %v vlan mode: %v", mode, err)
|
|
| 58 |
+ } |
|
| 59 |
+ if mode != netlink.IPVLAN_MODE_L2 {
|
|
| 60 |
+ t.Fatalf("expected %d got %d", netlink.IPVLAN_MODE_L2, mode)
|
|
| 61 |
+ } |
|
| 62 |
+ // test ipvlan l3 mode |
|
| 63 |
+ mode, err = setIPVlanMode(modeL3) |
|
| 64 |
+ if err != nil {
|
|
| 65 |
+ t.Fatalf("error parsing %v vlan mode: %v", mode, err)
|
|
| 66 |
+ } |
|
| 67 |
+ if mode != netlink.IPVLAN_MODE_L3 {
|
|
| 68 |
+ t.Fatalf("expected %d got %d", netlink.IPVLAN_MODE_L3, mode)
|
|
| 69 |
+ } |
|
| 70 |
+ // test invalid mode |
|
| 71 |
+ mode, err = setIPVlanMode("foo")
|
|
| 72 |
+ if err == nil {
|
|
| 73 |
+ t.Fatal("invalid ipvlan mode should have returned an error")
|
|
| 74 |
+ } |
|
| 75 |
+ if mode != 0 {
|
|
| 76 |
+ t.Fatalf("expected 0 got %d", mode)
|
|
| 77 |
+ } |
|
| 78 |
+ // test null mode |
|
| 79 |
+ mode, err = setIPVlanMode("")
|
|
| 80 |
+ if err == nil {
|
|
| 81 |
+ t.Fatal("invalid ipvlan mode should have returned an error")
|
|
| 82 |
+ } |
|
| 83 |
+ if mode != 0 {
|
|
| 84 |
+ t.Fatalf("expected 0 got %d", mode)
|
|
| 85 |
+ } |
|
| 86 |
+} |
| 0 | 87 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,109 @@ |
| 0 |
+package ipvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/Sirupsen/logrus" |
|
| 6 |
+ "github.com/docker/libnetwork/osl" |
|
| 7 |
+ "github.com/docker/libnetwork/types" |
|
| 8 |
+) |
|
| 9 |
+ |
|
| 10 |
+func (d *driver) network(nid string) *network {
|
|
| 11 |
+ d.Lock() |
|
| 12 |
+ n, ok := d.networks[nid] |
|
| 13 |
+ d.Unlock() |
|
| 14 |
+ if !ok {
|
|
| 15 |
+ logrus.Errorf("network id %s not found", nid)
|
|
| 16 |
+ } |
|
| 17 |
+ return n |
|
| 18 |
+} |
|
| 19 |
+ |
|
| 20 |
+func (d *driver) addNetwork(n *network) {
|
|
| 21 |
+ d.Lock() |
|
| 22 |
+ d.networks[n.id] = n |
|
| 23 |
+ d.Unlock() |
|
| 24 |
+} |
|
| 25 |
+ |
|
| 26 |
+func (d *driver) deleteNetwork(nid string) {
|
|
| 27 |
+ d.Lock() |
|
| 28 |
+ delete(d.networks, nid) |
|
| 29 |
+ d.Unlock() |
|
| 30 |
+} |
|
| 31 |
+ |
|
| 32 |
+// getNetworks Safely returns a slice of existng networks |
|
| 33 |
+func (d *driver) getNetworks() []*network {
|
|
| 34 |
+ d.Lock() |
|
| 35 |
+ defer d.Unlock() |
|
| 36 |
+ |
|
| 37 |
+ ls := make([]*network, 0, len(d.networks)) |
|
| 38 |
+ for _, nw := range d.networks {
|
|
| 39 |
+ ls = append(ls, nw) |
|
| 40 |
+ } |
|
| 41 |
+ return ls |
|
| 42 |
+} |
|
| 43 |
+ |
|
| 44 |
+func (n *network) endpoint(eid string) *endpoint {
|
|
| 45 |
+ n.Lock() |
|
| 46 |
+ defer n.Unlock() |
|
| 47 |
+ |
|
| 48 |
+ return n.endpoints[eid] |
|
| 49 |
+} |
|
| 50 |
+ |
|
| 51 |
+func (n *network) addEndpoint(ep *endpoint) {
|
|
| 52 |
+ n.Lock() |
|
| 53 |
+ n.endpoints[ep.id] = ep |
|
| 54 |
+ n.Unlock() |
|
| 55 |
+} |
|
| 56 |
+ |
|
| 57 |
+func (n *network) deleteEndpoint(eid string) {
|
|
| 58 |
+ n.Lock() |
|
| 59 |
+ delete(n.endpoints, eid) |
|
| 60 |
+ n.Unlock() |
|
| 61 |
+} |
|
| 62 |
+ |
|
| 63 |
+func (n *network) getEndpoint(eid string) (*endpoint, error) {
|
|
| 64 |
+ n.Lock() |
|
| 65 |
+ defer n.Unlock() |
|
| 66 |
+ if eid == "" {
|
|
| 67 |
+ return nil, fmt.Errorf("endpoint id %s not found", eid)
|
|
| 68 |
+ } |
|
| 69 |
+ if ep, ok := n.endpoints[eid]; ok {
|
|
| 70 |
+ return ep, nil |
|
| 71 |
+ } |
|
| 72 |
+ return nil, nil |
|
| 73 |
+} |
|
| 74 |
+ |
|
| 75 |
+func validateID(nid, eid string) error {
|
|
| 76 |
+ if nid == "" {
|
|
| 77 |
+ return fmt.Errorf("invalid network id")
|
|
| 78 |
+ } |
|
| 79 |
+ if eid == "" {
|
|
| 80 |
+ return fmt.Errorf("invalid endpoint id")
|
|
| 81 |
+ } |
|
| 82 |
+ return nil |
|
| 83 |
+} |
|
| 84 |
+ |
|
| 85 |
+func (n *network) sandbox() osl.Sandbox {
|
|
| 86 |
+ n.Lock() |
|
| 87 |
+ defer n.Unlock() |
|
| 88 |
+ return n.sbox |
|
| 89 |
+} |
|
| 90 |
+ |
|
| 91 |
+func (n *network) setSandbox(sbox osl.Sandbox) {
|
|
| 92 |
+ n.Lock() |
|
| 93 |
+ n.sbox = sbox |
|
| 94 |
+ n.Unlock() |
|
| 95 |
+} |
|
| 96 |
+ |
|
| 97 |
+func (d *driver) getNetwork(id string) (*network, error) {
|
|
| 98 |
+ d.Lock() |
|
| 99 |
+ defer d.Unlock() |
|
| 100 |
+ if id == "" {
|
|
| 101 |
+ return nil, types.BadRequestErrorf("invalid network id: %s", id)
|
|
| 102 |
+ } |
|
| 103 |
+ |
|
| 104 |
+ if nw, ok := d.networks[id]; ok {
|
|
| 105 |
+ return nw, nil |
|
| 106 |
+ } |
|
| 107 |
+ return nil, types.NotFoundErrorf("network not found: %s", id)
|
|
| 108 |
+} |
| 0 | 109 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,213 @@ |
| 0 |
+package ipvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "encoding/json" |
|
| 4 |
+ "fmt" |
|
| 5 |
+ |
|
| 6 |
+ "github.com/Sirupsen/logrus" |
|
| 7 |
+ "github.com/docker/libkv/store/boltdb" |
|
| 8 |
+ "github.com/docker/libnetwork/datastore" |
|
| 9 |
+ "github.com/docker/libnetwork/discoverapi" |
|
| 10 |
+ "github.com/docker/libnetwork/netlabel" |
|
| 11 |
+ "github.com/docker/libnetwork/types" |
|
| 12 |
+) |
|
| 13 |
+ |
|
| 14 |
+const ipvlanPrefix = "ipvlan" // prefix used for persistent driver storage |
|
| 15 |
+ |
|
| 16 |
+// networkConfiguration for this driver's network specific configuration |
|
| 17 |
+type configuration struct {
|
|
| 18 |
+ ID string |
|
| 19 |
+ Mtu int |
|
| 20 |
+ dbIndex uint64 |
|
| 21 |
+ dbExists bool |
|
| 22 |
+ Internal bool |
|
| 23 |
+ HostIface string |
|
| 24 |
+ IpvlanMode string |
|
| 25 |
+ CreatedSlaveLink bool |
|
| 26 |
+ Ipv4Subnets []*ipv4Subnet |
|
| 27 |
+ Ipv6Subnets []*ipv6Subnet |
|
| 28 |
+} |
|
| 29 |
+ |
|
| 30 |
+type ipv4Subnet struct {
|
|
| 31 |
+ SubnetIP string |
|
| 32 |
+ GwIP string |
|
| 33 |
+} |
|
| 34 |
+ |
|
| 35 |
+type ipv6Subnet struct {
|
|
| 36 |
+ SubnetIP string |
|
| 37 |
+ GwIP string |
|
| 38 |
+} |
|
| 39 |
+ |
|
| 40 |
+// initStore drivers are responsible for caching their own persistent state |
|
| 41 |
+func (d *driver) initStore(option map[string]interface{}) error {
|
|
| 42 |
+ if data, ok := option[netlabel.LocalKVClient]; ok {
|
|
| 43 |
+ var err error |
|
| 44 |
+ dsc, ok := data.(discoverapi.DatastoreConfigData) |
|
| 45 |
+ if !ok {
|
|
| 46 |
+ return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
|
|
| 47 |
+ } |
|
| 48 |
+ d.store, err = datastore.NewDataStoreFromConfig(dsc) |
|
| 49 |
+ if err != nil {
|
|
| 50 |
+ return types.InternalErrorf("ipvlan driver failed to initialize data store: %v", err)
|
|
| 51 |
+ } |
|
| 52 |
+ |
|
| 53 |
+ return d.populateNetworks() |
|
| 54 |
+ } |
|
| 55 |
+ |
|
| 56 |
+ return nil |
|
| 57 |
+} |
|
| 58 |
+ |
|
| 59 |
+// populateNetworks is invoked at driver init to recreate persistently stored networks |
|
| 60 |
+func (d *driver) populateNetworks() error {
|
|
| 61 |
+ kvol, err := d.store.List(datastore.Key(ipvlanPrefix), &configuration{})
|
|
| 62 |
+ if err != nil && err != datastore.ErrKeyNotFound && err != boltdb.ErrBoltBucketNotFound {
|
|
| 63 |
+ return fmt.Errorf("failed to get ipvlan network configurations from store: %v", err)
|
|
| 64 |
+ } |
|
| 65 |
+ // If empty it simply means no ipvlan networks have been created yet |
|
| 66 |
+ if err == datastore.ErrKeyNotFound {
|
|
| 67 |
+ return nil |
|
| 68 |
+ } |
|
| 69 |
+ for _, kvo := range kvol {
|
|
| 70 |
+ config := kvo.(*configuration) |
|
| 71 |
+ if err = d.createNetwork(config); err != nil {
|
|
| 72 |
+ logrus.Warnf("could not create ipvlan network for id %s from persistent state", config.ID)
|
|
| 73 |
+ } |
|
| 74 |
+ } |
|
| 75 |
+ |
|
| 76 |
+ return nil |
|
| 77 |
+} |
|
| 78 |
+ |
|
| 79 |
+// storeUpdate used to update persistent ipvlan network records as they are created |
|
| 80 |
+func (d *driver) storeUpdate(kvObject datastore.KVObject) error {
|
|
| 81 |
+ if d.store == nil {
|
|
| 82 |
+ logrus.Warnf("ipvlan store not initialized. kv object %s is not added to the store", datastore.Key(kvObject.Key()...))
|
|
| 83 |
+ return nil |
|
| 84 |
+ } |
|
| 85 |
+ if err := d.store.PutObjectAtomic(kvObject); err != nil {
|
|
| 86 |
+ return fmt.Errorf("failed to update ipvlan store for object type %T: %v", kvObject, err)
|
|
| 87 |
+ } |
|
| 88 |
+ |
|
| 89 |
+ return nil |
|
| 90 |
+} |
|
| 91 |
+ |
|
| 92 |
+// storeDelete used to delete ipvlan network records from persistent cache as they are deleted |
|
| 93 |
+func (d *driver) storeDelete(kvObject datastore.KVObject) error {
|
|
| 94 |
+ if d.store == nil {
|
|
| 95 |
+ logrus.Debugf("ipvlan store not initialized. kv object %s is not deleted from store", datastore.Key(kvObject.Key()...))
|
|
| 96 |
+ return nil |
|
| 97 |
+ } |
|
| 98 |
+retry: |
|
| 99 |
+ if err := d.store.DeleteObjectAtomic(kvObject); err != nil {
|
|
| 100 |
+ if err == datastore.ErrKeyModified {
|
|
| 101 |
+ if err := d.store.GetObject(datastore.Key(kvObject.Key()...), kvObject); err != nil {
|
|
| 102 |
+ return fmt.Errorf("could not update the kvobject to latest when trying to delete: %v", err)
|
|
| 103 |
+ } |
|
| 104 |
+ goto retry |
|
| 105 |
+ } |
|
| 106 |
+ return err |
|
| 107 |
+ } |
|
| 108 |
+ |
|
| 109 |
+ return nil |
|
| 110 |
+} |
|
| 111 |
+ |
|
| 112 |
+func (config *configuration) MarshalJSON() ([]byte, error) {
|
|
| 113 |
+ nMap := make(map[string]interface{})
|
|
| 114 |
+ nMap["ID"] = config.ID |
|
| 115 |
+ nMap["Mtu"] = config.Mtu |
|
| 116 |
+ nMap["HostIface"] = config.HostIface |
|
| 117 |
+ nMap["IpvlanMode"] = config.IpvlanMode |
|
| 118 |
+ nMap["CreatedSubIface"] = config.CreatedSlaveLink |
|
| 119 |
+ if len(config.Ipv4Subnets) > 0 {
|
|
| 120 |
+ iis, err := json.Marshal(config.Ipv4Subnets) |
|
| 121 |
+ if err != nil {
|
|
| 122 |
+ return nil, err |
|
| 123 |
+ } |
|
| 124 |
+ nMap["Ipv4Subnets"] = string(iis) |
|
| 125 |
+ } |
|
| 126 |
+ if len(config.Ipv6Subnets) > 0 {
|
|
| 127 |
+ iis, err := json.Marshal(config.Ipv6Subnets) |
|
| 128 |
+ if err != nil {
|
|
| 129 |
+ return nil, err |
|
| 130 |
+ } |
|
| 131 |
+ nMap["Ipv6Subnets"] = string(iis) |
|
| 132 |
+ } |
|
| 133 |
+ return json.Marshal(nMap) |
|
| 134 |
+} |
|
| 135 |
+ |
|
| 136 |
+func (config *configuration) UnmarshalJSON(b []byte) error {
|
|
| 137 |
+ var ( |
|
| 138 |
+ err error |
|
| 139 |
+ nMap map[string]interface{}
|
|
| 140 |
+ ) |
|
| 141 |
+ |
|
| 142 |
+ if err = json.Unmarshal(b, &nMap); err != nil {
|
|
| 143 |
+ return err |
|
| 144 |
+ } |
|
| 145 |
+ config.ID = nMap["ID"].(string) |
|
| 146 |
+ config.Mtu = int(nMap["Mtu"].(float64)) |
|
| 147 |
+ config.HostIface = nMap["HostIface"].(string) |
|
| 148 |
+ config.IpvlanMode = nMap["IpvlanMode"].(string) |
|
| 149 |
+ config.CreatedSlaveLink = nMap["CreatedSubIface"].(bool) |
|
| 150 |
+ if v, ok := nMap["Ipv4Subnets"]; ok {
|
|
| 151 |
+ if err := json.Unmarshal([]byte(v.(string)), &config.Ipv4Subnets); err != nil {
|
|
| 152 |
+ return err |
|
| 153 |
+ } |
|
| 154 |
+ } |
|
| 155 |
+ if v, ok := nMap["Ipv6Subnets"]; ok {
|
|
| 156 |
+ if err := json.Unmarshal([]byte(v.(string)), &config.Ipv6Subnets); err != nil {
|
|
| 157 |
+ return err |
|
| 158 |
+ } |
|
| 159 |
+ } |
|
| 160 |
+ return nil |
|
| 161 |
+} |
|
| 162 |
+ |
|
| 163 |
+func (config *configuration) Key() []string {
|
|
| 164 |
+ return []string{ipvlanPrefix, config.ID}
|
|
| 165 |
+} |
|
| 166 |
+ |
|
| 167 |
+func (config *configuration) KeyPrefix() []string {
|
|
| 168 |
+ return []string{ipvlanPrefix}
|
|
| 169 |
+} |
|
| 170 |
+ |
|
| 171 |
+func (config *configuration) Value() []byte {
|
|
| 172 |
+ b, err := json.Marshal(config) |
|
| 173 |
+ if err != nil {
|
|
| 174 |
+ return nil |
|
| 175 |
+ } |
|
| 176 |
+ return b |
|
| 177 |
+} |
|
| 178 |
+ |
|
| 179 |
+func (config *configuration) SetValue(value []byte) error {
|
|
| 180 |
+ return json.Unmarshal(value, config) |
|
| 181 |
+} |
|
| 182 |
+ |
|
| 183 |
+func (config *configuration) Index() uint64 {
|
|
| 184 |
+ return config.dbIndex |
|
| 185 |
+} |
|
| 186 |
+ |
|
| 187 |
+func (config *configuration) SetIndex(index uint64) {
|
|
| 188 |
+ config.dbIndex = index |
|
| 189 |
+ config.dbExists = true |
|
| 190 |
+} |
|
| 191 |
+ |
|
| 192 |
+func (config *configuration) Exists() bool {
|
|
| 193 |
+ return config.dbExists |
|
| 194 |
+} |
|
| 195 |
+ |
|
| 196 |
+func (config *configuration) Skip() bool {
|
|
| 197 |
+ return false |
|
| 198 |
+} |
|
| 199 |
+ |
|
| 200 |
+func (config *configuration) New() datastore.KVObject {
|
|
| 201 |
+ return &configuration{}
|
|
| 202 |
+} |
|
| 203 |
+ |
|
| 204 |
+func (config *configuration) CopyTo(o datastore.KVObject) error {
|
|
| 205 |
+ dstNcfg := o.(*configuration) |
|
| 206 |
+ *dstNcfg = *config |
|
| 207 |
+ return nil |
|
| 208 |
+} |
|
| 209 |
+ |
|
| 210 |
+func (config *configuration) DataScope() string {
|
|
| 211 |
+ return datastore.LocalScope |
|
| 212 |
+} |
| 0 | 213 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,60 @@ |
| 0 |
+package ipvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "testing" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/docker/libnetwork/driverapi" |
|
| 6 |
+ _ "github.com/docker/libnetwork/testutils" |
|
| 7 |
+) |
|
| 8 |
+ |
|
| 9 |
+const testNetworkType = "ipvlan" |
|
| 10 |
+ |
|
| 11 |
+type driverTester struct {
|
|
| 12 |
+ t *testing.T |
|
| 13 |
+ d *driver |
|
| 14 |
+} |
|
| 15 |
+ |
|
| 16 |
+func (dt *driverTester) RegisterDriver(name string, drv driverapi.Driver, |
|
| 17 |
+ cap driverapi.Capability) error {
|
|
| 18 |
+ if name != testNetworkType {
|
|
| 19 |
+ dt.t.Fatalf("Expected driver register name to be %q. Instead got %q",
|
|
| 20 |
+ testNetworkType, name) |
|
| 21 |
+ } |
|
| 22 |
+ |
|
| 23 |
+ if _, ok := drv.(*driver); !ok {
|
|
| 24 |
+ dt.t.Fatalf("Expected driver type to be %T. Instead got %T",
|
|
| 25 |
+ &driver{}, drv)
|
|
| 26 |
+ } |
|
| 27 |
+ |
|
| 28 |
+ dt.d = drv.(*driver) |
|
| 29 |
+ return nil |
|
| 30 |
+} |
|
| 31 |
+ |
|
| 32 |
+func TestIpvlanInit(t *testing.T) {
|
|
| 33 |
+ if err := Init(&driverTester{t: t}, nil); err != nil {
|
|
| 34 |
+ t.Fatal(err) |
|
| 35 |
+ } |
|
| 36 |
+} |
|
| 37 |
+ |
|
| 38 |
+func TestIpvlanNilConfig(t *testing.T) {
|
|
| 39 |
+ dt := &driverTester{t: t}
|
|
| 40 |
+ if err := Init(dt, nil); err != nil {
|
|
| 41 |
+ t.Fatal(err) |
|
| 42 |
+ } |
|
| 43 |
+ |
|
| 44 |
+ if err := dt.d.initStore(nil); err != nil {
|
|
| 45 |
+ t.Fatal(err) |
|
| 46 |
+ } |
|
| 47 |
+} |
|
| 48 |
+ |
|
| 49 |
+func TestIpvlanType(t *testing.T) {
|
|
| 50 |
+ dt := &driverTester{t: t}
|
|
| 51 |
+ if err := Init(dt, nil); err != nil {
|
|
| 52 |
+ t.Fatal(err) |
|
| 53 |
+ } |
|
| 54 |
+ |
|
| 55 |
+ if dt.d.Type() != testNetworkType {
|
|
| 56 |
+ t.Fatalf("Expected Type() to return %q. Instead got %q", testNetworkType,
|
|
| 57 |
+ dt.d.Type()) |
|
| 58 |
+ } |
|
| 59 |
+} |
| 0 | 60 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,88 @@ |
| 0 |
+package macvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "net" |
|
| 4 |
+ "sync" |
|
| 5 |
+ |
|
| 6 |
+ "github.com/Sirupsen/logrus" |
|
| 7 |
+ "github.com/docker/libnetwork/datastore" |
|
| 8 |
+ "github.com/docker/libnetwork/discoverapi" |
|
| 9 |
+ "github.com/docker/libnetwork/driverapi" |
|
| 10 |
+ "github.com/docker/libnetwork/osl" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+const ( |
|
| 14 |
+ vethLen = 7 |
|
| 15 |
+ containerVethPrefix = "eth" |
|
| 16 |
+ vethPrefix = "veth" |
|
| 17 |
+ macvlanType = "macvlan" // driver type name |
|
| 18 |
+ modePrivate = "private" // macvlan mode private |
|
| 19 |
+ modeVepa = "vepa" // macvlan mode vepa |
|
| 20 |
+ modeBridge = "bridge" // macvlan mode bridge |
|
| 21 |
+ modePassthru = "passthru" // macvlan mode passthrough |
|
| 22 |
+ hostIfaceOpt = "host_iface" // host interface -o host_iface |
|
| 23 |
+ modeOpt = "_mode" // macvlan mode ux opt suffix |
|
| 24 |
+) |
|
| 25 |
+ |
|
| 26 |
+var driverModeOpt = macvlanType + modeOpt // mode --option macvlan_mode |
|
| 27 |
+ |
|
| 28 |
+type endpointTable map[string]*endpoint |
|
| 29 |
+ |
|
| 30 |
+type networkTable map[string]*network |
|
| 31 |
+ |
|
| 32 |
+type driver struct {
|
|
| 33 |
+ networks networkTable |
|
| 34 |
+ sync.Once |
|
| 35 |
+ sync.Mutex |
|
| 36 |
+ store datastore.DataStore |
|
| 37 |
+} |
|
| 38 |
+ |
|
| 39 |
+type endpoint struct {
|
|
| 40 |
+ id string |
|
| 41 |
+ mac net.HardwareAddr |
|
| 42 |
+ addr *net.IPNet |
|
| 43 |
+ addrv6 *net.IPNet |
|
| 44 |
+ srcName string |
|
| 45 |
+} |
|
| 46 |
+ |
|
| 47 |
+type network struct {
|
|
| 48 |
+ id string |
|
| 49 |
+ sbox osl.Sandbox |
|
| 50 |
+ endpoints endpointTable |
|
| 51 |
+ driver *driver |
|
| 52 |
+ config *configuration |
|
| 53 |
+ sync.Mutex |
|
| 54 |
+} |
|
| 55 |
+ |
|
| 56 |
+// Init initializes and registers the libnetwork macvlan driver |
|
| 57 |
+func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
|
|
| 58 |
+ if err := kernelSupport(macvlanType); err != nil {
|
|
| 59 |
+ logrus.Warnf("encountered errors loading the macvlan kernel module: %v", err)
|
|
| 60 |
+ } |
|
| 61 |
+ c := driverapi.Capability{
|
|
| 62 |
+ DataScope: datastore.LocalScope, |
|
| 63 |
+ } |
|
| 64 |
+ d := &driver{
|
|
| 65 |
+ networks: networkTable{},
|
|
| 66 |
+ } |
|
| 67 |
+ d.initStore(config) |
|
| 68 |
+ return dc.RegisterDriver(macvlanType, d, c) |
|
| 69 |
+} |
|
| 70 |
+ |
|
| 71 |
+func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
|
|
| 72 |
+ return make(map[string]interface{}, 0), nil
|
|
| 73 |
+} |
|
| 74 |
+ |
|
| 75 |
+func (d *driver) Type() string {
|
|
| 76 |
+ return macvlanType |
|
| 77 |
+} |
|
| 78 |
+ |
|
| 79 |
+// DiscoverNew is a notification for a new discovery event |
|
| 80 |
+func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
|
|
| 81 |
+ return nil |
|
| 82 |
+} |
|
| 83 |
+ |
|
| 84 |
+// DiscoverDelete is a notification for a discovery delete event |
|
| 85 |
+func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
|
|
| 86 |
+ return nil |
|
| 87 |
+} |
| 0 | 88 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,80 @@ |
| 0 |
+package macvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/docker/libnetwork/driverapi" |
|
| 6 |
+ "github.com/docker/libnetwork/netlabel" |
|
| 7 |
+ "github.com/docker/libnetwork/netutils" |
|
| 8 |
+ "github.com/docker/libnetwork/types" |
|
| 9 |
+ "github.com/vishvananda/netlink" |
|
| 10 |
+) |
|
| 11 |
+ |
|
| 12 |
+// CreateEndpoint assigns the mac, ip and endpoint id for the new container |
|
| 13 |
+func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, |
|
| 14 |
+ epOptions map[string]interface{}) error {
|
|
| 15 |
+ |
|
| 16 |
+ if err := validateID(nid, eid); err != nil {
|
|
| 17 |
+ return err |
|
| 18 |
+ } |
|
| 19 |
+ n, err := d.getNetwork(nid) |
|
| 20 |
+ if err != nil {
|
|
| 21 |
+ return fmt.Errorf("network id %q not found", nid)
|
|
| 22 |
+ } |
|
| 23 |
+ if ifInfo.MacAddress() != nil {
|
|
| 24 |
+ return fmt.Errorf("%s interfaces do not support custom mac address assigment", macvlanType)
|
|
| 25 |
+ } |
|
| 26 |
+ ep := &endpoint{
|
|
| 27 |
+ id: eid, |
|
| 28 |
+ addr: ifInfo.Address(), |
|
| 29 |
+ addrv6: ifInfo.AddressIPv6(), |
|
| 30 |
+ mac: ifInfo.MacAddress(), |
|
| 31 |
+ } |
|
| 32 |
+ if ep.addr == nil {
|
|
| 33 |
+ return fmt.Errorf("create endpoint was not passed an IP address")
|
|
| 34 |
+ } |
|
| 35 |
+ if ep.mac == nil {
|
|
| 36 |
+ ep.mac = netutils.GenerateMACFromIP(ep.addr.IP) |
|
| 37 |
+ if err := ifInfo.SetMacAddress(ep.mac); err != nil {
|
|
| 38 |
+ return err |
|
| 39 |
+ } |
|
| 40 |
+ } |
|
| 41 |
+ // disallow portmapping -p |
|
| 42 |
+ if opt, ok := epOptions[netlabel.PortMap]; ok {
|
|
| 43 |
+ if _, ok := opt.([]types.PortBinding); ok {
|
|
| 44 |
+ if len(opt.([]types.PortBinding)) > 0 {
|
|
| 45 |
+ return fmt.Errorf("%s driver does not support port mappings", macvlanType)
|
|
| 46 |
+ } |
|
| 47 |
+ } |
|
| 48 |
+ } |
|
| 49 |
+ // disallow port exposure --expose |
|
| 50 |
+ if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
|
|
| 51 |
+ if _, ok := opt.([]types.TransportPort); ok {
|
|
| 52 |
+ if len(opt.([]types.TransportPort)) > 0 {
|
|
| 53 |
+ return fmt.Errorf("%s driver does not support port exposures", macvlanType)
|
|
| 54 |
+ } |
|
| 55 |
+ } |
|
| 56 |
+ } |
|
| 57 |
+ n.addEndpoint(ep) |
|
| 58 |
+ |
|
| 59 |
+ return nil |
|
| 60 |
+} |
|
| 61 |
+ |
|
| 62 |
+// DeleteEndpoint remove the endpoint and associated netlink interface |
|
| 63 |
+func (d *driver) DeleteEndpoint(nid, eid string) error {
|
|
| 64 |
+ if err := validateID(nid, eid); err != nil {
|
|
| 65 |
+ return err |
|
| 66 |
+ } |
|
| 67 |
+ n := d.network(nid) |
|
| 68 |
+ if n == nil {
|
|
| 69 |
+ return fmt.Errorf("network id %q not found", nid)
|
|
| 70 |
+ } |
|
| 71 |
+ ep := n.endpoint(eid) |
|
| 72 |
+ if ep == nil {
|
|
| 73 |
+ return fmt.Errorf("endpoint id %q not found", eid)
|
|
| 74 |
+ } |
|
| 75 |
+ if link, err := netlink.LinkByName(ep.srcName); err == nil {
|
|
| 76 |
+ netlink.LinkDel(link) |
|
| 77 |
+ } |
|
| 78 |
+ return nil |
|
| 79 |
+} |
| 0 | 80 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,136 @@ |
| 0 |
+package macvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ "net" |
|
| 5 |
+ |
|
| 6 |
+ "github.com/Sirupsen/logrus" |
|
| 7 |
+ "github.com/docker/libnetwork/driverapi" |
|
| 8 |
+ "github.com/docker/libnetwork/netutils" |
|
| 9 |
+ "github.com/docker/libnetwork/osl" |
|
| 10 |
+) |
|
| 11 |
+ |
|
| 12 |
+// Join method is invoked when a Sandbox is attached to an endpoint. |
|
| 13 |
+func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
|
|
| 14 |
+ defer osl.InitOSContext()() |
|
| 15 |
+ n, err := d.getNetwork(nid) |
|
| 16 |
+ if err != nil {
|
|
| 17 |
+ return err |
|
| 18 |
+ } |
|
| 19 |
+ endpoint := n.endpoint(eid) |
|
| 20 |
+ if endpoint == nil {
|
|
| 21 |
+ return fmt.Errorf("could not find endpoint with id %s", eid)
|
|
| 22 |
+ } |
|
| 23 |
+ // generate a name for the iface that will be renamed to eth0 in the sbox |
|
| 24 |
+ containerIfName, err := netutils.GenerateIfaceName(vethPrefix, vethLen) |
|
| 25 |
+ if err != nil {
|
|
| 26 |
+ return fmt.Errorf("error generating an interface name: %s", err)
|
|
| 27 |
+ } |
|
| 28 |
+ // create the netlink macvlan interface |
|
| 29 |
+ vethName, err := createMacVlan(containerIfName, n.config.HostIface, n.config.MacvlanMode) |
|
| 30 |
+ if err != nil {
|
|
| 31 |
+ return err |
|
| 32 |
+ } |
|
| 33 |
+ // bind the generated iface name to the endpoint |
|
| 34 |
+ endpoint.srcName = vethName |
|
| 35 |
+ ep := n.endpoint(eid) |
|
| 36 |
+ if ep == nil {
|
|
| 37 |
+ return fmt.Errorf("could not find endpoint with id %s", eid)
|
|
| 38 |
+ } |
|
| 39 |
+ // parse and match the endpoint address with the available v4 subnets |
|
| 40 |
+ if len(n.config.Ipv4Subnets) > 0 {
|
|
| 41 |
+ s := n.getSubnetforIPv4(ep.addr) |
|
| 42 |
+ if s == nil {
|
|
| 43 |
+ return fmt.Errorf("could not find a valid ipv4 subnet for endpoint %s", eid)
|
|
| 44 |
+ } |
|
| 45 |
+ v4gw, _, err := net.ParseCIDR(s.GwIP) |
|
| 46 |
+ if err != nil {
|
|
| 47 |
+ return fmt.Errorf("gatway %s is not a valid ipv4 address: %v", s.GwIP, err)
|
|
| 48 |
+ } |
|
| 49 |
+ err = jinfo.SetGateway(v4gw) |
|
| 50 |
+ if err != nil {
|
|
| 51 |
+ return err |
|
| 52 |
+ } |
|
| 53 |
+ logrus.Debugf("Macvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, MacVlan_Mode: %s, Host_Iface: %s",
|
|
| 54 |
+ ep.addr.IP.String(), v4gw.String(), n.config.MacvlanMode, n.config.HostIface) |
|
| 55 |
+ } |
|
| 56 |
+ // parse and match the endpoint address with the available v6 subnets |
|
| 57 |
+ if len(n.config.Ipv6Subnets) > 0 {
|
|
| 58 |
+ s := n.getSubnetforIPv6(ep.addrv6) |
|
| 59 |
+ if s == nil {
|
|
| 60 |
+ return fmt.Errorf("could not find a valid ipv6 subnet for endpoint %s", eid)
|
|
| 61 |
+ } |
|
| 62 |
+ v6gw, _, err := net.ParseCIDR(s.GwIP) |
|
| 63 |
+ if err != nil {
|
|
| 64 |
+ return fmt.Errorf("gatway %s is not a valid ipv6 address: %v", s.GwIP, err)
|
|
| 65 |
+ } |
|
| 66 |
+ err = jinfo.SetGatewayIPv6(v6gw) |
|
| 67 |
+ if err != nil {
|
|
| 68 |
+ return err |
|
| 69 |
+ } |
|
| 70 |
+ logrus.Debugf("Macvlan Endpoint Joined with IPv6_Addr: %s Gateway: %s MacVlan_Mode: %s, Host_Iface: %s",
|
|
| 71 |
+ ep.addrv6.IP.String(), v6gw.String(), n.config.MacvlanMode, n.config.HostIface) |
|
| 72 |
+ } |
|
| 73 |
+ iNames := jinfo.InterfaceName() |
|
| 74 |
+ err = iNames.SetNames(vethName, containerVethPrefix) |
|
| 75 |
+ if err != nil {
|
|
| 76 |
+ return err |
|
| 77 |
+ } |
|
| 78 |
+ return nil |
|
| 79 |
+} |
|
| 80 |
+ |
|
| 81 |
+// Leave method is invoked when a Sandbox detaches from an endpoint. |
|
| 82 |
+func (d *driver) Leave(nid, eid string) error {
|
|
| 83 |
+ network, err := d.getNetwork(nid) |
|
| 84 |
+ if err != nil {
|
|
| 85 |
+ return err |
|
| 86 |
+ } |
|
| 87 |
+ endpoint, err := network.getEndpoint(eid) |
|
| 88 |
+ if err != nil {
|
|
| 89 |
+ return err |
|
| 90 |
+ } |
|
| 91 |
+ if endpoint == nil {
|
|
| 92 |
+ return fmt.Errorf("could not find endpoint with id %s", eid)
|
|
| 93 |
+ } |
|
| 94 |
+ return nil |
|
| 95 |
+} |
|
| 96 |
+ |
|
| 97 |
+// getSubnetforIP returns the ipv4 subnet to which the given IP belongs |
|
| 98 |
+func (n *network) getSubnetforIPv4(ip *net.IPNet) *ipv4Subnet {
|
|
| 99 |
+ for _, s := range n.config.Ipv4Subnets {
|
|
| 100 |
+ _, snet, err := net.ParseCIDR(s.SubnetIP) |
|
| 101 |
+ if err != nil {
|
|
| 102 |
+ return nil |
|
| 103 |
+ } |
|
| 104 |
+ // first check if the mask lengths are the same |
|
| 105 |
+ i, _ := snet.Mask.Size() |
|
| 106 |
+ j, _ := ip.Mask.Size() |
|
| 107 |
+ if i != j {
|
|
| 108 |
+ continue |
|
| 109 |
+ } |
|
| 110 |
+ if snet.Contains(ip.IP) {
|
|
| 111 |
+ return s |
|
| 112 |
+ } |
|
| 113 |
+ } |
|
| 114 |
+ return nil |
|
| 115 |
+} |
|
| 116 |
+ |
|
| 117 |
+// getSubnetforIPv6 returns the ipv6 subnet to which the given IP belongs |
|
| 118 |
+func (n *network) getSubnetforIPv6(ip *net.IPNet) *ipv6Subnet {
|
|
| 119 |
+ for _, s := range n.config.Ipv6Subnets {
|
|
| 120 |
+ _, snet, err := net.ParseCIDR(s.SubnetIP) |
|
| 121 |
+ if err != nil {
|
|
| 122 |
+ return nil |
|
| 123 |
+ } |
|
| 124 |
+ // first check if the mask lengths are the same |
|
| 125 |
+ i, _ := snet.Mask.Size() |
|
| 126 |
+ j, _ := ip.Mask.Size() |
|
| 127 |
+ if i != j {
|
|
| 128 |
+ continue |
|
| 129 |
+ } |
|
| 130 |
+ if snet.Contains(ip.IP) {
|
|
| 131 |
+ return s |
|
| 132 |
+ } |
|
| 133 |
+ } |
|
| 134 |
+ return nil |
|
| 135 |
+} |
| 0 | 136 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,192 @@ |
| 0 |
+package macvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/Sirupsen/logrus" |
|
| 6 |
+ "github.com/docker/docker/pkg/stringid" |
|
| 7 |
+ "github.com/docker/libnetwork/driverapi" |
|
| 8 |
+ "github.com/docker/libnetwork/netlabel" |
|
| 9 |
+ "github.com/docker/libnetwork/options" |
|
| 10 |
+ "github.com/docker/libnetwork/types" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+// CreateNetwork the network for the specified driver type |
|
| 14 |
+func (d *driver) CreateNetwork(id string, option map[string]interface{}, ipV4Data, ipV6Data []driverapi.IPAMData) error {
|
|
| 15 |
+ // parse and validate the config and bind to networkConfiguration |
|
| 16 |
+ config, err := parseNetworkOptions(id, option) |
|
| 17 |
+ if err != nil {
|
|
| 18 |
+ return err |
|
| 19 |
+ } |
|
| 20 |
+ config.ID = id |
|
| 21 |
+ err = config.processIPAM(id, ipV4Data, ipV6Data) |
|
| 22 |
+ if err != nil {
|
|
| 23 |
+ return err |
|
| 24 |
+ } |
|
| 25 |
+ // user must specify a parent interface on the host to attach the container vif -o host_iface=eth0 |
|
| 26 |
+ if config.HostIface == "" {
|
|
| 27 |
+ return fmt.Errorf("%s requires an interface from the docker host to be specified (usage: -o host_iface=eth)", macvlanType)
|
|
| 28 |
+ } |
|
| 29 |
+ // loopback is not a valid parent link |
|
| 30 |
+ if config.HostIface == "lo" {
|
|
| 31 |
+ return fmt.Errorf("loopback interface is not a valid %s parent link", macvlanType)
|
|
| 32 |
+ } |
|
| 33 |
+ // verify the macvlan mode from -o macvlan_mode option |
|
| 34 |
+ switch config.MacvlanMode {
|
|
| 35 |
+ case "", modeBridge: |
|
| 36 |
+ // default to macvlan bridge mode if -o macvlan_mode is empty |
|
| 37 |
+ config.MacvlanMode = modeBridge |
|
| 38 |
+ case modeOpt: |
|
| 39 |
+ config.MacvlanMode = modeOpt |
|
| 40 |
+ case modePassthru: |
|
| 41 |
+ config.MacvlanMode = modePassthru |
|
| 42 |
+ case modeVepa: |
|
| 43 |
+ config.MacvlanMode = modeVepa |
|
| 44 |
+ default: |
|
| 45 |
+ return fmt.Errorf("requested macvlan mode '%s' is not valid, 'bridge' mode is the macvlan driver default", config.MacvlanMode)
|
|
| 46 |
+ } |
|
| 47 |
+ err = d.createNetwork(config) |
|
| 48 |
+ if err != nil {
|
|
| 49 |
+ return err |
|
| 50 |
+ } |
|
| 51 |
+ // update persistent db, rollback on fail |
|
| 52 |
+ err = d.storeUpdate(config) |
|
| 53 |
+ if err != nil {
|
|
| 54 |
+ d.deleteNetwork(config.ID) |
|
| 55 |
+ return err |
|
| 56 |
+ } |
|
| 57 |
+ |
|
| 58 |
+ return nil |
|
| 59 |
+} |
|
| 60 |
+ |
|
| 61 |
+// createNetwork is used by new network callbacks and persistent network cache |
|
| 62 |
+func (d *driver) createNetwork(config *configuration) error {
|
|
| 63 |
+ // if the -o host_iface does not exist, attempt to parse a parent_iface.vlan_id |
|
| 64 |
+ networkList := d.getNetworks() |
|
| 65 |
+ for _, nw := range networkList {
|
|
| 66 |
+ if config.HostIface == nw.config.HostIface {
|
|
| 67 |
+ return fmt.Errorf("network %s is already using host interface %s",
|
|
| 68 |
+ stringid.TruncateID(nw.config.ID), config.HostIface) |
|
| 69 |
+ } |
|
| 70 |
+ } |
|
| 71 |
+ // if the -o host_iface does not exist, attempt to parse a parent_iface.vlan_id |
|
| 72 |
+ if ok := hostIfaceExists(config.HostIface); !ok {
|
|
| 73 |
+ // if the subinterface parent_iface.vlan_id checks do not pass, return err. |
|
| 74 |
+ // a valid example is eth0.10 for a parent iface: eth0 with a vlan id: 10 |
|
| 75 |
+ err := createVlanLink(config.HostIface) |
|
| 76 |
+ if err != nil {
|
|
| 77 |
+ return err |
|
| 78 |
+ } |
|
| 79 |
+ // if driver created the networks slave link, record it for future deletion |
|
| 80 |
+ config.CreatedSlaveLink = true |
|
| 81 |
+ } |
|
| 82 |
+ n := &network{
|
|
| 83 |
+ id: config.ID, |
|
| 84 |
+ driver: d, |
|
| 85 |
+ endpoints: endpointTable{},
|
|
| 86 |
+ config: config, |
|
| 87 |
+ } |
|
| 88 |
+ // add the *network |
|
| 89 |
+ d.addNetwork(n) |
|
| 90 |
+ |
|
| 91 |
+ return nil |
|
| 92 |
+} |
|
| 93 |
+ |
|
| 94 |
+// DeleteNetwork the network for the specified driver type |
|
| 95 |
+func (d *driver) DeleteNetwork(nid string) error {
|
|
| 96 |
+ n := d.network(nid) |
|
| 97 |
+ // if the driver created the slave interface, delete it, otherwise leave it |
|
| 98 |
+ if ok := n.config.CreatedSlaveLink; ok {
|
|
| 99 |
+ // if the interface exists, only delete if it matches iface.vlan naming |
|
| 100 |
+ if ok := hostIfaceExists(n.config.HostIface); ok {
|
|
| 101 |
+ err := delVlanLink(n.config.HostIface) |
|
| 102 |
+ if err != nil {
|
|
| 103 |
+ logrus.Debugf("link %s was not deleted, continuing the delete network operation: %v", n.config.HostIface, err)
|
|
| 104 |
+ } |
|
| 105 |
+ } |
|
| 106 |
+ } |
|
| 107 |
+ // delete the *network |
|
| 108 |
+ d.deleteNetwork(nid) |
|
| 109 |
+ // delete the network record from persistent cache |
|
| 110 |
+ return d.storeDelete(n.config) |
|
| 111 |
+} |
|
| 112 |
+ |
|
| 113 |
+// parseNetworkOptions parse docker network options |
|
| 114 |
+func parseNetworkOptions(id string, option options.Generic) (*configuration, error) {
|
|
| 115 |
+ var ( |
|
| 116 |
+ err error |
|
| 117 |
+ config = &configuration{}
|
|
| 118 |
+ ) |
|
| 119 |
+ // parse generic labels first |
|
| 120 |
+ if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
|
|
| 121 |
+ if config, err = parseNetworkGenericOptions(genData); err != nil {
|
|
| 122 |
+ return nil, err |
|
| 123 |
+ } |
|
| 124 |
+ } |
|
| 125 |
+ // return an error if the unsupported --internal is passed |
|
| 126 |
+ if _, ok := option[netlabel.Internal]; ok {
|
|
| 127 |
+ return nil, fmt.Errorf("--internal option is not supported with the macvlan driver")
|
|
| 128 |
+ } |
|
| 129 |
+ return config, nil |
|
| 130 |
+} |
|
| 131 |
+ |
|
| 132 |
+// parseNetworkGenericOptions parse generic driver docker network options |
|
| 133 |
+func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
|
|
| 134 |
+ var ( |
|
| 135 |
+ err error |
|
| 136 |
+ config *configuration |
|
| 137 |
+ ) |
|
| 138 |
+ switch opt := data.(type) {
|
|
| 139 |
+ case *configuration: |
|
| 140 |
+ config = opt |
|
| 141 |
+ case map[string]string: |
|
| 142 |
+ config = &configuration{}
|
|
| 143 |
+ err = config.fromOptions(opt) |
|
| 144 |
+ case options.Generic: |
|
| 145 |
+ var opaqueConfig interface{}
|
|
| 146 |
+ if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
|
|
| 147 |
+ config = opaqueConfig.(*configuration) |
|
| 148 |
+ } |
|
| 149 |
+ default: |
|
| 150 |
+ err = types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
|
|
| 151 |
+ } |
|
| 152 |
+ return config, err |
|
| 153 |
+} |
|
| 154 |
+ |
|
| 155 |
+// fromOptions binds the generic options to networkConfiguration to cache |
|
| 156 |
+func (config *configuration) fromOptions(labels map[string]string) error {
|
|
| 157 |
+ for label, value := range labels {
|
|
| 158 |
+ switch label {
|
|
| 159 |
+ case hostIfaceOpt: |
|
| 160 |
+ // parse driver option '-o host_iface' |
|
| 161 |
+ config.HostIface = value |
|
| 162 |
+ case driverModeOpt: |
|
| 163 |
+ // parse driver option '-o macvlan_mode' |
|
| 164 |
+ config.MacvlanMode = value |
|
| 165 |
+ } |
|
| 166 |
+ } |
|
| 167 |
+ return nil |
|
| 168 |
+} |
|
| 169 |
+ |
|
| 170 |
+// processIPAM parses v4 and v6 IP information and binds it to the network configuration |
|
| 171 |
+func (config *configuration) processIPAM(id string, ipamV4Data, ipamV6Data []driverapi.IPAMData) error {
|
|
| 172 |
+ if len(ipamV4Data) > 0 {
|
|
| 173 |
+ for _, ipd := range ipamV4Data {
|
|
| 174 |
+ s := &ipv4Subnet{
|
|
| 175 |
+ SubnetIP: ipd.Pool.String(), |
|
| 176 |
+ GwIP: ipd.Gateway.String(), |
|
| 177 |
+ } |
|
| 178 |
+ config.Ipv4Subnets = append(config.Ipv4Subnets, s) |
|
| 179 |
+ } |
|
| 180 |
+ } |
|
| 181 |
+ if len(ipamV6Data) > 0 {
|
|
| 182 |
+ for _, ipd := range ipamV6Data {
|
|
| 183 |
+ s := &ipv6Subnet{
|
|
| 184 |
+ SubnetIP: ipd.Pool.String(), |
|
| 185 |
+ GwIP: ipd.Gateway.String(), |
|
| 186 |
+ } |
|
| 187 |
+ config.Ipv6Subnets = append(config.Ipv6Subnets, s) |
|
| 188 |
+ } |
|
| 189 |
+ } |
|
| 190 |
+ return nil |
|
| 191 |
+} |
| 0 | 192 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,175 @@ |
| 0 |
+package macvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bufio" |
|
| 4 |
+ "fmt" |
|
| 5 |
+ "os" |
|
| 6 |
+ "os/exec" |
|
| 7 |
+ "strconv" |
|
| 8 |
+ "strings" |
|
| 9 |
+ |
|
| 10 |
+ "github.com/Sirupsen/logrus" |
|
| 11 |
+ "github.com/docker/libnetwork/osl" |
|
| 12 |
+ "github.com/vishvananda/netlink" |
|
| 13 |
+) |
|
| 14 |
+ |
|
| 15 |
+// Create the netlink interface specifying the source name |
|
| 16 |
+func createMacVlan(containerIfName, hostIface, macvlanMode string) (string, error) {
|
|
| 17 |
+ defer osl.InitOSContext()() |
|
| 18 |
+ |
|
| 19 |
+ // Set the macvlan mode. Default is bridge mode |
|
| 20 |
+ mode, err := setMacVlanMode(macvlanMode) |
|
| 21 |
+ if err != nil {
|
|
| 22 |
+ return "", fmt.Errorf("Unsupported %s macvlan mode: %v", macvlanMode, err)
|
|
| 23 |
+ } |
|
| 24 |
+ // verify the Docker host interface acting as the macvlan parent iface exists |
|
| 25 |
+ if ok := hostIfaceExists(hostIface); !ok {
|
|
| 26 |
+ return "", fmt.Errorf("the requested host interface %s was not found on the Docker host", hostIface)
|
|
| 27 |
+ } |
|
| 28 |
+ // Get the link for the master index (Example: the docker host eth iface) |
|
| 29 |
+ hostEth, err := netlink.LinkByName(hostIface) |
|
| 30 |
+ if err != nil {
|
|
| 31 |
+ logrus.Errorf("error occoured looking up the parent iface %s mode: %s error: %s", hostIface, macvlanMode, err)
|
|
| 32 |
+ } |
|
| 33 |
+ // Create a macvlan link |
|
| 34 |
+ macvlan := &netlink.Macvlan{
|
|
| 35 |
+ LinkAttrs: netlink.LinkAttrs{
|
|
| 36 |
+ Name: containerIfName, |
|
| 37 |
+ ParentIndex: hostEth.Attrs().Index, |
|
| 38 |
+ }, |
|
| 39 |
+ Mode: mode, |
|
| 40 |
+ } |
|
| 41 |
+ if err := netlink.LinkAdd(macvlan); err != nil {
|
|
| 42 |
+ // verbose but will be an issue if user creates a macvlan and ipvlan on same parent.Netlink msg is uninformative |
|
| 43 |
+ logrus.Warn("Ensure there are no ipvlan networks using the same `-o host_iface` as the macvlan network.")
|
|
| 44 |
+ return "", fmt.Errorf("Failed to create macvlan link: %s with the error: %v", macvlan.Name, err)
|
|
| 45 |
+ } |
|
| 46 |
+ return macvlan.Attrs().Name, nil |
|
| 47 |
+} |
|
| 48 |
+ |
|
| 49 |
+// setMacVlanMode setter for one of the four macvlan port types |
|
| 50 |
+func setMacVlanMode(mode string) (netlink.MacvlanMode, error) {
|
|
| 51 |
+ switch mode {
|
|
| 52 |
+ case modePrivate: |
|
| 53 |
+ return netlink.MACVLAN_MODE_PRIVATE, nil |
|
| 54 |
+ case modeVepa: |
|
| 55 |
+ return netlink.MACVLAN_MODE_VEPA, nil |
|
| 56 |
+ case modeBridge: |
|
| 57 |
+ return netlink.MACVLAN_MODE_BRIDGE, nil |
|
| 58 |
+ case modePassthru: |
|
| 59 |
+ return netlink.MACVLAN_MODE_PASSTHRU, nil |
|
| 60 |
+ default: |
|
| 61 |
+ return 0, fmt.Errorf("unknown macvlan mode: %s", mode)
|
|
| 62 |
+ } |
|
| 63 |
+} |
|
| 64 |
+ |
|
| 65 |
+// validateHostIface check if the specified interface exists in the default namespace |
|
| 66 |
+func hostIfaceExists(ifaceStr string) bool {
|
|
| 67 |
+ _, err := netlink.LinkByName(ifaceStr) |
|
| 68 |
+ if err != nil {
|
|
| 69 |
+ return false |
|
| 70 |
+ } |
|
| 71 |
+ return true |
|
| 72 |
+} |
|
| 73 |
+ |
|
| 74 |
+// kernelSupport for the necessary kernel module for the driver type |
|
| 75 |
+func kernelSupport(networkTpe string) error {
|
|
| 76 |
+ // attempt to load the module, silent if successful or already loaded |
|
| 77 |
+ exec.Command("modprobe", macvlanType).Run()
|
|
| 78 |
+ f, err := os.Open("/proc/modules")
|
|
| 79 |
+ if err != nil {
|
|
| 80 |
+ return err |
|
| 81 |
+ } |
|
| 82 |
+ defer f.Close() |
|
| 83 |
+ s := bufio.NewScanner(f) |
|
| 84 |
+ for s.Scan() {
|
|
| 85 |
+ if strings.Contains(s.Text(), macvlanType) {
|
|
| 86 |
+ return nil |
|
| 87 |
+ } |
|
| 88 |
+ } |
|
| 89 |
+ return fmt.Errorf("required kernel module '%s' was not found in /proc/modules, kernel version >= 3.19 is recommended", macvlanType)
|
|
| 90 |
+} |
|
| 91 |
+ |
|
| 92 |
+// createVlanLink parses sub-interfaces and vlan id for creation |
|
| 93 |
+func createVlanLink(ifaceName string) error {
|
|
| 94 |
+ if strings.Contains(ifaceName, ".") {
|
|
| 95 |
+ parentIface, vidInt, err := parseVlan(ifaceName) |
|
| 96 |
+ if err != nil {
|
|
| 97 |
+ return err |
|
| 98 |
+ } |
|
| 99 |
+ // VLAN identifier or VID is a 12-bit field specifying the VLAN to which the frame belongs |
|
| 100 |
+ if vidInt > 4094 || vidInt < 1 {
|
|
| 101 |
+ return fmt.Errorf("vlan id must be between 1-4094, received: %d", vidInt)
|
|
| 102 |
+ } |
|
| 103 |
+ // get the parent link to attach a vlan subinterface |
|
| 104 |
+ hostIface, err := netlink.LinkByName(parentIface) |
|
| 105 |
+ if err != nil {
|
|
| 106 |
+ return fmt.Errorf("failed to find master interface %s on the Docker host: %v", parentIface, err)
|
|
| 107 |
+ } |
|
| 108 |
+ vlanIface := &netlink.Vlan{
|
|
| 109 |
+ LinkAttrs: netlink.LinkAttrs{
|
|
| 110 |
+ Name: ifaceName, |
|
| 111 |
+ ParentIndex: hostIface.Attrs().Index, |
|
| 112 |
+ }, |
|
| 113 |
+ VlanId: vidInt, |
|
| 114 |
+ } |
|
| 115 |
+ // create the subinterface |
|
| 116 |
+ if err := netlink.LinkAdd(vlanIface); err != nil {
|
|
| 117 |
+ return fmt.Errorf("failed to create %s vlan link: %v", vlanIface.Name, err)
|
|
| 118 |
+ } |
|
| 119 |
+ // Bring the new netlink iface up |
|
| 120 |
+ if err := netlink.LinkSetUp(vlanIface); err != nil {
|
|
| 121 |
+ return fmt.Errorf("failed to enable %s the macvlan netlink link: %v", vlanIface.Name, err)
|
|
| 122 |
+ } |
|
| 123 |
+ logrus.Debugf("Added a vlan tagged netlink subinterface: %s with a vlan id: %d", ifaceName, vidInt)
|
|
| 124 |
+ return nil |
|
| 125 |
+ } |
|
| 126 |
+ return fmt.Errorf("invalid subinterface vlan name %s, example formatting is eth0.10", ifaceName)
|
|
| 127 |
+} |
|
| 128 |
+ |
|
| 129 |
+// verifyVlanDel verifies only sub-interfaces with a vlan id get deleted |
|
| 130 |
+func delVlanLink(ifaceName string) error {
|
|
| 131 |
+ if strings.Contains(ifaceName, ".") {
|
|
| 132 |
+ _, _, err := parseVlan(ifaceName) |
|
| 133 |
+ if err != nil {
|
|
| 134 |
+ return err |
|
| 135 |
+ } |
|
| 136 |
+ // delete the vlan subinterface |
|
| 137 |
+ vlanIface, err := netlink.LinkByName(ifaceName) |
|
| 138 |
+ if err != nil {
|
|
| 139 |
+ return fmt.Errorf("failed to find interface %s on the Docker host : %v", ifaceName, err)
|
|
| 140 |
+ } |
|
| 141 |
+ // verify a parent interface isn't being deleted |
|
| 142 |
+ if vlanIface.Attrs().ParentIndex == 0 {
|
|
| 143 |
+ return fmt.Errorf("interface %s does not appear to be a slave device: %v", ifaceName, err)
|
|
| 144 |
+ } |
|
| 145 |
+ // delete the macvlan slave device |
|
| 146 |
+ if err := netlink.LinkDel(vlanIface); err != nil {
|
|
| 147 |
+ return fmt.Errorf("failed to delete %s link: %v", ifaceName, err)
|
|
| 148 |
+ } |
|
| 149 |
+ logrus.Debugf("Deleted a vlan tagged netlink subinterface: %s", ifaceName)
|
|
| 150 |
+ } |
|
| 151 |
+ // if the subinterface doesn't parse to iface.vlan_id leave the interface in |
|
| 152 |
+ // place since it could be a user specified name not created by the driver. |
|
| 153 |
+ return nil |
|
| 154 |
+} |
|
| 155 |
+ |
|
| 156 |
+// parseVlan parses and verifies a slave interface name: -o host_iface=eth0.10 |
|
| 157 |
+func parseVlan(ifaceName string) (string, int, error) {
|
|
| 158 |
+ // parse -o host_iface=eth0.10 |
|
| 159 |
+ splitIface := strings.Split(ifaceName, ".") |
|
| 160 |
+ if len(splitIface) != 2 {
|
|
| 161 |
+ return "", 0, fmt.Errorf("required interface name format is: name.vlan_id, ex. eth0.10 for vlan 10, instead received %s", ifaceName)
|
|
| 162 |
+ } |
|
| 163 |
+ parentIface, vidStr := splitIface[0], splitIface[1] |
|
| 164 |
+ // validate type and convert vlan id to int |
|
| 165 |
+ vidInt, err := strconv.Atoi(vidStr) |
|
| 166 |
+ if err != nil {
|
|
| 167 |
+ return "", 0, fmt.Errorf("unable to parse a valid vlan id from: %s (ex. eth0.10 for vlan 10)", vidStr)
|
|
| 168 |
+ } |
|
| 169 |
+ // Check if the interface exists |
|
| 170 |
+ if ok := hostIfaceExists(parentIface); !ok {
|
|
| 171 |
+ return "", 0, fmt.Errorf("-o host_iface parent interface does was not found on the host: %s", parentIface)
|
|
| 172 |
+ } |
|
| 173 |
+ return parentIface, vidInt, nil |
|
| 174 |
+} |
| 0 | 175 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,103 @@ |
| 0 |
+package macvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "testing" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/vishvananda/netlink" |
|
| 6 |
+) |
|
| 7 |
+ |
|
| 8 |
+// TestValidateLink tests the validateHostIface function |
|
| 9 |
+func TestValidateLink(t *testing.T) {
|
|
| 10 |
+ validIface := "lo" |
|
| 11 |
+ invalidIface := "foo12345" |
|
| 12 |
+ |
|
| 13 |
+ // test a valid parent interface validation |
|
| 14 |
+ if ok := hostIfaceExists(validIface); !ok {
|
|
| 15 |
+ t.Fatalf("failed validating loopback %s", validIface)
|
|
| 16 |
+ } |
|
| 17 |
+ // test a invalid parent interface validation |
|
| 18 |
+ if ok := hostIfaceExists(invalidIface); ok {
|
|
| 19 |
+ t.Fatalf("failed to invalidate interface %s", invalidIface)
|
|
| 20 |
+ } |
|
| 21 |
+} |
|
| 22 |
+ |
|
| 23 |
+// TestValidateSubLink tests valid 802.1q naming convention |
|
| 24 |
+func TestValidateSubLink(t *testing.T) {
|
|
| 25 |
+ validSubIface := "lo.10" |
|
| 26 |
+ invalidSubIface1 := "lo" |
|
| 27 |
+ invalidSubIface2 := "lo:10" |
|
| 28 |
+ invalidSubIface3 := "foo123.456" |
|
| 29 |
+ |
|
| 30 |
+ // test a valid parent_iface.vlan_id |
|
| 31 |
+ _, _, err := parseVlan(validSubIface) |
|
| 32 |
+ if err != nil {
|
|
| 33 |
+ t.Fatalf("failed subinterface validation: %v", err)
|
|
| 34 |
+ } |
|
| 35 |
+ // test a invalid vid with a valid parent link |
|
| 36 |
+ _, _, err = parseVlan(invalidSubIface1) |
|
| 37 |
+ if err == nil {
|
|
| 38 |
+ t.Fatalf("failed subinterface validation test: %s", invalidSubIface1)
|
|
| 39 |
+ } |
|
| 40 |
+ // test a valid vid with a valid parent link with a invalid delimiter |
|
| 41 |
+ _, _, err = parseVlan(invalidSubIface2) |
|
| 42 |
+ if err == nil {
|
|
| 43 |
+ t.Fatalf("failed subinterface validation test: %v", invalidSubIface2)
|
|
| 44 |
+ } |
|
| 45 |
+ // test a invalid parent link with a valid vid |
|
| 46 |
+ _, _, err = parseVlan(invalidSubIface3) |
|
| 47 |
+ if err == nil {
|
|
| 48 |
+ t.Fatalf("failed subinterface validation test: %v", invalidSubIface3)
|
|
| 49 |
+ } |
|
| 50 |
+} |
|
| 51 |
+ |
|
| 52 |
+// TestSetMacVlanMode tests the macvlan mode setter |
|
| 53 |
+func TestSetMacVlanMode(t *testing.T) {
|
|
| 54 |
+ // test macvlan bridge mode |
|
| 55 |
+ mode, err := setMacVlanMode(modeBridge) |
|
| 56 |
+ if err != nil {
|
|
| 57 |
+ t.Fatalf("error parsing %v vlan mode: %v", mode, err)
|
|
| 58 |
+ } |
|
| 59 |
+ if mode != netlink.MACVLAN_MODE_BRIDGE {
|
|
| 60 |
+ t.Fatalf("expected %d got %d", netlink.MACVLAN_MODE_BRIDGE, mode)
|
|
| 61 |
+ } |
|
| 62 |
+ // test macvlan passthrough mode |
|
| 63 |
+ mode, err = setMacVlanMode(modePassthru) |
|
| 64 |
+ if err != nil {
|
|
| 65 |
+ t.Fatalf("error parsing %v vlan mode: %v", mode, err)
|
|
| 66 |
+ } |
|
| 67 |
+ if mode != netlink.MACVLAN_MODE_PASSTHRU {
|
|
| 68 |
+ t.Fatalf("expected %d got %d", netlink.MACVLAN_MODE_PASSTHRU, mode)
|
|
| 69 |
+ } |
|
| 70 |
+ // test macvlan private mode |
|
| 71 |
+ mode, err = setMacVlanMode(modePrivate) |
|
| 72 |
+ if err != nil {
|
|
| 73 |
+ t.Fatalf("error parsing %v vlan mode: %v", mode, err)
|
|
| 74 |
+ } |
|
| 75 |
+ if mode != netlink.MACVLAN_MODE_PRIVATE {
|
|
| 76 |
+ t.Fatalf("expected %d got %d", netlink.MACVLAN_MODE_PRIVATE, mode)
|
|
| 77 |
+ } |
|
| 78 |
+ // test macvlan vepa mode |
|
| 79 |
+ mode, err = setMacVlanMode(modeVepa) |
|
| 80 |
+ if err != nil {
|
|
| 81 |
+ t.Fatalf("error parsing %v vlan mode: %v", mode, err)
|
|
| 82 |
+ } |
|
| 83 |
+ if mode != netlink.MACVLAN_MODE_VEPA {
|
|
| 84 |
+ t.Fatalf("expected %d got %d", netlink.MACVLAN_MODE_VEPA, mode)
|
|
| 85 |
+ } |
|
| 86 |
+ // test invalid mode |
|
| 87 |
+ mode, err = setMacVlanMode("foo")
|
|
| 88 |
+ if err == nil {
|
|
| 89 |
+ t.Fatal("invalid macvlan mode should have returned an error")
|
|
| 90 |
+ } |
|
| 91 |
+ if mode != 0 {
|
|
| 92 |
+ t.Fatalf("expected 0 got %d", mode)
|
|
| 93 |
+ } |
|
| 94 |
+ // test null mode |
|
| 95 |
+ mode, err = setMacVlanMode("")
|
|
| 96 |
+ if err == nil {
|
|
| 97 |
+ t.Fatal("invalid macvlan mode should have returned an error")
|
|
| 98 |
+ } |
|
| 99 |
+ if mode != 0 {
|
|
| 100 |
+ t.Fatalf("expected 0 got %d", mode)
|
|
| 101 |
+ } |
|
| 102 |
+} |
| 0 | 103 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,108 @@ |
| 0 |
+package macvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/Sirupsen/logrus" |
|
| 6 |
+ "github.com/docker/libnetwork/osl" |
|
| 7 |
+ "github.com/docker/libnetwork/types" |
|
| 8 |
+) |
|
| 9 |
+ |
|
| 10 |
+func (d *driver) network(nid string) *network {
|
|
| 11 |
+ d.Lock() |
|
| 12 |
+ n, ok := d.networks[nid] |
|
| 13 |
+ d.Unlock() |
|
| 14 |
+ if !ok {
|
|
| 15 |
+ logrus.Errorf("network id %s not found", nid)
|
|
| 16 |
+ } |
|
| 17 |
+ return n |
|
| 18 |
+} |
|
| 19 |
+ |
|
| 20 |
+func (d *driver) addNetwork(n *network) {
|
|
| 21 |
+ d.Lock() |
|
| 22 |
+ d.networks[n.id] = n |
|
| 23 |
+ d.Unlock() |
|
| 24 |
+} |
|
| 25 |
+ |
|
| 26 |
+func (d *driver) deleteNetwork(nid string) {
|
|
| 27 |
+ d.Lock() |
|
| 28 |
+ delete(d.networks, nid) |
|
| 29 |
+ d.Unlock() |
|
| 30 |
+} |
|
| 31 |
+ |
|
| 32 |
+// getNetworks Safely returns a slice of existng networks |
|
| 33 |
+func (d *driver) getNetworks() []*network {
|
|
| 34 |
+ d.Lock() |
|
| 35 |
+ defer d.Unlock() |
|
| 36 |
+ |
|
| 37 |
+ ls := make([]*network, 0, len(d.networks)) |
|
| 38 |
+ for _, nw := range d.networks {
|
|
| 39 |
+ ls = append(ls, nw) |
|
| 40 |
+ } |
|
| 41 |
+ return ls |
|
| 42 |
+} |
|
| 43 |
+ |
|
| 44 |
+func (n *network) endpoint(eid string) *endpoint {
|
|
| 45 |
+ n.Lock() |
|
| 46 |
+ defer n.Unlock() |
|
| 47 |
+ |
|
| 48 |
+ return n.endpoints[eid] |
|
| 49 |
+} |
|
| 50 |
+ |
|
| 51 |
+func (n *network) addEndpoint(ep *endpoint) {
|
|
| 52 |
+ n.Lock() |
|
| 53 |
+ n.endpoints[ep.id] = ep |
|
| 54 |
+ n.Unlock() |
|
| 55 |
+} |
|
| 56 |
+ |
|
| 57 |
+func (n *network) deleteEndpoint(eid string) {
|
|
| 58 |
+ n.Lock() |
|
| 59 |
+ delete(n.endpoints, eid) |
|
| 60 |
+ n.Unlock() |
|
| 61 |
+} |
|
| 62 |
+ |
|
| 63 |
+func (n *network) getEndpoint(eid string) (*endpoint, error) {
|
|
| 64 |
+ n.Lock() |
|
| 65 |
+ defer n.Unlock() |
|
| 66 |
+ if eid == "" {
|
|
| 67 |
+ return nil, fmt.Errorf("endpoint id %s not found", eid)
|
|
| 68 |
+ } |
|
| 69 |
+ if ep, ok := n.endpoints[eid]; ok {
|
|
| 70 |
+ return ep, nil |
|
| 71 |
+ } |
|
| 72 |
+ return nil, nil |
|
| 73 |
+} |
|
| 74 |
+ |
|
| 75 |
+func validateID(nid, eid string) error {
|
|
| 76 |
+ if nid == "" {
|
|
| 77 |
+ return fmt.Errorf("invalid network id")
|
|
| 78 |
+ } |
|
| 79 |
+ if eid == "" {
|
|
| 80 |
+ return fmt.Errorf("invalid endpoint id")
|
|
| 81 |
+ } |
|
| 82 |
+ return nil |
|
| 83 |
+} |
|
| 84 |
+ |
|
| 85 |
+func (n *network) sandbox() osl.Sandbox {
|
|
| 86 |
+ n.Lock() |
|
| 87 |
+ defer n.Unlock() |
|
| 88 |
+ return n.sbox |
|
| 89 |
+} |
|
| 90 |
+ |
|
| 91 |
+func (n *network) setSandbox(sbox osl.Sandbox) {
|
|
| 92 |
+ n.Lock() |
|
| 93 |
+ n.sbox = sbox |
|
| 94 |
+ n.Unlock() |
|
| 95 |
+} |
|
| 96 |
+ |
|
| 97 |
+func (d *driver) getNetwork(id string) (*network, error) {
|
|
| 98 |
+ d.Lock() |
|
| 99 |
+ defer d.Unlock() |
|
| 100 |
+ if id == "" {
|
|
| 101 |
+ return nil, types.BadRequestErrorf("invalid network id: %s", id)
|
|
| 102 |
+ } |
|
| 103 |
+ if nw, ok := d.networks[id]; ok {
|
|
| 104 |
+ return nw, nil |
|
| 105 |
+ } |
|
| 106 |
+ return nil, types.NotFoundErrorf("network not found: %s", id)
|
|
| 107 |
+} |
| 0 | 108 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,213 @@ |
| 0 |
+package macvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "encoding/json" |
|
| 4 |
+ "fmt" |
|
| 5 |
+ |
|
| 6 |
+ "github.com/Sirupsen/logrus" |
|
| 7 |
+ "github.com/docker/libkv/store/boltdb" |
|
| 8 |
+ "github.com/docker/libnetwork/datastore" |
|
| 9 |
+ "github.com/docker/libnetwork/discoverapi" |
|
| 10 |
+ "github.com/docker/libnetwork/netlabel" |
|
| 11 |
+ "github.com/docker/libnetwork/types" |
|
| 12 |
+) |
|
| 13 |
+ |
|
| 14 |
+const macvlanPrefix = "macvlan" // prefix used for persistent driver storage |
|
| 15 |
+ |
|
| 16 |
+// networkConfiguration for this driver's network specific configuration |
|
| 17 |
+type configuration struct {
|
|
| 18 |
+ ID string |
|
| 19 |
+ Mtu int |
|
| 20 |
+ dbIndex uint64 |
|
| 21 |
+ dbExists bool |
|
| 22 |
+ Internal bool |
|
| 23 |
+ HostIface string |
|
| 24 |
+ MacvlanMode string |
|
| 25 |
+ CreatedSlaveLink bool |
|
| 26 |
+ Ipv4Subnets []*ipv4Subnet |
|
| 27 |
+ Ipv6Subnets []*ipv6Subnet |
|
| 28 |
+} |
|
| 29 |
+ |
|
| 30 |
+type ipv4Subnet struct {
|
|
| 31 |
+ SubnetIP string |
|
| 32 |
+ GwIP string |
|
| 33 |
+} |
|
| 34 |
+ |
|
| 35 |
+type ipv6Subnet struct {
|
|
| 36 |
+ SubnetIP string |
|
| 37 |
+ GwIP string |
|
| 38 |
+} |
|
| 39 |
+ |
|
| 40 |
+// initStore drivers are responsible for caching their own persistent state |
|
| 41 |
+func (d *driver) initStore(option map[string]interface{}) error {
|
|
| 42 |
+ if data, ok := option[netlabel.LocalKVClient]; ok {
|
|
| 43 |
+ var err error |
|
| 44 |
+ dsc, ok := data.(discoverapi.DatastoreConfigData) |
|
| 45 |
+ if !ok {
|
|
| 46 |
+ return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
|
|
| 47 |
+ } |
|
| 48 |
+ d.store, err = datastore.NewDataStoreFromConfig(dsc) |
|
| 49 |
+ if err != nil {
|
|
| 50 |
+ return types.InternalErrorf("macvlan driver failed to initialize data store: %v", err)
|
|
| 51 |
+ } |
|
| 52 |
+ |
|
| 53 |
+ return d.populateNetworks() |
|
| 54 |
+ } |
|
| 55 |
+ |
|
| 56 |
+ return nil |
|
| 57 |
+} |
|
| 58 |
+ |
|
| 59 |
+// populateNetworks is invoked at driver init to recreate persistently stored networks |
|
| 60 |
+func (d *driver) populateNetworks() error {
|
|
| 61 |
+ kvol, err := d.store.List(datastore.Key(macvlanPrefix), &configuration{})
|
|
| 62 |
+ if err != nil && err != datastore.ErrKeyNotFound && err != boltdb.ErrBoltBucketNotFound {
|
|
| 63 |
+ return fmt.Errorf("failed to get macvlan network configurations from store: %v", err)
|
|
| 64 |
+ } |
|
| 65 |
+ // If empty it simply means no macvlan networks have been created yet |
|
| 66 |
+ if err == datastore.ErrKeyNotFound {
|
|
| 67 |
+ return nil |
|
| 68 |
+ } |
|
| 69 |
+ for _, kvo := range kvol {
|
|
| 70 |
+ config := kvo.(*configuration) |
|
| 71 |
+ if err = d.createNetwork(config); err != nil {
|
|
| 72 |
+ logrus.Warnf("Could not create macvlan network for id %s from persistent state", config.ID)
|
|
| 73 |
+ } |
|
| 74 |
+ } |
|
| 75 |
+ |
|
| 76 |
+ return nil |
|
| 77 |
+} |
|
| 78 |
+ |
|
| 79 |
+// storeUpdate used to update persistent macvlan network records as they are created |
|
| 80 |
+func (d *driver) storeUpdate(kvObject datastore.KVObject) error {
|
|
| 81 |
+ if d.store == nil {
|
|
| 82 |
+ logrus.Warnf("macvlan store not initialized. kv object %s is not added to the store", datastore.Key(kvObject.Key()...))
|
|
| 83 |
+ return nil |
|
| 84 |
+ } |
|
| 85 |
+ if err := d.store.PutObjectAtomic(kvObject); err != nil {
|
|
| 86 |
+ return fmt.Errorf("failed to update macvlan store for object type %T: %v", kvObject, err)
|
|
| 87 |
+ } |
|
| 88 |
+ |
|
| 89 |
+ return nil |
|
| 90 |
+} |
|
| 91 |
+ |
|
| 92 |
+// storeDelete used to delete macvlan records from persistent cache as they are deleted |
|
| 93 |
+func (d *driver) storeDelete(kvObject datastore.KVObject) error {
|
|
| 94 |
+ if d.store == nil {
|
|
| 95 |
+ logrus.Debugf("macvlan store not initialized. kv object %s is not deleted from store", datastore.Key(kvObject.Key()...))
|
|
| 96 |
+ return nil |
|
| 97 |
+ } |
|
| 98 |
+retry: |
|
| 99 |
+ if err := d.store.DeleteObjectAtomic(kvObject); err != nil {
|
|
| 100 |
+ if err == datastore.ErrKeyModified {
|
|
| 101 |
+ if err := d.store.GetObject(datastore.Key(kvObject.Key()...), kvObject); err != nil {
|
|
| 102 |
+ return fmt.Errorf("could not update the kvobject to latest when trying to delete: %v", err)
|
|
| 103 |
+ } |
|
| 104 |
+ goto retry |
|
| 105 |
+ } |
|
| 106 |
+ return err |
|
| 107 |
+ } |
|
| 108 |
+ |
|
| 109 |
+ return nil |
|
| 110 |
+} |
|
| 111 |
+ |
|
| 112 |
+func (config *configuration) MarshalJSON() ([]byte, error) {
|
|
| 113 |
+ nMap := make(map[string]interface{})
|
|
| 114 |
+ nMap["ID"] = config.ID |
|
| 115 |
+ nMap["Mtu"] = config.Mtu |
|
| 116 |
+ nMap["HostIface"] = config.HostIface |
|
| 117 |
+ nMap["MacvlanMode"] = config.MacvlanMode |
|
| 118 |
+ nMap["CreatedSubIface"] = config.CreatedSlaveLink |
|
| 119 |
+ if len(config.Ipv4Subnets) > 0 {
|
|
| 120 |
+ iis, err := json.Marshal(config.Ipv4Subnets) |
|
| 121 |
+ if err != nil {
|
|
| 122 |
+ return nil, err |
|
| 123 |
+ } |
|
| 124 |
+ nMap["Ipv4Subnets"] = string(iis) |
|
| 125 |
+ } |
|
| 126 |
+ if len(config.Ipv6Subnets) > 0 {
|
|
| 127 |
+ iis, err := json.Marshal(config.Ipv6Subnets) |
|
| 128 |
+ if err != nil {
|
|
| 129 |
+ return nil, err |
|
| 130 |
+ } |
|
| 131 |
+ nMap["Ipv6Subnets"] = string(iis) |
|
| 132 |
+ } |
|
| 133 |
+ return json.Marshal(nMap) |
|
| 134 |
+} |
|
| 135 |
+ |
|
| 136 |
+func (config *configuration) UnmarshalJSON(b []byte) error {
|
|
| 137 |
+ var ( |
|
| 138 |
+ err error |
|
| 139 |
+ nMap map[string]interface{}
|
|
| 140 |
+ ) |
|
| 141 |
+ |
|
| 142 |
+ if err = json.Unmarshal(b, &nMap); err != nil {
|
|
| 143 |
+ return err |
|
| 144 |
+ } |
|
| 145 |
+ config.ID = nMap["ID"].(string) |
|
| 146 |
+ config.Mtu = int(nMap["Mtu"].(float64)) |
|
| 147 |
+ config.HostIface = nMap["HostIface"].(string) |
|
| 148 |
+ config.MacvlanMode = nMap["MacvlanMode"].(string) |
|
| 149 |
+ config.CreatedSlaveLink = nMap["CreatedSubIface"].(bool) |
|
| 150 |
+ if v, ok := nMap["Ipv4Subnets"]; ok {
|
|
| 151 |
+ if err := json.Unmarshal([]byte(v.(string)), &config.Ipv4Subnets); err != nil {
|
|
| 152 |
+ return err |
|
| 153 |
+ } |
|
| 154 |
+ } |
|
| 155 |
+ if v, ok := nMap["Ipv6Subnets"]; ok {
|
|
| 156 |
+ if err := json.Unmarshal([]byte(v.(string)), &config.Ipv6Subnets); err != nil {
|
|
| 157 |
+ return err |
|
| 158 |
+ } |
|
| 159 |
+ } |
|
| 160 |
+ return nil |
|
| 161 |
+} |
|
| 162 |
+ |
|
| 163 |
+func (config *configuration) Key() []string {
|
|
| 164 |
+ return []string{macvlanPrefix, config.ID}
|
|
| 165 |
+} |
|
| 166 |
+ |
|
| 167 |
+func (config *configuration) KeyPrefix() []string {
|
|
| 168 |
+ return []string{macvlanPrefix}
|
|
| 169 |
+} |
|
| 170 |
+ |
|
| 171 |
+func (config *configuration) Value() []byte {
|
|
| 172 |
+ b, err := json.Marshal(config) |
|
| 173 |
+ if err != nil {
|
|
| 174 |
+ return nil |
|
| 175 |
+ } |
|
| 176 |
+ return b |
|
| 177 |
+} |
|
| 178 |
+ |
|
| 179 |
+func (config *configuration) SetValue(value []byte) error {
|
|
| 180 |
+ return json.Unmarshal(value, config) |
|
| 181 |
+} |
|
| 182 |
+ |
|
| 183 |
+func (config *configuration) Index() uint64 {
|
|
| 184 |
+ return config.dbIndex |
|
| 185 |
+} |
|
| 186 |
+ |
|
| 187 |
+func (config *configuration) SetIndex(index uint64) {
|
|
| 188 |
+ config.dbIndex = index |
|
| 189 |
+ config.dbExists = true |
|
| 190 |
+} |
|
| 191 |
+ |
|
| 192 |
+func (config *configuration) Exists() bool {
|
|
| 193 |
+ return config.dbExists |
|
| 194 |
+} |
|
| 195 |
+ |
|
| 196 |
+func (config *configuration) Skip() bool {
|
|
| 197 |
+ return false |
|
| 198 |
+} |
|
| 199 |
+ |
|
| 200 |
+func (config *configuration) New() datastore.KVObject {
|
|
| 201 |
+ return &configuration{}
|
|
| 202 |
+} |
|
| 203 |
+ |
|
| 204 |
+func (config *configuration) CopyTo(o datastore.KVObject) error {
|
|
| 205 |
+ dstNcfg := o.(*configuration) |
|
| 206 |
+ *dstNcfg = *config |
|
| 207 |
+ return nil |
|
| 208 |
+} |
|
| 209 |
+ |
|
| 210 |
+func (config *configuration) DataScope() string {
|
|
| 211 |
+ return datastore.LocalScope |
|
| 212 |
+} |
| 0 | 213 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,60 @@ |
| 0 |
+package macvlan |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "testing" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/docker/libnetwork/driverapi" |
|
| 6 |
+ _ "github.com/docker/libnetwork/testutils" |
|
| 7 |
+) |
|
| 8 |
+ |
|
| 9 |
+const testNetworkType = "macvlan" |
|
| 10 |
+ |
|
| 11 |
+type driverTester struct {
|
|
| 12 |
+ t *testing.T |
|
| 13 |
+ d *driver |
|
| 14 |
+} |
|
| 15 |
+ |
|
| 16 |
+func (dt *driverTester) RegisterDriver(name string, drv driverapi.Driver, |
|
| 17 |
+ cap driverapi.Capability) error {
|
|
| 18 |
+ if name != testNetworkType {
|
|
| 19 |
+ dt.t.Fatalf("Expected driver register name to be %q. Instead got %q",
|
|
| 20 |
+ testNetworkType, name) |
|
| 21 |
+ } |
|
| 22 |
+ |
|
| 23 |
+ if _, ok := drv.(*driver); !ok {
|
|
| 24 |
+ dt.t.Fatalf("Expected driver type to be %T. Instead got %T",
|
|
| 25 |
+ &driver{}, drv)
|
|
| 26 |
+ } |
|
| 27 |
+ |
|
| 28 |
+ dt.d = drv.(*driver) |
|
| 29 |
+ return nil |
|
| 30 |
+} |
|
| 31 |
+ |
|
| 32 |
+func TestMacvlanInit(t *testing.T) {
|
|
| 33 |
+ if err := Init(&driverTester{t: t}, nil); err != nil {
|
|
| 34 |
+ t.Fatal(err) |
|
| 35 |
+ } |
|
| 36 |
+} |
|
| 37 |
+ |
|
| 38 |
+func TestMacvlanNilConfig(t *testing.T) {
|
|
| 39 |
+ dt := &driverTester{t: t}
|
|
| 40 |
+ if err := Init(dt, nil); err != nil {
|
|
| 41 |
+ t.Fatal(err) |
|
| 42 |
+ } |
|
| 43 |
+ |
|
| 44 |
+ if err := dt.d.initStore(nil); err != nil {
|
|
| 45 |
+ t.Fatal(err) |
|
| 46 |
+ } |
|
| 47 |
+} |
|
| 48 |
+ |
|
| 49 |
+func TestMacvlanType(t *testing.T) {
|
|
| 50 |
+ dt := &driverTester{t: t}
|
|
| 51 |
+ if err := Init(dt, nil); err != nil {
|
|
| 52 |
+ t.Fatal(err) |
|
| 53 |
+ } |
|
| 54 |
+ |
|
| 55 |
+ if dt.d.Type() != testNetworkType {
|
|
| 56 |
+ t.Fatalf("Expected Type() to return %q. Instead got %q", testNetworkType,
|
|
| 57 |
+ dt.d.Type()) |
|
| 58 |
+ } |
|
| 59 |
+} |
| ... | ... |
@@ -3,6 +3,8 @@ package libnetwork |
| 3 | 3 |
import ( |
| 4 | 4 |
"github.com/docker/libnetwork/drivers/bridge" |
| 5 | 5 |
"github.com/docker/libnetwork/drivers/host" |
| 6 |
+ "github.com/docker/libnetwork/drivers/ipvlan" |
|
| 7 |
+ "github.com/docker/libnetwork/drivers/macvlan" |
|
| 6 | 8 |
"github.com/docker/libnetwork/drivers/null" |
| 7 | 9 |
"github.com/docker/libnetwork/drivers/overlay" |
| 8 | 10 |
"github.com/docker/libnetwork/drivers/remote" |
| ... | ... |
@@ -15,5 +17,7 @@ func getInitializers() []initializer {
|
| 15 | 15 |
{null.Init, "null"},
|
| 16 | 16 |
{remote.Init, "remote"},
|
| 17 | 17 |
{overlay.Init, "overlay"},
|
| 18 |
+ {macvlan.Init, "macvlan"},
|
|
| 19 |
+ {ipvlan.Init, "ipvlan"},
|
|
| 18 | 20 |
} |
| 19 | 21 |
} |