2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2021-05-28 14:53:49 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-06-15 14:41:04 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/drivers/overlay"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2021-05-27 20:15:56 -04:00
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-06-15 14:41:04 -04:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
type router struct {
|
|
|
|
d driverapi.Driver
|
|
|
|
}
|
|
|
|
|
|
|
|
type endpoint struct {
|
2015-10-03 19:11:50 -04:00
|
|
|
addr *net.IPNet
|
2015-06-15 14:41:04 -04:00
|
|
|
mac net.HardwareAddr
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2016-10-07 14:58:10 -04:00
|
|
|
func (r *router) GetPluginGetter() plugingetter.PluginGetter {
|
2016-09-27 16:54:25 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:41:04 -04:00
|
|
|
func (r *router) RegisterDriver(name string, driver driverapi.Driver, c driverapi.Capability) error {
|
|
|
|
r.d = driver
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
func (ep *endpoint) Interface() driverapi.InterfaceInfo {
|
2015-06-15 14:41:04 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func (ep *endpoint) SetMacAddress(mac net.HardwareAddr) error {
|
|
|
|
if ep.mac != nil {
|
|
|
|
return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", ep.mac, mac)
|
|
|
|
}
|
|
|
|
if mac == nil {
|
|
|
|
return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface")
|
|
|
|
}
|
|
|
|
ep.mac = types.GetMacCopy(mac)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) SetIPAddress(address *net.IPNet) error {
|
|
|
|
if address.IP == nil {
|
|
|
|
return types.BadRequestErrorf("tried to set nil IP address to endpoint interface")
|
|
|
|
}
|
|
|
|
if address.IP.To4() == nil {
|
|
|
|
return types.NotImplementedErrorf("do not support ipv6 yet")
|
|
|
|
}
|
|
|
|
if ep.addr != nil {
|
|
|
|
return types.ForbiddenErrorf("endpoint interface IP present (%s). Cannot be modified with %s.", ep.addr, address)
|
|
|
|
}
|
|
|
|
ep.addr = types.GetIPNetCopy(address)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) MacAddress() net.HardwareAddr {
|
|
|
|
return types.GetMacCopy(ep.mac)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Address() *net.IPNet {
|
|
|
|
return types.GetIPNetCopy(ep.addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) AddressIPv6() *net.IPNet {
|
2015-06-15 14:41:04 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
func (ep *endpoint) InterfaceName() driverapi.InterfaceNameInfo {
|
|
|
|
return ep
|
2015-06-15 14:41:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) SetNames(srcName, dstPrefix string) error {
|
|
|
|
ep.name = srcName
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) SetGateway(net.IP) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) SetGatewayIPv6(net.IP) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) AddStaticRoute(destination *net.IPNet, routeType int,
|
2015-09-09 19:06:35 -04:00
|
|
|
nextHop net.IP) error {
|
2015-06-15 14:41:04 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-18 22:55:39 -04:00
|
|
|
func (ep *endpoint) AddTableEntry(tableName string, key string, value []byte) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-02 21:07:44 -05:00
|
|
|
func (ep *endpoint) DisableGatewayService() {}
|
|
|
|
|
2015-06-15 14:41:04 -04:00
|
|
|
func main() {
|
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := make(map[string]interface{})
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
opt[netlabel.OverlayBindInterface] = os.Args[1]
|
|
|
|
}
|
|
|
|
if len(os.Args) > 2 {
|
|
|
|
opt[netlabel.OverlayNeighborIP] = os.Args[2]
|
|
|
|
}
|
|
|
|
if len(os.Args) > 3 {
|
2015-10-06 20:28:47 -04:00
|
|
|
opt[netlabel.GlobalKVProvider] = os.Args[3]
|
2015-06-15 14:41:04 -04:00
|
|
|
}
|
|
|
|
if len(os.Args) > 4 {
|
2015-10-06 20:28:47 -04:00
|
|
|
opt[netlabel.GlobalKVProviderURL] = os.Args[4]
|
2015-06-15 14:41:04 -04:00
|
|
|
}
|
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
r := &router{}
|
|
|
|
if err := overlay.Init(r, opt); err != nil {
|
|
|
|
fmt.Printf("Failed to initialize overlay driver: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-06-15 14:41:04 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := r.d.CreateNetwork("testnetwork",
|
2016-04-18 22:55:39 -04:00
|
|
|
map[string]interface{}{}, nil, nil, nil); err != nil {
|
2015-06-15 14:41:04 -04:00
|
|
|
fmt.Printf("Failed to create network in the driver: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep := &endpoint{}
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := r.d.CreateEndpoint("testnetwork", "testep",
|
2015-06-15 14:41:04 -04:00
|
|
|
ep, map[string]interface{}{}); err != nil {
|
|
|
|
fmt.Printf("Failed to create endpoint in the driver: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := r.d.Join("testnetwork", "testep",
|
2015-06-15 14:41:04 -04:00
|
|
|
"", ep, map[string]interface{}{}); err != nil {
|
|
|
|
fmt.Printf("Failed to join an endpoint in the driver: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
link, err := netlink.LinkByName(ep.name)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to find the container interface with name %s: %v\n",
|
|
|
|
ep.name, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
ipAddr := &netlink.Addr{IPNet: ep.addr, Label: ""}
|
2015-06-15 14:41:04 -04:00
|
|
|
if err := netlink.AddrAdd(link, ipAddr); err != nil {
|
|
|
|
fmt.Printf("Failed to add address to the interface: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
sigCh := make(chan os.Signal, 1)
|
2021-05-27 20:15:56 -04:00
|
|
|
signal.Notify(sigCh, os.Interrupt)
|
|
|
|
|
|
|
|
for range sigCh {
|
|
|
|
if err := r.d.Leave("testnetwork", "testep"); err != nil {
|
|
|
|
fmt.Printf("Error leaving: %v", err)
|
2015-06-15 14:41:04 -04:00
|
|
|
}
|
2021-05-27 20:15:56 -04:00
|
|
|
overlay.Fini(r.d)
|
|
|
|
os.Exit(0)
|
2015-06-15 14:41:04 -04:00
|
|
|
}
|
|
|
|
}
|