Commit graph

2822 commits

Author SHA1 Message Date
Cory Snider
d31fa84c7c libnet/networkdb: use atomics for stats counters
The per-network statistics counters are loaded and incremented without
any concurrency control. Use atomic integers to prevent data races
without having to add any synchronization.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-10 15:21:58 -05:00
Tibor Vass
3539452ef0 libnetwork/networkdb: make go test -race ./libnetwork/networkdb pass
Signed-off-by: Tibor Vass <teabee89@gmail.com>
Co-authored-by: Cory Snider <csnider@mirantis.com>
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-10 15:18:31 -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
91725ddc92 libnet/d/ipvlan: gracefully migrate from older dbs
IPVLAN networks created on Moby v20.10 do not have the IpvlanFlag
configuration value persisted in the libnetwork database as that config
value did not exist before v23.0.0. Gracefully migrate configurations on
unmarshal to prevent type-assertion panics at daemon start after upgrade.

Fixes #44925

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-06 12:08:28 -05:00
Cory Snider
7950abcc46 libnetwork: delete CHANGELOG.md
It hasn't been updated since 2016.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-30 19:01:26 -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
6f08fe20e9 libnetwork/bit{seq,map}: delete CheckConsistency()
That method was only referenced by ipam.Allocator, but as it no longer
stores any state persistently there is no possibility for it to load an
inconsistent bit-sequence from Docker 1.9.x.

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
28edc8e2d6 libnet: convert to new-style driver registration
Per the Interface Segregation Principle, network drivers should not have
to depend on GetPluginGetter methods they do not use. The remote network
driver is the only one which needs a PluginGetter, and it is already
special-cased in Controller so there is no sense warping the interfaces
to achieve a foolish consistency. Replace all other network drivers' Init
functions with Register functions which take a driverapi.Registerer
argument instead of a driverapi.DriverCallback. Add back in Init wrapper
functions for only the drivers which Swarmkit references so that
Swarmkit can continue to build.

Refactor the libnetwork Controller to use the new drvregistry.Networks
and drvregistry.IPAMs driver registries in place of the legacy ones.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-27 11:47:42 -05:00
Cory Snider
5595311209 libnetwork/drvregistry: split up the registries
There is no benefit to having a single registry for both IPAM drivers
and network drivers. IPAM drivers are registered in a separate namespace
from network drivers, have separate registration methods, separate
accessor methods and do not interact with network drivers within a
DrvRegistry in any way. The only point of commonality is

    interface { GetPluginGetter() plugingetter.PluginGetter }

which is only used by the respective remote drivers and therefore should
be outside of the scope of a driver registry.

Create new, separate registry types for network drivers and IPAM
drivers, respectively. These types are "legacy-free". Neither type has
GetPluginGetter methods. The IPAMs registry does not have an
IPAMDefaultAddressSpaces method as that information can be queried
directly from the driver using its GetDefaultAddressSpaces method.
The Networks registry does not have an AddDriver method as that method
is a trivial wrapper around calling one of its arguments with its other
arguments.

Refactor DrvRegistry in terms of the new IPAMs and Networks registries
so that existing code in libnetwork and Swarmkit will continue to work.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-27 11:47:42 -05:00
Cory Snider
d478e13639 libnet: un-plumb datastores from IPAM inits
The datastore arguments to the IPAM driver Init() functions are always
nil, even in Swarmkit. The only IPAM driver which consumed the
datastores was builtin; all others (null, remote, windowsipam) simply
ignored it. As the signatures of the IPAM driver init functions cannot
be changed without breaking the Swarmkit build, they have to be left
with the same signatures for the time being. Assert that nil datastores
are always passed into the builtin IPAM driver's init function so that
there is no ambiguity the datastores are no longer respected.

Add new Register functions for the IPAM drivers which are free from the
legacy baggage of the Init functions. (The legacy Init functions can be
removed once Swarmkit is migrated to using the Register functions.) As
the remote IPAM driver is the only one which depends on a PluginGetter,
pass it in explicitly as an argument to Register. The other IPAM drivers
should not be forced to depend on a GetPluginGetter() method they do not
use (Interface Segregation Principle).

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-27 11:47:42 -05:00
Cory Snider
27cca19c9a libnetwork/drvregistry: drop unused args
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-26 17:56:40 -05:00
Cory Snider
befff0e13f libnetwork: remove more datastore scope plumbing
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-26 17:56:40 -05:00
Cory Snider
142b522946 libnetwork/config: remove vestiges of global scope
Without (*Controller).ReloadConfiguration, the only way to configure
datastore scopes would be by passing config.Options to libnetwork.New.
The only options defined which relate to datastore scopes are limited to
configuring the local-scope datastore. Furthermore, the default
datastore config only defines configuration for the local-scope
datastore. The local-scope datastore is therefore the only datastore
scope possible in libnetwork. Start removing code which is only
needed to support multiple datastore scopes.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-26 17:56:29 -05:00
Cory Snider
52d9883812 libnetwork: drop (*Controller).ReloadConfiguration
...as it is unused.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-26 16:48:35 -05:00
Cory Snider
e8011d7872 libnw/ipamutils: make local defaults immutable
ConfigLocalScopeDefaultNetworks is now dead code, thank goodness! Make
sure it stays dead by deleting the function. Refactor package ipamutils
to simplify things given its newly-reduced (ahem) scope.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-26 14:56:12 -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
Cory Snider
48ad9e19e4 libnetwork/netutils: drop ElectInterfaceAddresses
The function references global shared, mutable state and is no longer
needed. Deleting it brings us one step closer to getting rid of that
pesky shared state.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-26 14:56:11 -05:00
Brian Goff
3119b1ef7f
Merge pull request #44864 from corhere/libnet/split-bitseq
libnetwork: split package bitseq
2023-01-26 11:44:45 -08:00
Bjorn Neergaard
390532cbc6
libnetwork/windows/overlay: drop unused variables
These package-level variables were copied over from the Linux
implementation; drop them for clarity's sake.

Signed-off-by: Bjorn Neergaard <bneergaard@mirantis.com>
2023-01-24 12:44:19 -07:00
Bjorn Neergaard
b3e6aa9316
libnetwork/netutils: clean up GenerateIfaceName
netlink offers the netlink.LinkNotFoundError type, which we can use with
errors.As() to detect a unused link name.

Additionally, early return if GenerateRandomName fails, as reading
random bytes should be a highly reliable operation, and otherwise the
error would be swallowed by the fall-through return.

Signed-off-by: Bjorn Neergaard <bneergaard@mirantis.com>
2023-01-24 12:44:17 -07:00
Bjorn Neergaard
3775939303
libnetwork/netutils: refactor GenerateRandomName
GenerateRandomName now uses length to represent the overall length of
the string; this will help future users avoid creating interface names
that are too long for the kernel to accept by mistake. The test coverage
is increased and cleaned up using gotest.tools.

Signed-off-by: Bjorn Neergaard <bneergaard@mirantis.com>
2023-01-24 12:44:14 -07:00
Cory Snider
c0eb207b76 libnetwork/bitseq: refactor JSON marshaling
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-20 15:54:19 -05:00
Cory Snider
89ae725d23 libnetwork/bitseq: make mutex an unexported field
*bitseq.Handle should not implement sync.Locker. The mutex is a private
implementation detail which external consumers should not be able to
manipulate.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-20 15:54:14 -05:00
Cory Snider
94ef26428b libnetwork/bitseq: refactor in terms of bitmap
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-20 15:54:01 -05:00
Cory Snider
143c092187 libnetwork/bitmap: optimize binary serialization
The byte-slice temporary is fully overwritten on each loop iteration so
it can be safely reused to reduce GC pressure.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-20 13:57:44 -05:00
Cory Snider
c4d7294b5c libnetwork/bitmap: remove datastore concerns
There is a solid bit-vector datatype hidden inside bitseq.Handle, but
it's obscured by all the intrusive datastore and KVObject nonsense.
It can be used without a datastore, but the datastore baggage limits its
utility inside and outside libnetwork. Extract the datatype goodness
into its own package which depends only on the standard library so it
can be used in more situations.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-20 13:42:40 -05:00
Cory Snider
ad03a09451 libnetwork/bitmap: dup from package bitseq
...in preparation for separating the bit-sequence datatype from the
datastore persistence (KVObject) concerns. The new package's contents
are identical to those of package libnetwork/bitseq to assist in
reviewing the changes made on each side of the split.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-20 13:33:22 -05:00
Cory Snider
cd2e7fafd4 libnetwork/bitseq: add marshal/unmarshal tests
Catch any backwards-compatibility hazards with serialization of
sequences early.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-20 12:23:21 -05:00
Albin Kerouanton
ffd75c2e0c
libnetwork: Support IPv6 in arrangeUserFilterRule() (redux)
This reapplies commit 2d397beb00.

Fixes #44451.

Co-authored-by: Bjorn Neergaard <bneergaard@mirantis.com>
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
Signed-off-by: Bjorn Neergaard <bneergaard@mirantis.com>
2023-01-14 19:11:44 -07:00
Bjorn Neergaard
17723691e5
Revert "libnetwork: Support IPv6 in arrangeUserFilterRule()"
This reverts commit 2d397beb00.

moby#44706 and moby#44805 were both merged, and both refactored the same
file. The combination broke the build, and was not detected in CI as
only the combination of the two, applied to the same parent commit,
caused the failure.

moby#44706 should be carried forward, based on the current master, in
order to resolve this conflict.

Signed-off-by: Bjorn Neergaard <bneergaard@mirantis.com>
2023-01-14 15:31:56 -07:00
Bjorn Neergaard
803c21f4b2
Merge pull request #44706 from akerouanton/fix-44451
libnetwork: Support IPv6 in arrangeUserFilterRule()
2023-01-14 15:18:01 -07:00
Cory Snider
8be470eea8 libnetwork: don't embed mutex in network
Embedded structs are part of the exported surface of a struct type.
Boxing a struct value into an interface value does not erase that;
any code could gain access to the embedded struct value with a simple
type assertion. The mutex is supposed to be a private implementation
detail, but *network implements sync.Locker because the mutex is
embedded. Change the mutex to an unexported field so *network no
longer spuriously implements the sync.Locker interface.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-13 14:19:06 -05:00
Cory Snider
c71555f030 libnetwork: return concrete-typed *Endpoint
libnetwork.Endpoint is an interface with a single implementation.

https://github.com/golang/go/wiki/CodeReviewComments#interfaces

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-13 14:19:06 -05:00
Cory Snider
581f005aad libnetwork: don't embed mutex in endpoint
Embedded structs are part of the exported surface of a struct type.
Boxing a struct value into an interface value does not erase that;
any code could gain access to the embedded struct value with a simple
type assertion. The mutex is supposed to be a private implementation
detail, but *endpoint implements sync.Locker because the mutex is
embedded. Change the mutex to an unexported field so *endpoint no
longer spuriously implements the sync.Locker interface.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-13 14:19:06 -05:00
Cory Snider
0e91d2e0e9 libnetwork: return concrete-typed *Sandbox
Basically every exported method which takes a libnetwork.Sandbox
argument asserts that the value's concrete type is *sandbox. Passing any
other implementation of the interface is a runtime error! This interface
is a footgun, and clearly not necessary. Export and use the concrete
type instead.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-13 14:19:06 -05:00
Cory Snider
0425baf883 libnetwork: don't embed mutex in sandbox
Embedded structs are part of the exported surface of a struct type.
Boxing a struct value into an interface value does not erase that;
any code could gain access to the embedded struct value with a simple
type assertion. The mutex is supposed to be a private implementation
detail, but *sandbox implements sync.Locker because the mutex is
embedded. Change the mutex to an unexported field so *sandbox no
longer spuriously implements the sync.Locker interface.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-13 14:09:37 -05:00
Cory Snider
f96b9bf761 libnetwork: return concrete-typed *Controller
libnetwork.NetworkController is an interface with a single
implementation.

https://github.com/golang/go/wiki/CodeReviewComments#interfaces

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-13 14:09:37 -05:00
Cory Snider
ae09fe3da7 libnetwork: don't embed mutex in controller
Embedded structs are part of the exported surface of a struct type.
Boxing a struct value into an interface value does not erase that;
any code could gain access to the embedded struct value with a simple
type assertion. The mutex is supposed to be a private implementation
detail, but *controller implements sync.Locker because the mutex is
embedded.

    c, _ := libnetwork.New()
    c.(sync.Locker).Lock()

Change the mutex to an unexported field so *controller no longer
spuriously implements the sync.Locker interface.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-13 14:09:37 -05:00
Brian Goff
483b03562a
Merge pull request #44491 from corhere/libnetwork-minus-reexec
libnetwork: eliminate almost all reexecs
2023-01-13 10:44:25 -08:00
Bjorn Neergaard
dae48a8064
Merge pull request #44803 from akerouanton/fix-44721
libnetwork: Remove iptables nat rule when hairpin is disabled
2023-01-12 08:36:10 -07:00
Cory Snider
102090916e libnetwork: addRedirectRules without reexec
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-11 12:14:32 -05:00
Cory Snider
582dd705c1 libnetwork: fwmarker without reexec
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-11 12:14:32 -05:00
Cory Snider
d6cc02d301 libnetwork: drop (resolver).resolverKey field
...as it is now unused.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-11 12:14:32 -05:00
Cory Snider
50a4951ddc libnetwork: setup DNS resolver without reexec
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-11 12:14:32 -05:00
Cory Snider
4733127a04 libnetwork: set default VLAN without reexec
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-11 12:14:31 -05:00
Cory Snider
7037c48e58 libnetwork: set IPv6 without reexec
unshare.Go() is not used as an existing network namespace needs to be
entered, not a new one created. Explicitly lock main() to the initial
thread so as not to depend on the side effects of importing the
internal/unshare package to achieve the same.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-11 12:05:39 -05:00
Cory Snider
0246332954 libnetwork: create netns without reexec
Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-11 12:05:39 -05:00
Albin Kerouanton
ef161d4aeb
libnetwork: Clean up sysfs-based operations
- The oldest kernel version currently supported is v3.10. Bridge
parameters can be set through netlink since v3.8 (see
torvalds/linux@25c71c7). As such, we don't need to fallback to sysfs to
set hairpin mode.
- `scanInterfaceStats()` is never called, so no need to keep it alive.
- Document why `default_pvid` is set through sysfs

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2023-01-11 17:01:53 +01:00
Albin Kerouanton
566a2e4c79
libnetwork: Remove iptables nat rule when hairpin is disabled
When userland-proxy is turned off and on again, the iptables nat rule
doing hairpinning isn't properly removed. This fix makes sure this nat
rule is removed whenever the bridge is torn down or hairpinning is
disabled (through setting userland-proxy to true).

Unlike for ip masquerading and ICC, the `programChainRule()` call
setting up the "MASQ LOCAL HOST" rule has to be called unconditionally
because the hairpin parameter isn't restored from the driver store, but
always comes from the driver config.

For the "SKIP DNAT" rule, things are a bit different: this rule is
always deleted by `removeIPChains()` when the bridge driver is
initialized.

Fixes #44721.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2023-01-11 16:32:18 +01:00
Sebastiaan van Stijn
65aa43bf66
libnetwork: use example.com for tests and examples
Trying to remove the "docker.io" domain from locations where it's not relevant.
In these cases, this domain was used as a "random" domain for testing or example
purposes.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-01-10 15:27:58 +01:00
Albin Kerouanton
2d397beb00
libnetwork: Support IPv6 in arrangeUserFilterRule()
Fixes #44451.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2023-01-09 17:18:28 +01:00
Jan Garcia
6ab12ec8f4 rootless: move ./rootless to ./pkg/rootless
Signed-off-by: Jan Garcia <github-public@n-garcia.com>
2023-01-09 16:26:06 +01:00
Albin Kerouanton
b37d34307d
Clear conntrack entries for published UDP ports
Conntrack entries are created for UDP flows even if there's nowhere to
route these packets (ie. no listening socket and no NAT rules to
apply). Moreover, iptables NAT rules are evaluated by netfilter only
when creating a new conntrack entry.

When Docker adds NAT rules, netfilter will ignore them for any packet
matching a pre-existing conntrack entry. In such case, when
dockerd runs with userland proxy enabled, packets got routed to it and
the main symptom will be bad source IP address (as shown by #44688).

If the publishing container is run through Docker Swarm or in
"standalone" Docker but with no userland proxy, affected packets will
be dropped (eg. routed to nowhere).

As such, Docker needs to flush all conntrack entries for published UDP
ports to make sure NAT rules are correctly applied to all packets.

- Fixes #44688
- Fixes #8795
- Fixes #16720
- Fixes #7540
- Fixes moby/libnetwork#2423
- and probably more.

As a precautionary measure, those conntrack entries are also flushed
when revoking external connectivity to avoid those entries to be reused
when a new sandbox is created (although the kernel should already
prevent such case).

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2023-01-05 12:53:22 +01:00
Bjorn Neergaard
f5106148e3
Merge pull request #43060 from akerouanton/fix-42127
Check iptables options before looking for ip6tables binary
2022-12-29 17:13:36 -07:00
Albin Kerouanton
799cc143c9
Always use iptables -C to look for rules
iptables -C flag was introduced in v1.4.11, which was released ten
years ago. Thus, there're no more Linux distributions supported by
Docker using this version. As such, this commit removes the old way of
checking if an iptables rule exists (by using substring matching).

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2022-12-23 11:04:28 +01:00
Albin Kerouanton
205e5278c6
Merge iptables.probe() into iptables.detectIptables()
The former was doing some checks and logging warnings, whereas
the latter was doing the same checks but to set some internal variables.
As both are called only once and from the same place, there're now
merged together.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2022-12-23 11:04:28 +01:00
Cory Snider
3330fc2e93
Merge pull request #44667 from masibw/44610-logs-for-DNS-failures
libnetwork: improve logs for DNS failures
2022-12-22 16:24:47 -05:00
Tianon Gravi
bcb8f69cc5
Merge pull request #44239 from thaJeztah/resolvconf_refactor_step2
libnetwork: simplify handling of reading resolv.conf
2022-12-22 13:18:47 -08:00
Sebastiaan van Stijn
1eb09c8ce2
Merge pull request #44683 from thaJeztah/libnetwork_consts
libnetwork/netlabel: make consts actual consts, remove redundant utils
2022-12-22 21:55:05 +01:00
Yamazaki Masashi
0787ea8b26 libnetwork: improve logs for DNS failures
Signed-off-by: Yamazaki Masashi <masi19bw@gmail.com>

libnetwork: fix function call

Signed-off-by: Yamazaki Masashi <masi19bw@gmail.com>
2022-12-22 23:25:21 +09:00
Tianon Gravi
204cbfb68d
Merge pull request #44684 from thaJeztah/libnetwork_bridge_remove_deprecated
libnetwork/drivers/bridge: remove "ioctl" fallback code for legacy kernels
2022-12-21 09:41:19 -08:00
Sebastiaan van Stijn
a959487597
libnetwork/netlabel: remove Key(), Value(), and KeyValue() utils
These were only used in a single location, and in a rather bad way;
replace them with strings.Cut() which should be all we need for this.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-12-21 18:17:54 +01:00
Sebastiaan van Stijn
9015cb7111
libnetwork: controller: rename vars that collided or shadowed
- config collided with import
- cap collided with a built-in
- c collided with the "controller" receiver

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-12-21 18:17:17 +01:00
Sebastiaan van Stijn
96cfb076ce
libnetwork/netlabel: make consts actual consts
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-12-21 18:17:13 +01:00
Sebastiaan van Stijn
36151bd1d7
libnetwork/drivers/bridge: remove "ioctl" fallback code for legacy kernels
This code was forked from libcontainer (now runc) in
fb6dd9766e

From the description of this code:

> THIS CODE DOES NOT COMMUNICATE WITH KERNEL VIA RTNETLINK INTERFACE
> IT IS HERE FOR BACKWARDS COMPATIBILITY WITH OLDER LINUX KERNELS
> WHICH SHIP WITH OLDER NOT ENTIRELY FUNCTIONAL VERSION OF NETLINK

That comment was added as part of a refactor in;
4fe2c7a4db

Digging deeper into the code, it describes:

> This is more backward-compatible than netlink.NetworkSetMaster and
> works on RHEL 6.

That comment (and code) moved around a few times;

- moved into the libcontainer pkg: 6158ccad97
- moved within the networkdriver pkg: 4cdcea2047
- moved into the networkdriver pkg: 90494600d3

Ultimately leading to 7a94cdf8ed, which implemented
this:

> create the bridge device with ioctl
>
> On RHEL 6, creation of a bridge device with netlink fails.  Use the more
> backward-compatible ioctl instead.  This fixes networking on RHEL 6.

So from that information, it looks indeed to support RHEL 6, and Ubuntu 12.04
which are both EOL, and we haven't supported for a long time, so probably time
to remove this.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-12-21 17:32:04 +01:00
Sebastiaan van Stijn
46f7c92c9a
libnetwork: use strings.Cut() and minor refactor
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-12-21 11:09:03 +01:00
Sebastiaan van Stijn
87ca9490b0
libnetwork/datastore: rename var that collided with import
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-12-21 11:09:03 +01:00
Sebastiaan van Stijn
c766258e9c
Merge pull request #44409 from corhere/libnetwork-test-sanity
libnetwork: improve tests & fix some bugs
2022-12-16 12:30:02 +01:00
Sebastiaan van Stijn
cc1884dc04
Merge pull request #44501 from tiborvass/immutable_radix
libnetwork: use go-immutable-radix instead of radix
2022-12-06 12:46:53 +01:00
Tibor Vass
eaa74497b8
libnetwork: use go-immutable-radix instead of radix
This commit allows to remove dependency on the mutable version armon/go-radix.

The go-immutable-radix package is better maintained.

It is likely that a bit more memory will be used when using the
immutable version, though discarded nodes are being reused in a pool.
These changes happen when networks are added/removed or nodes come and
go in a cluster, so we are still talking about a relatively low
frequency event.

The major changes compared to the old radix are when modifying (insert
or delete) a tree, and those are pretty self-contained: we replace the
entire immutable tree under a lock.

Signed-off-by: Tibor Vass <teabee89@gmail.com>
2022-11-30 17:03:46 -08:00
AdamKorcz
93fa093122
testing: move fuzzers over from OSS-Fuzz
Signed-off-by: AdamKorcz <adam@adalogics.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-30 17:31:03 +01:00
Sebastiaan van Stijn
0f0fce5dcc
libnetwork/netutils: FindAvailableNetwork(): simplify reading of resolv.conf
We only need the content here, not the checksum, so simplifying the code by
just using os.ReadFile().

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-29 20:10:43 +01:00
Sebastiaan van Stijn
0cbe6524db
libnetwork/drivers/overlay: getBridgeNamePrefix() simplify reading of resolv.conf
We only need the content here, not the checksum, so simplifying the code by
just using os.ReadFile().

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-29 20:10:42 +01:00
Sebastiaan van Stijn
0ef0dab2f7
libnetwork: sandbox.rebuildDNS() make updating sandbox more atomic
Don't update external resolvers until after we successfully parsed all options.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-29 20:10:42 +01:00
Sebastiaan van Stijn
806b4fbcad
libnetwork: sandbox.rebuildDNS() move variables closer to where they're used
Some of these options required parsing the resolv.conf file, but the function
could return an error further down; this patch moves the parsing closer to
where their results are used (which may not happen if we're encountering an
error before).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-29 20:10:42 +01:00
Sebastiaan van Stijn
d178a71b7c
libnetwork: sandbox.rebuildDNS() simplify reading of resolv.conf
We only need the content here, not the checksum, so simplifying the code by
just using os.ReadFile().

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-29 20:10:41 +01:00
Sebastiaan van Stijn
05b3356631
libnetwork: sandbox.setupDNS() don't parse host resolv.conf if not needed
The existing code was always parsing the host's resolv.conf to read
the nameservers, searchdomain and options, but those options were
only needed if these options were not configured on the sandbox.

This patch reverses the logic to only parse the resolv.conf if
no options are present in the sandbox configuration.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-29 20:10:41 +01:00
Sebastiaan van Stijn
08d5e3d0fa
libnetwork: sandbox.setupDNS() simplify reading of resolv.conf
We only need the content here, not the checksum, so simplifying the
code by just using os.ReadFile().

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-29 20:10:40 +01:00
Cory Snider
4651269dc8
Merge pull request #43384 from ch33hau/42696-panic-test
libnetwork/iptables: Fix test panic when execute only one test
2022-11-17 12:14:20 -05:00
Chee Hau Lim
a2cea992c2 libnetwork/iptables: Fix test panic when execute only one test
- use local variables for chains instead of sharing global variables
- make createNewChain a t.Helper

Signed-off-by: Chee Hau Lim <ch33hau@gmail.com>
2022-11-15 14:20:49 +01:00
Cory Snider
6a15f40803 libnetwork_test: improve OptionBoltdbWithRandomDBFile
Now that this function is only ever called from contexts where a
*testing.T value is available, several improvements can be made to it.
Refactor it to be a test helper function so that callers do not need to
check and fail the test themselves. Leverage t.TempDir() so that the
temporary file is cleaned up automatically. Change it to return a single
config.Option to get better ergonomics at call sites.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 18:06:30 -05:00
Cory Snider
a0f9caec99 libnetwork_test: stop controllers at end of test
There are a handful of tests which neglected to stop all controllers
they created.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:58:06 -05:00
Cory Snider
010077ba0f libnet/d/bridge: fix race condition in test case
TestCreateParallel, which was ostensibly added as a regression test for
race conditions inside the bridge driver, contains a race condition. The
getIPv4Data() calls race the network configuration and so will sometimes
see the existing address assignments return IP address ranges which do
not conflict with them. While normally a good thing, the test asserts
that exactly one of the 100 networks is successfully created. Pass the
same IPAM data when attempting to create every network to ensure that
the address ranges conflict.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:58:06 -05:00
Cory Snider
7b2308980c libnet/d/bridge: fix bridgeInterface.addresses()
addresses() would incorrectly return all IP addresses assigned to any
interface in the network namespace if exists() is false. This went
unnoticed as the unit test covering this case tested the method inside a
clean new network namespace, which had no interfaces brought up and
therefore no IP addresses assigned. Modifying
testutils.SetupTestOSContext() to bring up the loopback interface 'lo'
resulted in the loopback addresses 127.0.0.1 and [::1] being assigned to
the loopback interface, causing addresses() to return the loopback
addresses and TestAddressesEmptyInterface to start failing. Fix the
implementation of addresses() so that it only ever returns addresses for
the bridge interface.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:58:06 -05:00
Cory Snider
c2a087a9f7 libnet/d/bridge: use fresh PortAllocator in tests
portallocator.PortAllocator holds persistent state on port allocations
in the network namespace. It normal operation it is used as a singleton,
which is a problem in unit tests as every test runs in a clean network
namespace. The singleton "default" PortAllocator remembers all the port
allocations made in other tests---in other network namespaces---and
can result in spurious test failures. Refactor the bridge driver so that
tests can construct driver instances with unique PortAllocators.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:58:06 -05:00
Cory Snider
1b64f2e28b libnetwork: stop caching global netlink handle
The global netlink handle ns.NlHandle() is indirectly cached for the
life of the process by the netutils.CheckRouteOverlaps() function. This
caching behaviour is a problem for the libnetwork unit tests as the
global netlink handle changes every time testutils.SetupTestOSContext()
is called, i.e. at the start of nearly every test case. Route overlaps
can be checked for in the wrong network namespace, causing spurious test
failures e.g. when running the same test twice in a row with -count=2.
Stop the netlink handle from being cached by shadowing the package-scope
variable with a function-scoped one.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:58:06 -05:00
Cory Snider
d0096bba21 libnetwork_test: overhaul TestParallel
TestParallel has been written in an unusual style which relies on the
testing package's intra-test parallelism feature and lots of global
state to test one thing using three cooperating parallel tests. It is
complicated to reason about and quite brittle. For example, the command

    go test -run TestParallel1 ./libnetwork

would deadlock, waiting until the test timeout for TestParallel2 and
TestParallel3 to run. And the test would be skipped if the
'-test.parallel' flag was less than three, either explicitly or
implicitly (default: GOMAXPROCS).

Overhaul TestParallel to address the aforementioned deficiencies and
get rid of mutable global state.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:58:06 -05:00
Cory Snider
32ace57479 libnetwork_test: isolate tests from each other
Reusing the same "OS context" (read: network namespace) and
NetworkController across multiple tests risks tests interfering with
each other, or worse: _depending on_ other tests to set up
preconditions. Construct a new controller for every test which needs
one, and run every test which mutates or inspects the host environment
in a fresh OS context.

The only outlier is runParallelTests. It is the only remaining test
implementation which references the "global" package-scoped controller,
so the global controller instance is effectively private to that one
test.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:56:45 -05:00
Cory Snider
0411336b49 libnetwork_test: pass controller into createTestNetwork
Sharing a single NetworkController instance between multiple tests
makes it possible for tests to interfere with each other. As a first
step towards giving each test its own private controller instance, make
explicit which controller createTestNetwork() creates the test network
on.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:55:25 -05:00
Cory Snider
9a0953a0a0 libnet/testutils: spawn goroutines in test OS ctxs
There are a handful of libnetwork tests which need to have multiple
concurrent goroutines inside the same test OS context (network
namespace), including some platform-agnostic tests. Provide test
utilities for spawning goroutines inside an OS context and
setting/restoring an existing goroutine's OS context to abstract away
platform differences and so that each test does not need to reinvent the
wheel.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:55:25 -05:00
Cory Snider
07be7b087d libnetwork_test: remove in-container special case
The SetupTestOSContext calls were made conditional in
https://github.com/moby/libnetwork/pull/148 to work around limitations
in runtime.LockOSThread() which existed before Go 1.10. This workaround
is no longer necessary now that runtime.UnlockOSThread() needs to be
called an equal number of times before the goroutine is unlocked from
the OS thread.

Unfortunately some tests break when SetupTestOSContext is not skipped.
(Evidently this code path has not been exercised in a long time.) A
newly-created network namespace is very barebones: it contains a
loopback interface in the down state and little else. Even pinging
localhost does not work inside of a brand new namespace. Set the
loopback interface to up during namespace setup so that tests which
need to use the loopback interface can do so.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:55:25 -05:00
Cory Snider
e2a89b7ad1 libnet/d/bridge: configure store when opts missing
If the GenericData option is missing from the map, the bridge driver
would skip configuration. At first glance this seems fine: the driver
defaults are to not configure anything. But it also skips over
initializing the persistent storage, which is configured through other
option keys in the map. Fix this oversight by removing the early return.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:55:25 -05:00
Cory Snider
8a20564847 libnet/datastore: stop sharing defaultScopes singleton
Each call to datastore.DefaultScopes() would modify and return the same
map. Consequently, some of the config for every NetworkController in the
process would be mutated each time one is constructed or reconfigured.
This behaviour is unexpected, unintended, and undesirable. Stop
accidentally sharing configuration by changing DefaultScopes() to return
a distinct map on each call.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:55:24 -05:00
Cory Snider
8404507b9b libnet/osl: stop assuming caller thread is clean
(*networkNamespace).InvokeFunc() cleaned up the state of the locked
thread by setting its network namespace to the netns of the goroutine
which called InvokeFunc(). If InvokeFunc() was to be called after the
caller had modified its thread's network namespace, InvokeFunc() would
incorrectly "restore" the state of its goroutine thread to the wrong
namespace, violating the invariant that unlocked threads are fungible.
Change the implementation to restore the thread's netns to the netns
that particular thread had before InvokeFunc() modified it.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-08 17:55:24 -05:00
Cory Snider
f39b83e232 libnetwork: fix TestParallel "bad file descriptor"
When running inside a container, testns == origns. Consequently, closing
testns causes the deferred netns.Set(origns) call to fail. Stop closing
the aliased original namespace handle.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-04 14:07:54 -04:00
Cory Snider
fe6706a2ce libnetwork: make tests less dependent on others
TestResolvConfHost and TestParallel both depended on a network named
"testhost" already existing in the libnetwork controller. Only TestHost
created that network, so the aforementioned tests would fail unless run
after TestHost. Fix those tests so they can be run in any order.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-11-04 13:55:26 -04:00
Sebastiaan van Stijn
6c829007cc
Merge pull request #44356 from corhere/libnetwork-namespace-correctness
libnetwork: fix restoring thread network namespaces
2022-11-03 22:33:29 +01:00
Sebastiaan van Stijn
542c735926
Merge pull request #44256 from thaJeztah/redundant_sprintfs
replace redundant fmt.Sprintf() with strconv
2022-10-25 16:48:15 -04:00
Cory Snider
22529b81f8 libnetwork: drop InitOSContext()
The function is a no-op on all platforms.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-10-25 13:35:44 -04:00
Cory Snider
7fc29c1435 libnetwork/osl: clean up Linux InvokeFunc()
Aside from unconditionally unlocking the OS thread even if restoring the
thread's network namespace fails, func (*networkNamespace).InvokeFunc()
correctly implements invoking a function inside a network namespace.
This is far from obvious, however. func InitOSContext() does much of the
heavy lifting but in a bizarre fashion: it restores the initial network
namespace before it is changed in the first place, and the cleanup
function it returns does not restore the network namespace at all! The
InvokeFunc() implementation has to restore the network namespace
explicitly by deferring a call to ns.SetNamespace().

func InitOSContext() is a leaky abstraction taped to a footgun. On the
one hand, it defensively resets the current thread's network namespace,
which has the potential to fix up the thread state if other buggy code
had failed to maintain the invariant that an OS thread must be locked to
a goroutine unless it is interchangeable with a "clean" thread as
spawned by the Go runtime. On the other hand, it _facilitates_ writing
buggy code which fails to maintain the aforementioned invariant because
the cleanup function it returns unlocks the thread from the goroutine
unconditionally while neglecting to restore the thread's network
namespace! It is quite scary to need a function which fixes up threads'
network namespaces after the fact as an arbitrary number of goroutines
could have been scheduled onto a "dirty" thread and run non-libnetwork
code before the thread's namespace is fixed up. Any number of
(not-so-)subtle misbehaviours could result if an unfortunate goroutine
is scheduled onto a "dirty" thread. The whole repository has been
audited to ensure that the aforementioned invariant is never violated,
making after-the-fact fixing up of thread network namespaces redundant.
Make InitOSContext() a no-op on Linux and inline the thread-locking into
the function (singular) which previously relied on it to do so.

func ns.SetNamespace() is of similarly dubious utility. It intermixes
capturing the initial network namespace and restoring the thread's
network namespace, which could result in threads getting put into the
wrong network namespace if the wrong thread is the first to call it.
Delete it entirely; functions which need to manipulate a thread's
network namespace are better served by being explicit about capturing
and restoring the thread's namespace.

Rewrite InvokeFunc() to invoke the closure inside a goroutine to enable
a graceful and safe recovery if the thread's network namespace could not
be restored. Avoid any potential race conditions due to changing the
main thread's network namespace by preventing the aforementioned
goroutines from being eligible to be scheduled onto the main thread.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-10-25 13:35:44 -04:00
Cory Snider
d1e3705c1a libnet/d/overlay: restore thread netns
func (*network) watchMiss() correctly locks its goroutine to an OS
thread before changing the thread's network namespace, but neglects to
restore the thread's network namespace before unlocking. Fix this
oversight by unlocking iff the thread's network namespace is
successfully restored.

Prevent the watchMiss goroutine from being locked to the main thread to
avoid the issues which would arise if such a situation was to occur.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-10-25 13:35:44 -04:00
Cory Snider
3e2f0c7a39 libnetwork: fixup thread locking in Linux tests
The parallel tests were unconditionally unlocking the test case
goroutine from the OS thread, irrespective of whether the thread's
network namespace was successfully restored. This was not a problem in
practice as the unpaired calls to runtime.LockOSThread() peppered
through the test case would have prevented the goroutine from being
unlocked. Unlock the goroutine from the thread iff the thread's network
namespace is successfully restored.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-10-25 13:35:44 -04:00
Cory Snider
afa41b16ea libnetwork/testutils: restore netns on teardown
testutils.SetupTestOSContext() sets the calling thread's network
namespace but neglected to restore it on teardown. This was not a
problem in practice as it called runtime.LockOSThread() twice but
runtime.UnlockOSThread() only once, so the tampered threads would be
terminated by the runtime when the test case returned and replaced with
a clean thread. Correct the utility so it restores the thread's network
namespace during teardown and unlocks the goroutine from the thread on
success.

Remove unnecessary runtime.LockOSThread() calls peppering test cases
which leverage testutils.SetupTestOSContext().

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-10-24 15:37:46 -04:00
Sebastiaan van Stijn
3db11af44b
libnetwork/drivers/overlay: use filepath.WalkDir instead of filepath.Walk
WalkDir is more performant as it doesn't perform an os.Lstat on every visited
file or directory.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-10-09 17:25:03 +02:00
Sebastiaan van Stijn
145817a9cf
libnetwork: use strconv instead of fmt.Sprintf()
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-10-08 17:41:39 +02:00
Sebastiaan van Stijn
1515e02c8a
Merge pull request #44215 from corhere/fix-unlockosthread-pdeathsig
Stop subprocesses from getting unexpectedly killed
2022-10-06 20:08:53 +02:00
Cory Snider
1f22b15030 Lock OS threads when exec'ing with Pdeathsig
On Linux, when (os/exec.Cmd).SysProcAttr.Pdeathsig is set, the signal
will be sent to the process when the OS thread on which cmd.Start() was
executed dies. The runtime terminates an OS thread when a goroutine
exits after being wired to the thread with runtime.LockOSThread(). If
other goroutines are allowed to be scheduled onto a thread which called
cmd.Start(), an unrelated goroutine could cause the thread to be
terminated and prematurely signal the command. See
https://github.com/golang/go/issues/27505 for more information.

Prevent started subprocesses with Pdeathsig from getting signaled
prematurely by wiring the starting goroutine to the OS thread until the
subprocess has exited. No other goroutines can be scheduled onto a
locked thread so it will remain alive until unlocked or the daemon
process exits.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-10-05 12:18:03 -04:00
Sebastiaan van Stijn
c523d6d25c
libnetwork: remove some outdated comments
The corresponding "nolint" comments were removed in 2f1c382a6d,
but didn't remove these comments.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-10-04 15:34:39 +02:00
Sebastiaan van Stijn
60ace31be0
libnetwork: sandbox.updateDNS(): use const for file permissions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-10-02 01:28:05 +02:00
Sebastiaan van Stijn
f0be4d126d
libnetwork: use object-literal for some structs
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-10-02 01:26:15 +02:00
Sebastiaan van Stijn
50a7c67363
libnetwork: fix some whitespace formatting
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-10-02 01:26:14 +02:00
Sebastiaan van Stijn
481185fb8a
libnetwork/osl: fix GoDoc formatting
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-10-02 01:26:14 +02:00
Sebastiaan van Stijn
173d16b233
Merge pull request #44193 from thaJeztah/libnetwork_cleanup
libnetwork: cleanup config package, remove old integration tests
2022-09-27 22:41:32 +02:00
Sebastiaan van Stijn
762fc76cf9
Merge pull request #44089 from thaJeztah/update_golangci_lint
golangci-lint: update to v1.49.0
2022-09-27 18:24:15 +02:00
Sebastiaan van Stijn
89555e45f2
Merge pull request #44191 from corhere/drop-containerfs-iface
Remove LCOW: pkg/containerfs: drop ContainerFS abstraction
2022-09-27 10:28:35 +02:00
Sebastiaan van Stijn
e3d80cfc15
Merge pull request #44179 from thaJeztah/resolvconf_deadcode
libnetwork/resolvconf: removed unused GetIfChanged() and GetLastModified()
2022-09-26 23:51:52 +02:00
Sebastiaan van Stijn
cd381aea56
libnetwork: fix empty-lines (revive)
libnetwork/etchosts/etchosts_test.go:167:54: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/osl/route_linux.go:185:74: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/osl/sandbox_linux_test.go:323:36: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/bitseq/sequence.go:412:48: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/datastore/datastore_test.go:67:46: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/datastore/mock_store.go:34:60: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/iptables/firewalld.go:202:44: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/iptables/firewalld_test.go:76:36: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/iptables/iptables.go:256:67: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/iptables/iptables.go:303:128: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/networkdb/cluster.go:183:72: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/ipams/null/null_test.go:44:38: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/macvlan/macvlan_store.go:45:52: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/ipam/allocator_test.go:1058:39: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/bridge/port_mapping.go:88:111: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/bridge/link.go:26:90: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/bridge/setup_ipv6_test.go:17:34: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/bridge/setup_ip_tables.go:392:4: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/bridge/bridge.go:804:50: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/ov_serf.go:183:29: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/ov_utils.go:81:64: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/overlay/peerdb.go:172:67: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/peerdb.go:209:67: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/peerdb.go:344:89: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/peerdb.go:436:63: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/overlay.go:183:36: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/encryption.go:69:28: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/overlay/ov_network.go:563:81: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/default_gateway.go:32:43: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/errors_test.go:9:40: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/service_common.go:184:64: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/endpoint.go:161:55: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/store.go:320:33: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/store_linux_test.go:11:38: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/sandbox.go:571:36: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/service_common.go:317:246: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/endpoint.go:550:17: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/sandbox_dns_unix.go:213:106: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/controller.go:676:85: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/agent.go:876:60: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/resolver.go:324:69: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/network.go:1153:92: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/network.go:1955:67: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/network.go:2235:9: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/libnetwork_internal_test.go:336:26: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/resolver_test.go:76:35: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/libnetwork_test.go:303:38: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/libnetwork_test.go:985:46: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/ipam/allocator_test.go:1263:37: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/errors_test.go:9:40: empty-lines: extra empty line at the end of a block (revive)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-26 19:21:58 +02:00
Sebastiaan van Stijn
267108e113
libnetwork/config: rename ParseConfigOptions() to New()
This function effectively is a constructor, so rename it to better describe
it's functionality.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-26 19:20:55 +02:00
Sebastiaan van Stijn
09cc2f9d0e
libnetwork/config: inline LoadDefaultScopes()
This method was an exported method, but only used as part of ParseConfigOptions,
so inlining it.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-26 17:40:06 +02:00
Sebastiaan van Stijn
528428919e
libnetwork/config: merge DaemonCfg into Config
It was unclear what the distinction was between these configuration
structs, so merging them to simplify.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-26 12:05:37 +02:00
Sebastiaan van Stijn
571baffd59
libnetwork: remove old integration tests
This was used for testing purposes when libnetwork was in a separate repo, using
the dnet utility, which was removed in 7266a956a8.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-26 12:05:37 +02:00
Sebastiaan van Stijn
46f4a45769
libnetwork/config: remove ParseConfig()
Libnetwork configuration files were only used as part of integration tests using
the dnet utility, which was removed in 7266a956a8

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-26 12:05:37 +02:00
Sebastiaan van Stijn
7d574f5ac6
libnetwork/config: inline ProcessOptions
This method was only used in a single place; inlining it makes it
easier to see what's done.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-26 12:05:37 +02:00
Sebastiaan van Stijn
a8a8bd1e42
libnetwork/config: remove "Experimental" and "Debug" options
These were no longer used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-26 12:05:22 +02:00
Sebastiaan van Stijn
2f1c382a6d
golangci-lint: update to v1.49.0
Remove the "deadcode", "structcheck", and "varcheck" linters, as they are
deprecated:

    WARN [runner] The linter 'deadcode' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter.  Replaced by unused.
    WARN [runner] The linter 'structcheck' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter.  Replaced by unused.
    WARN [runner] The linter 'varcheck' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter.  Replaced by unused.
    WARN [linters context] structcheck is disabled because of generics. You can track the evolution of the generics support by following the https://github.com/golangci/golangci-lint/issues/2649.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-23 23:31:27 +02:00
Cory Snider
e332c41e9d pkg/containerfs: alias ContainerFS to string
Drop the constructor and redundant string() type-casts.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-09-23 16:56:52 -04:00
Sebastiaan van Stijn
49de15cdcc
libnetwork/resolvconf: removed unused GetIfChanged() and GetLastModified()
These functions were used in 63a7ccdd23, which was
part of Docker v1.5.0 and v1.6.0, but removed in Docker v1.7.0 when the network
stack was replaced with libnetwork in d18919e304.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-22 13:58:34 +02:00
Sebastiaan van Stijn
55fd77f724
set ReadHeaderTimeout to address G112: Potential Slowloris Attack (gosec)
After discussing in the maintainers meeting, we concluded that Slowloris attacks
are not a real risk other than potentially having some additional goroutines
lingering around, so setting a long timeout to satisfy the linter, and to at
least have "some" timeout.

    libnetwork/diagnostic/server.go:96:10: G112: Potential Slowloris Attack because ReadHeaderTimeout is not configured in the http.Server (gosec)
        srv := &http.Server{
            Addr:    net.JoinHostPort(ip, strconv.Itoa(port)),
            Handler: s,
        }
    api/server/server.go:60:10: G112: Potential Slowloris Attack because ReadHeaderTimeout is not configured in the http.Server (gosec)
                srv: &http.Server{
                    Addr: addr,
                },
    daemon/metrics_unix.go:34:13: G114: Use of net/http serve function that has no support for setting timeouts (gosec)
            if err := http.Serve(l, mux); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
                      ^
    cmd/dockerd/metrics.go:27:13: G114: Use of net/http serve function that has no support for setting timeouts (gosec)
            if err := http.Serve(l, mux); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
                      ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-22 12:13:28 +02:00
Samuel Karp
7860686a8d
Merge pull request #44090 from thaJeztah/fix_linting_issues 2022-09-07 14:27:03 -07:00
Sebastiaan van Stijn
58413c15cb
update to golang 1.19
also ran gofmt with go1.19

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-07 15:27:16 +02:00
Sebastiaan van Stijn
561a010161
linting: suppress false positive for G404 (gosec)
The linter falsely detects this as using "math/rand":

    libnetwork/networkdb/cluster.go:721:14: G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec)
       val, err := rand.Int(rand.Reader, big.NewInt(int64(n)))
                   ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-04 15:36:49 +02:00
Sebastiaan van Stijn
a33d1f9a7c
libnetwork/diagnostic: EnableDiagnostic(): use net.JoinHostPort
Use net.JoinHostPort to account for IPv6 addresses.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-04 15:36:47 +02:00
Youfu Zhang
549d24b437 libnetwork/drivers/ipvlan: fix missing IpvlanFlag field in config JSON
Fixes #42542

Signed-off-by: Youfu Zhang <zhangyoufu@gmail.com>
2022-08-24 16:23:32 +08:00
Brian Goff
724feb898f
Merge pull request #43857 from corhere/follow-up-42829
libnetwork: refactor networkdb test implementation
2022-08-16 10:48:02 -07:00
Tianon Gravi
8d9d5a3bb5
Merge pull request #43844 from Abirdcfly/master
fix minor code unreachability error
2022-08-12 17:12:15 -07:00
Abirdcfly
9031de6a9b fix minor code unreachability error
Signed-off-by: Abirdcfly <fp544037857@gmail.com>
2022-08-04 22:32:59 +08:00
Cory Snider
1213881712 libnetwork: refactor networkdb test implementation
Leverage higher-order functions to DRY the polling checks in
TestNetworkDBNodeJoinLeaveIteration.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-07-25 09:44:35 -04:00
David Wang
f499c6b9ec
Test: wait for network changes in TestNetworkDBNodeJoinLeaveIteration
In network node change test, the expected behavior is focused on how many nodes
left in networkDB, besides timing issues, things would also go tricky for a
leave-then-join sequence, if the check (counting the nodes) happened before the
first "leave" event, then the testcase actually miss its target and report PASS
without verifying its final result; if the check happened after the 'leave' event,
but before the 'join' event, the test would report FAIL unnecessary;

This code change would check both the db changes and the node count, it would
report PASS only when networkdb has indeed changed and the node count is expected.

Signed-off-by: David Wang <00107082@163.com>
2022-07-21 22:56:58 +08:00
Sebastiaan van Stijn
5de77049db
Merge pull request #43793 from AkihiroSuda/fix-43781
libnetwork: skip firewalld management for rootless
2022-07-15 13:44:11 +02:00
Akihiro Suda
9464898b47
libnetwork: skip firewalld management for rootless
Fix issue 43781

Co-authored-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
2022-07-15 00:59:11 +09:00
Sebastiaan van Stijn
4f08346686
fix formatting of "nolint" tags for go1.19
The correct formatting for machine-readable comments is;

    //<some alphanumeric identifier>:<options>[,<option>...][ // comment]

Which basically means:

- MUST NOT have a space before `<identifier>` (e.g. `nolint`)
- Identified MUST be alphanumeric
- MUST be followed by a colon
- MUST be followed by at least one `<option>`
- Optionally additional `<options>` (comma-separated)
- Optionally followed by a comment

Any other format will not be considered a machine-readable comment by `gofmt`,
and thus formatted as a regular comment. Note that this also means that a
`//nolint` (without anything after it) is considered invalid, same for `//#nosec`
(starts with a `#`).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-13 22:31:53 +02:00
Tianon Gravi
c8d18e27bd
Merge pull request #43760 from thaJeztah/vlan_cleanups
libnetwork: some cleaning up in ipvlan and macvlan drivers
2022-07-13 11:53:02 -07:00
Sebastiaan van Stijn
52c1a2fae8
gofmt GoDoc comments with go1.19
Older versions of Go don't format comments, so committing this as
a separate commit, so that we can already make these changes before
we upgrade to Go 1.19.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-08 19:56:23 +02:00
Sebastiaan van Stijn
968ff5ab44
fix some minor linting issues
libnetwork/firewall_linux.go:11:21: var-declaration: should drop = nil from declaration of var ctrl; it is the zero value (revive)
        ctrl *controller = nil
                           ^
    distribution/pull_v2_test.go:213:4: S1038: should use t.Fatalf(...) instead of t.Fatal(fmt.Sprintf(...)) (gosimple)
                t.Fatal(fmt.Sprintf("expected formatPlatform to show windows platform with a version, but got '%s'", result))
                ^
    integration-cli/docker_cli_build_test.go:5951:3: S1038: should use c.Skipf(...) instead of c.Skip(fmt.Sprintf(...)) (gosimple)
            c.Skip(fmt.Sprintf("Bug fixed in 18.06 or higher.Skipping it for %s", testEnv.DaemonInfo.ServerVersion))
            ^
    integration-cli/docker_cli_daemon_test.go:240:3: S1038: should use c.Skipf(...) instead of c.Skip(fmt.Sprintf(...)) (gosimple)
            c.Skip(fmt.Sprintf("New base device size (%v) must be greater than (%s)", units.HumanSize(float64(newBasesizeBytes)), units.HumanSize(float64(oldBasesizeBytes))))
            ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-04 10:15:28 +02:00
Sebastiaan van Stijn
b1a6d5388d
libnetwork: macvlan: reduce use of const for driver name
Inlining the string makes the code more grep'able; renaming the
const to "driverName" to reflect the remaining uses of it.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-01 13:07:39 +02:00
Sebastiaan van Stijn
aca80d1cda
libnetwork: ipvlan: reduce use of const for driver name
Inlining the string makes the code more grep'able; renaming the
const to "driverName" to reflect the remaining uses of it.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-01 11:44:49 +02:00
Sebastiaan van Stijn
dddb4d25d2
libnetwork: macvlan: cleanup parseNetworkGenericOptions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-01 11:44:47 +02:00
Sebastiaan van Stijn
1992190162
libnetwork: macvlan: make configuration.fromOptions a constructor
This was effectively a constructor, but through some indirection; make it a
regular function, which is a bit more idiomatic.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-01 11:44:46 +02:00
Sebastiaan van Stijn
99bde59229
libnetwork: ipvlan: cleanup parseNetworkGenericOptions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-07-01 11:44:44 +02:00