2021-08-23 15:14:53 +02:00
|
|
|
//go:build linux
|
2021-05-25 23:48:54 +00:00
|
|
|
// +build linux
|
|
|
|
|
2016-02-16 22:15:18 -05:00
|
|
|
package macvlan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/netutils"
|
|
|
|
"github.com/docker/docker/libnetwork/ns"
|
|
|
|
"github.com/docker/docker/libnetwork/osl"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2017-07-26 14:18:31 -07: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 13:23:04 -08: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 19:54:00 -07: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 {
|
2022-07-01 11:32:46 +02:00
|
|
|
logrus.Warnf("macvlan driver does not support port mappings")
|
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 {
|
2022-07-01 11:32:46 +02:00
|
|
|
logrus.Warnf("macvlan driver does not support port exposures")
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-10 19:54:00 -07: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 19:54:00 -07:00
|
|
|
}
|
|
|
|
|
2016-02-16 22:15:18 -05:00
|
|
|
n.addEndpoint(ep)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-18 17:57:49 +08: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 13:23:04 -08: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 11:51:40 -07:00
|
|
|
if link, err := ns.NlHandle().LinkByName(ep.srcName); err == nil {
|
2018-03-01 01:38:35 +08: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 12:57:51 -07:00
|
|
|
|
2016-06-10 19:54:00 -07: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 19:54:00 -07:00
|
|
|
}
|
2016-06-14 12:57:51 -07:00
|
|
|
|
|
|
|
n.deleteEndpoint(ep.id)
|
|
|
|
|
2016-02-16 22:15:18 -05:00
|
|
|
return nil
|
|
|
|
}
|