Commit graph

85 commits

Author SHA1 Message Date
Rob Murray
0f9f9a132e Move 'netip' utils from 'ipam' to 'internal'.
Signed-off-by: Rob Murray <rob.murray@docker.com>
2023-12-06 17:13:40 +00:00
Cory Snider
7257c77e19 libnetwork/ipam: refactor prefix-overlap checks
I am finally convinced that, given two netip.Prefix values a and b, the
expression

    a.Contains(b.Addr()) || b.Contains(a.Addr())

is functionally equivalent to

    a.Overlaps(b)

The (netip.Prefix).Contains method works by masking the address with the
prefix's mask and testing whether the remaining most-significant bits
are equal to the same bits in the prefix. The (netip.Prefix).Overlaps
method works by masking the longer prefix to the length of the shorter
prefix and testing whether the remaining most-significant bits are
equal. This is equivalent to
shorterPrefix.Contains(longerPrefix.Addr()), therefore applying Contains
symmetrically to two prefixes will always yield the same result as
applying Overlaps to the two prefixes in either order.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-11-01 11:44:24 -04:00
Sebastiaan van Stijn
cff4f20c44
migrate to github.com/containerd/log v0.1.0
The github.com/containerd/containerd/log package was moved to a separate
module, which will also be used by upcoming (patch) releases of containerd.

This patch moves our own uses of the package to use the new module.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-10-11 17:52:23 +02:00
Bjorn Neergaard
8383430946
Merge pull request #45759 from akerouanton/validate-ipam-config
Validate IPAM config before handing it over to libnetwork
2023-08-22 13:58:28 -06:00
Albin Kerouanton
3e8af0817a
ipam: Replace ChildSubnet with parent Subnet when its mask is bigger
Prior to moby/moby#44968, libnetwork would happily accept a ChildSubnet
with a bigger mask than its parent subnet. In such case, it was
producing IP addresses based on the parent subnet, and the child subnet
was not allocated from the address pool.

This commit automatically fixes invalid ChildSubnet for networks stored
in libnetwork's datastore.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2023-08-22 17:12:06 +02:00
Albin Kerouanton
42d34e40f9
libnet: Replace BadRequest with InvalidParameter
InvalidParameter is now compatible with errdefs.InvalidParameter. Thus,
these errors will now return a 400 status code instead of a 500.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2023-08-17 16:45:04 +02:00
Sebastiaan van Stijn
df03357d19
libnetwork/ipam: move PoolID.FromString() to a PoolIDFromString() func
This makes it easier to consume, without first having to create an empty
PoolID.

Performance is the same:

    BenchmarkPoolIDFromString-10   6100345   196.5 ns/op  112 B/op   3 allocs/op
    BenchmarkPoolIDFromString-10   6252750   192.0 ns/op  112 B/op   3 allocs/op

Note that I opted not to change the return-type to a pointer, as that seems
to perform less;

    BenchmarkPoolIDFromString-10   6252750   192.0 ns/op  112 B/op   3 allocs/op
    BenchmarkPoolIDFromString-10   5288682   226.6 ns/op  192 B/op   4 allocs/op

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-08-08 15:57:24 +02:00
Sebastiaan van Stijn
87fc8c772b
libnetwork/ipam: Allocator.RequestPool: name args, output vars
network.requestPoolHelper and Allocator.RequestPool have many args and
output vars with generic types. Add names for them to make it easier to
grasp what's what.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-08-08 15:57:20 +02:00
Sebastiaan van Stijn
6dbc9c1c53
libnetwork/ipam: Allocator.RequestPool: mark options arg as unused
The options are unused, other than for debug-logging, which made it look
as if they were actually consumed anywhere, but they aren't.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-08-08 15:56:24 +02:00
Sebastiaan van Stijn
7047964bd6
libnetwork/ipam: Allocator.RequestPool: make parseErr only handle errors
This makes it slightly more readable to see what's returned in each of
the code-paths. Also move validation of pool/subpool earlier in the
function.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-08-08 15:56:24 +02:00
Brian Goff
74da6a6363 Switch all logging to use containerd log pkg
This unifies our logging and allows us to propagate logging and trace
contexts together.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2023-06-24 00:23:44 +00:00
Cory Snider
3c59ef247f libnet/ipam: use netip types internally
The netip types can be used as map keys, unlike net.IP and friends,
which is a very useful property to have for this application.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-23 18:10:01 -05:00
Cory Snider
01dbe23b6f libnet/ipam: simplify the data model
The address spaces are orthogonal. There is no shared state between them
logically so there is no reason for them to share any in-memory data
structures. addrSpace is responsible for allocating subnets and
addresses, while Allocator is responsible for implementing the IPAM API.
Lower all the implementation details of allocation into addrSpace.

There is no longer a need to include the name of the address space in
the map keys for subnets now that each addrSpace holds its own state
independently from other addrSpaces. Remove the AddressSpace field from
the struct used for map keys within an addrSpace so that an addrSpace
does not need to know its own name.

Pool allocations were encoded in a tree structure, using parent
references and reference counters. This structure affords for pools
subdivided an arbitrary number of times to be modeled, in theory. In
practice, the max depth is only two: master pools and sub-pools. The
allocator data model has also been heavily influenced by the
requirements and limitations of Datastore persistence, which are no
longer applicable.

Address allocations are always associated with a master pool. Sub-pools
only serve to restrict the range of addresses within the master pool
which could be allocated from. Model pool allocations within an address
space as a two-level hierarchy of maps.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-23 18:09:22 -05:00
Cory Snider
8273db28f3 libnet/ipam: inline parsePoolRequest function
There is only one call site, in (*Allocator).RequestPool. The logic is
tightly coupled between the caller and callee, and having them separate
makes it harder to reason about.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-23 18:09:22 -05:00
Cory Snider
9a8b45c133 libnet/ipam: drop vestiges of custom addrSpaces
Only two address spaces are supported: LocalDefault and GlobalDefault.
Support for non-default address spaces in the IPAM Allocator is
vestigial, from a time when IPAM state was stored in a persistent shared
datastore. There is no way to create non-default address spaces through
the IPAM API so there is no need to retain code to support the use of
such address spaces. Drop all pretense that more address spaces can
exist, to the extent that the IPAM API allows.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-23 18:09:22 -05:00
Cory Snider
18ac200efe libnet/ipam: get rid of superfluous closure
The two-phase commit dance serves no purpose with the current IPAM
allocator implementation. There are no fallible operations between the
call to aSpace.updatePoolDBOnAdd() and invoking the returned closure.
Allocate the subnet in the address space immediately when called and get
rid of the closure return.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-23 18:09:22 -05:00
Cory Snider
5287b2ddbf libnet/ipam: stop eagerly stringifying debug logs
The (*bitmap.Handle).String() method can be rather expensive to call. It
is all the more tragic when the expensively-constructed string is
immediately discarded because the log level is not high enough. Let
logrus stringify the arguments to debug logs so they are only
stringified when the log level is high enough.

    # Before
    ok  	github.com/docker/docker/libnetwork/ipam	10.159s
    # After
    ok  	github.com/docker/docker/libnetwork/ipam	2.484s

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-08 16:54:27 -05:00
Cory Snider
a264f2dc55 libnetwork/ipam: skip Destroy()ing bitseq.Handle values
The (*bitseq.Handle).Destroy() method deletes the persisted KVObject
from the datastore. This is a no-op on all the bitseq handles in package
ipam as they are not persisted in any datastore.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-27 11:47:43 -05:00
Cory Snider
a08a254df3 libnetwork: drop DatastoreConfig discovery type
The DatastoreConfig discovery type is unused. Remove the constant and
any resulting dead code. Today's biggest loser is the IPAM Allocator:
DatastoreConfig was the only type of discovery event it was listening
for, and there was no other place where a non-nil datastore could be
passed into the allocator. Strip out all the dead persistence code from
Allocator, leaving it as purely an in-memory implementation.

There is no more need to check the consistency of the allocator's
bit-sequences as there is no persistent storage for inconsistent bit
sequences to be loaded from.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-27 11:47:43 -05:00
Cory Snider
540d1e0561 libnw: untangle IPAM allocator from global state
ipam.Allocator is not a singleton, but it references mutable singleton
state. Address that deficiency by refactoring it to instead take the
predefined address spaces as constructor arguments. Unfortunately some
work is needed on the Swarmkit side before the mutable singleton state
can be completely eliminated from the IPAMs.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-26 14:56:12 -05:00
Mark Feit
3a938df4b5
Make the network allocator handle IPv4 blocks too small for network/broadcast addresses.
This was originally in docker/libnetwork#2624, which has been closed since the
code was moved here.

When creating a new network, IPAM's address allocator attempts to reserve the
network and broadcast addresses on IPv4 networks of all sizes. For RFC 3021
point-to-point networks (IPv4 /31s), this consumes both available addresses and
renders any attempt to allocate an address from the block unsuccessful.

This change prevents those reservations from taking place on IPv4 networks having
two or fewer addresses (i.e., /31s and /32s) while retaining the existing behavior
for larger IPv4 blocks and all IPv6 blocks.

In case you're wondering why anyone would allocate /31s:  I work for a network
service provider.  We use a lot of point-to-point networks.  This cuts our
address space utilization for those by 50%, which makes ARIN happy.

This patch modifies the network allocator to recognize when an network is too
small for network and broadcast addresses and skip those reservations.

There are additional unit tests to make sure the functions involved behave as expected.

Try these out:

 * `docker network create --driver bridge --subnet 10.200.1.0/31 --ip-range 10.200.1.0/31 test-31`
 * `docker network create --driver bridge --subnet 10.200.1.0/32 --ip-range 10.200.1.0/32 test-32`

My installation has been running this patch in production with /31s since March.

Signed-off-by: Mark Feit <mfeit@internet2.edu>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-10-27 13:03:19 +02:00
Brian Goff
4b981436fe Fixup libnetwork lint errors
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-06-01 23:48:32 +00:00
Brian Goff
a0a473125b Fix libnetwork imports
After moving libnetwork to this repo, we need to update all the import
paths for libnetwork to point to docker/docker/libnetwork instead of
docker/libnetwork.
This change implements that.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-06-01 21:51:23 +00:00
selansen
814f6c1f4b Add getter function for Default Address Pools
ipamutils has two default address pool. Instead of allowing them to
be accessed directly, adding get functions so that other packages
can use get APIs.

Signed-off-by: selansen <elango.siva@docker.com>
2018-08-16 15:48:42 -04:00
selansen
52e85b4b9a Global Default Address Pool support
This change brings global default address pool feature into
libnetwork. Idea is to reuse same code flow and functions that were
implemented for local scope default address pool.
Function InitNetworks carries most of the changes. local scope default
address pool init should always happen only once. But Global scope
default address pool can be initialized multiple times.

Signed-off-by: selansen <elango.siva@docker.com>
2018-08-16 11:28:24 -04:00
Euan Harris
6fd25eea33 ipam, types: Expand documentation
Signed-off-by: Euan Harris <euan.harris@docker.com>
2018-07-04 09:50:31 +01:00
Chris Telfer
cc8b2cac28 Allocate subnets in order rather than restarting
This commit prevents subnets from being reused at least initially,
instead favoring to cycle through them as we do with addresses within a
subnet.

Signed-off-by: Chris Telfer <ctelfer@docker.com>
2018-06-08 19:26:04 -04:00
Euan Harris
ebf0054912 ipam/allocator: Fix typos in error message
Signed-off-by: Euan Harris <euan.harris@docker.com>
2018-05-23 11:03:55 +01:00
Abhinandan Prativadi
2f2811dd14 Adding logs for ipam state
Signed-off-by: Abhinandan Prativadi <abhi@docker.com>
2018-05-14 15:44:49 -07:00
Abhinandan Prativadi
fe629b6eba Fixing concurrent map access
This commit fixes panic due to concurrent map access

Signed-off-by: Abhinandan Prativadi <abhi@docker.com>
2018-03-08 11:19:45 -08:00
Abhinandan Prativadi
3d44975995 Adding a unit case to verify rollover
Signed-off-by: Abhinandan Prativadi <abhi@docker.com>
2017-10-03 12:15:34 -07:00
Flavio Crisciani
38382fb29b Merge pull request #1752 from aaronlehmann/sprintfs
all: Avoid trivial uses of Sprintf
2017-08-23 15:49:46 -07:00
Derek McGowan
710e0664c4 Update logrus to v1.0.1
Fix case sensitivity issue
Update docker and runc vendors

Signed-off-by: Derek McGowan <derek@mcgstyle.net>
2017-08-07 11:20:47 -07:00
Flavio Crisciani
65860255c6 Fixed code issues
Fixed issues highlighted by the new checks

Signed-off-by: Flavio Crisciani <flavio.crisciani@docker.com>
2017-06-12 11:31:35 -07:00
Aaron Lehmann
cc0b7e6aad all: Avoid trivial uses of Sprintf
Use the string concatenation operator instead of using Sprintf for
simple string concatenation. This is usually easier to read, and allows
the compiler to detect problems with the type or number of operands,
which would be runtime errors with Sprintf.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-05-09 16:07:09 -07:00
Madhu Venugopal
1b28c5e01d Internal interface to differentiate built-in drivers from remote
Signed-off-by: Madhu Venugopal <madhu@docker.com>
2016-12-19 05:17:42 -08:00
Alessandro Boch
83dd2c193a Respect auto allocation for --ipv6
Signed-off-by: Alessandro Boch <aboch@docker.com>
2016-11-08 19:05:51 -08:00
Daehyeok Mun
7f473c779a Refactoring logrus import and formatting
This fix tries to fix logrus formatting by removing `f` from
`logrus.[Error|Warn|Debug|Fatal|Panic|Info]f` when formatting string
is not present.
Also fix import name to use original project name 'logrus' instead of
'log'

Signed-off-by: Daehyeok Mun <daehyeok@gmail.com>
2016-11-08 12:42:41 -07:00
Alessandro Boch
5889b279fb Separate pool parsing from pool auto-allocation
- In default ipam driver. Also improve error message
  in case no good pool is found

Signed-off-by: Alessandro Boch <aboch@docker.com>
2016-05-25 15:02:44 -07:00
Alessandro Boch
a9c9765b33 IPAM allocator to not accept a datastore update if already present
- Restoring the above behavior which got recently broken

Signed-off-by: Alessandro Boch <aboch@docker.com>
2016-05-12 16:43:51 -07:00
Alessandro Boch
04f5343139 Make o/p of ipam DumpDatabase() consistent
Signed-off-by: Alessandro Boch <aboch@docker.com>
2016-04-15 17:34:48 -07:00
Jana Radhakrishnan
716af1c1ee Need boltdb Register only in tests
Fixing an earlier commit which needlessly registered
boltdb in allocator.go while it is needed only for tests.

Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
2016-04-15 16:34:09 -07:00
Alessandro Boch
216def1e1b Merge pull request #1120 from mrjana/store
Remove kvstore backend deps from datastore package
2016-04-15 15:50:46 -07:00
Jana Radhakrishnan
89e72d8888 Remove kvstore deps from datastore package
Currently datastore has dependencies on various kv backends.
This is undesirable if datastore had to be used as a backend
agnostic store management package with it's cache layer. This
PR aims to achieve that.

Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
2016-04-15 15:36:44 -07:00
Jana Radhakrishnan
4289ea637a Make IPAM work even without a backing store
In general the core IPAM and bitseq implementation has
very little assumptions about the presence of a backing
store. But there are a few places where this assumption exists
and this makes it not useful as a simple in-memory allocator.
This PR removes those false assumptions.

Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
2016-04-15 14:02:30 -07:00
Jana Radhakrishnan
b0d046a1af Remove all netlink/osl deps from ipam/ipamutils
Currently ipam/ipamutils has a bunch of dependencies
in osl and netlink which makes the ipam/ipamutils harder
to use independently with other applications. This PR
modularizes ipam/ipamutils into a standalone package
with no OS level dependencies.

Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
2016-04-10 11:05:39 -07:00
Alessandro Boch
5dc5acfa58 Handle datastore update in Ipam and overlay drivers
Signed-off-by: Alessandro Boch <aboch@docker.com>
2016-02-16 13:49:49 -08:00
Bryan Boreham
81c586e0e5 Stop referring to requested IP address as "preferred",
since it is an error if that address is unavailable.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2016-01-26 15:09:29 +00:00
Alessandro Boch
a509244057 Fix predefined pool reservation
- The pool request code does not behave properly in
  case of concurrent requests when client does not
  specify a preferred pool. It may dispense the same
  predefined pool to different networks.
- The issue is common for local and global
  address spaces

Signed-off-by: Alessandro Boch <aboch@docker.com>
2016-01-22 14:19:41 -08:00
Alessandro Boch
21219731d3 IPAM to run consistency check over its bitmasks
Signed-off-by: Alessandro Boch <aboch@docker.com>
2016-01-14 08:26:14 -08:00