2016-02-16 22:15:18 -05:00
|
|
|
package macvlan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/netutils"
|
2016-05-16 14:51:40 -04:00
|
|
|
"github.com/docker/libnetwork/ns"
|
2016-03-10 16:23:04 -05:00
|
|
|
"github.com/docker/libnetwork/osl"
|
2016-02-16 22:15:18 -05:00
|
|
|
"github.com/docker/libnetwork/types"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-02-16 22:15:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateEndpoint assigns the mac, ip and endpoint id for the new container
|
|
|
|
func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
|
|
|
|
epOptions map[string]interface{}) error {
|
2016-03-10 16:23:04 -05:00
|
|
|
defer osl.InitOSContext()()
|
2016-02-16 22:15:18 -05:00
|
|
|
|
|
|
|
if err := validateID(nid, eid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n, err := d.getNetwork(nid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("network id %q not found", nid)
|
|
|
|
}
|
|
|
|
ep := &endpoint{
|
|
|
|
id: eid,
|
2016-06-10 22:54:00 -04:00
|
|
|
nid: nid,
|
2016-02-16 22:15:18 -05:00
|
|
|
addr: ifInfo.Address(),
|
|
|
|
addrv6: ifInfo.AddressIPv6(),
|
|
|
|
mac: ifInfo.MacAddress(),
|
|
|
|
}
|
|
|
|
if ep.addr == nil {
|
|
|
|
return fmt.Errorf("create endpoint was not passed an IP address")
|
|
|
|
}
|
|
|
|
if ep.mac == nil {
|
|
|
|
ep.mac = netutils.GenerateMACFromIP(ep.addr.IP)
|
|
|
|
if err := ifInfo.SetMacAddress(ep.mac); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// disallow portmapping -p
|
|
|
|
if opt, ok := epOptions[netlabel.PortMap]; ok {
|
|
|
|
if _, ok := opt.([]types.PortBinding); ok {
|
|
|
|
if len(opt.([]types.PortBinding)) > 0 {
|
2016-03-02 20:13:15 -05:00
|
|
|
logrus.Warnf("%s driver does not support port mappings", macvlanType)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// disallow port exposure --expose
|
|
|
|
if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
|
|
|
|
if _, ok := opt.([]types.TransportPort); ok {
|
|
|
|
if len(opt.([]types.TransportPort)) > 0 {
|
2016-03-02 20:13:15 -05:00
|
|
|
logrus.Warnf("%s driver does not support port exposures", macvlanType)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-10 22:54:00 -04:00
|
|
|
|
|
|
|
if err := d.storeUpdate(ep); err != nil {
|
2018-07-05 16:33:01 -04:00
|
|
|
return fmt.Errorf("failed to save macvlan endpoint %.7s to store: %v", ep.id, err)
|
2016-06-10 22:54:00 -04:00
|
|
|
}
|
|
|
|
|
2016-02-16 22:15:18 -05:00
|
|
|
n.addEndpoint(ep)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-18 05:57:49 -04:00
|
|
|
// DeleteEndpoint removes the endpoint and associated netlink interface
|
2016-02-16 22:15:18 -05:00
|
|
|
func (d *driver) DeleteEndpoint(nid, eid string) error {
|
2016-03-10 16:23:04 -05:00
|
|
|
defer osl.InitOSContext()()
|
2016-02-16 22:15:18 -05:00
|
|
|
if err := validateID(nid, eid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n := d.network(nid)
|
|
|
|
if n == nil {
|
|
|
|
return fmt.Errorf("network id %q not found", nid)
|
|
|
|
}
|
|
|
|
ep := n.endpoint(eid)
|
|
|
|
if ep == nil {
|
|
|
|
return fmt.Errorf("endpoint id %q not found", eid)
|
|
|
|
}
|
2016-05-16 14:51:40 -04:00
|
|
|
if link, err := ns.NlHandle().LinkByName(ep.srcName); err == nil {
|
2018-02-28 12:38:35 -05:00
|
|
|
if err := ns.NlHandle().LinkDel(link); err != nil {
|
|
|
|
logrus.WithError(err).Warnf("Failed to delete interface (%s)'s link on endpoint (%s) delete", ep.srcName, ep.id)
|
|
|
|
}
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
2016-06-14 15:57:51 -04:00
|
|
|
|
2016-06-10 22:54:00 -04:00
|
|
|
if err := d.storeDelete(ep); err != nil {
|
2018-07-05 16:33:01 -04:00
|
|
|
logrus.Warnf("Failed to remove macvlan endpoint %.7s from store: %v", ep.id, err)
|
2016-06-10 22:54:00 -04:00
|
|
|
}
|
2016-06-14 15:57:51 -04:00
|
|
|
|
|
|
|
n.deleteEndpoint(ep.id)
|
|
|
|
|
2016-02-16 22:15:18 -05:00
|
|
|
return nil
|
|
|
|
}
|