2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2021-05-25 19:48:54 -04:00
|
|
|
// +build linux
|
|
|
|
|
2016-02-16 22:15:18 -05:00
|
|
|
package ipvlan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/ns"
|
|
|
|
"github.com/docker/docker/libnetwork/osl"
|
|
|
|
"github.com/docker/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)
|
|
|
|
}
|
|
|
|
if ifInfo.MacAddress() != nil {
|
2018-09-12 21:59:39 -04:00
|
|
|
return fmt.Errorf("%s interfaces do not support custom mac address assignment", ipvlanType)
|
2016-02-16 22:15:18 -05:00
|
|
|
}
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
if ep.addr == nil {
|
|
|
|
return fmt.Errorf("create endpoint was not passed an IP address")
|
|
|
|
}
|
|
|
|
// disallow port mapping -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", ipvlanType)
|
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", ipvlanType)
|
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 ipvlan 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteEndpoint remove the endpoint and associated netlink interface
|
|
|
|
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-03-02 20:13:15 -05: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 ipvlan endpoint %.7s from store: %v", ep.id, err)
|
2016-06-10 22:54:00 -04:00
|
|
|
}
|
2016-07-19 00:06:46 -04:00
|
|
|
n.deleteEndpoint(ep.id)
|
2016-02-16 22:15:18 -05:00
|
|
|
return nil
|
|
|
|
}
|