github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/libnetwork/cmd/ovrouter/ovrouter.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"net"
     9  	"os"
    10  	"os/signal"
    11  
    12  	"github.com/docker/docker/libnetwork/driverapi"
    13  	"github.com/docker/docker/libnetwork/drivers/overlay"
    14  	"github.com/docker/docker/libnetwork/netlabel"
    15  	"github.com/docker/docker/libnetwork/types"
    16  	"github.com/docker/docker/pkg/plugingetter"
    17  	"github.com/docker/docker/pkg/reexec"
    18  	"github.com/vishvananda/netlink"
    19  )
    20  
    21  type router struct {
    22  	d driverapi.Driver
    23  }
    24  
    25  type endpoint struct {
    26  	addr *net.IPNet
    27  	mac  net.HardwareAddr
    28  	name string
    29  }
    30  
    31  func (r *router) GetPluginGetter() plugingetter.PluginGetter {
    32  	return nil
    33  }
    34  
    35  func (r *router) RegisterDriver(name string, driver driverapi.Driver, c driverapi.Capability) error {
    36  	r.d = driver
    37  	return nil
    38  }
    39  
    40  func (ep *endpoint) Interface() driverapi.InterfaceInfo {
    41  	return nil
    42  }
    43  
    44  func (ep *endpoint) SetMacAddress(mac net.HardwareAddr) error {
    45  	if ep.mac != nil {
    46  		return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", ep.mac, mac)
    47  	}
    48  	if mac == nil {
    49  		return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface")
    50  	}
    51  	ep.mac = types.GetMacCopy(mac)
    52  	return nil
    53  }
    54  
    55  func (ep *endpoint) SetIPAddress(address *net.IPNet) error {
    56  	if address.IP == nil {
    57  		return types.BadRequestErrorf("tried to set nil IP address to endpoint interface")
    58  	}
    59  	if address.IP.To4() == nil {
    60  		return types.NotImplementedErrorf("do not support ipv6 yet")
    61  	}
    62  	if ep.addr != nil {
    63  		return types.ForbiddenErrorf("endpoint interface IP present (%s). Cannot be modified with %s.", ep.addr, address)
    64  	}
    65  	ep.addr = types.GetIPNetCopy(address)
    66  	return nil
    67  }
    68  
    69  func (ep *endpoint) MacAddress() net.HardwareAddr {
    70  	return types.GetMacCopy(ep.mac)
    71  }
    72  
    73  func (ep *endpoint) Address() *net.IPNet {
    74  	return types.GetIPNetCopy(ep.addr)
    75  }
    76  
    77  func (ep *endpoint) AddressIPv6() *net.IPNet {
    78  	return nil
    79  }
    80  
    81  func (ep *endpoint) InterfaceName() driverapi.InterfaceNameInfo {
    82  	return ep
    83  }
    84  
    85  func (ep *endpoint) SetNames(srcName, dstPrefix string) error {
    86  	ep.name = srcName
    87  	return nil
    88  }
    89  
    90  func (ep *endpoint) SetGateway(net.IP) error {
    91  	return nil
    92  }
    93  
    94  func (ep *endpoint) SetGatewayIPv6(net.IP) error {
    95  	return nil
    96  }
    97  
    98  func (ep *endpoint) AddStaticRoute(destination *net.IPNet, routeType int,
    99  	nextHop net.IP) error {
   100  	return nil
   101  }
   102  
   103  func (ep *endpoint) AddTableEntry(tableName string, key string, value []byte) error {
   104  	return nil
   105  }
   106  
   107  func (ep *endpoint) DisableGatewayService() {}
   108  
   109  func main() {
   110  	if reexec.Init() {
   111  		return
   112  	}
   113  
   114  	opt := make(map[string]interface{})
   115  	if len(os.Args) > 1 {
   116  		opt[netlabel.OverlayBindInterface] = os.Args[1]
   117  	}
   118  	if len(os.Args) > 2 {
   119  		opt[netlabel.OverlayNeighborIP] = os.Args[2]
   120  	}
   121  	if len(os.Args) > 3 {
   122  		opt[netlabel.GlobalKVProvider] = os.Args[3]
   123  	}
   124  	if len(os.Args) > 4 {
   125  		opt[netlabel.GlobalKVProviderURL] = os.Args[4]
   126  	}
   127  
   128  	r := &router{}
   129  	if err := overlay.Init(r, opt); err != nil {
   130  		fmt.Printf("Failed to initialize overlay driver: %v\n", err)
   131  		os.Exit(1)
   132  	}
   133  
   134  	if err := r.d.CreateNetwork("testnetwork",
   135  		map[string]interface{}{}, nil, nil, nil); err != nil {
   136  		fmt.Printf("Failed to create network in the driver: %v\n", err)
   137  		os.Exit(1)
   138  	}
   139  
   140  	ep := &endpoint{}
   141  	if err := r.d.CreateEndpoint("testnetwork", "testep",
   142  		ep, map[string]interface{}{}); err != nil {
   143  		fmt.Printf("Failed to create endpoint in the driver: %v\n", err)
   144  		os.Exit(1)
   145  	}
   146  
   147  	if err := r.d.Join("testnetwork", "testep",
   148  		"", ep, map[string]interface{}{}); err != nil {
   149  		fmt.Printf("Failed to join an endpoint in the driver: %v\n", err)
   150  		os.Exit(1)
   151  	}
   152  
   153  	link, err := netlink.LinkByName(ep.name)
   154  	if err != nil {
   155  		fmt.Printf("Failed to find the container interface with name %s: %v\n",
   156  			ep.name, err)
   157  		os.Exit(1)
   158  	}
   159  
   160  	ipAddr := &netlink.Addr{IPNet: ep.addr, Label: ""}
   161  	if err := netlink.AddrAdd(link, ipAddr); err != nil {
   162  		fmt.Printf("Failed to add address to the interface: %v\n", err)
   163  		os.Exit(1)
   164  	}
   165  
   166  	sigCh := make(chan os.Signal, 1)
   167  	signal.Notify(sigCh, os.Interrupt)
   168  
   169  	for range sigCh {
   170  		if err := r.d.Leave("testnetwork", "testep"); err != nil {
   171  			fmt.Printf("Error leaving: %v", err)
   172  		}
   173  		overlay.Fini(r.d)
   174  		os.Exit(0)
   175  	}
   176  }