netns: allow negative nsid

If the kernel receives a negative nsid it will automatically assign
the next available nsid. In this case alloc_netid() will set min and
max to 0 for ird_alloc(). And when max == 0 idr_alloc() will interpret
this as the maximum range, i.e. specific to nsids it will try to find
an id in the range [0,INT_MAX). This is intentionally supported in the
kernel for nsids.

Commit acbe9118ce ("ip netns: use strtol() instead of atoi()")
regressed ip netns in that respect although previously the use-case
was either accidentally supported or opaquely supported such that it
triggered the original commit. From what I can gather it went as
follows before: atoi() was called with a string indicating a negative
value which caused it to return -1 which was passed to the
kernel. Let's make it less opaque by introducing the keyword "auto":

ip netns set <netns-name> auto

will cause nsid to be set to -1 and the kernel will select an available
nsid.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
Christian Brauner 2018-02-06 19:39:31 +01:00 committed by Stephen Hemminger
parent 5f8265536f
commit 375d51caaa
2 changed files with 5 additions and 1 deletions

View File

@ -718,7 +718,10 @@ static int netns_set(int argc, char **argv)
return -1;
}
name = argv[0];
if (get_unsigned(&nsid, argv[1], 0))
/* If a negative nsid is specified the kernel will select the nsid. */
if (strcmp(argv[1], "auto") == 0)
nsid = -1;
else if (get_unsigned(&nsid, argv[1], 0))
invarg("Invalid \"netnsid\" value\n", argv[1]);
snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);

View File

@ -137,6 +137,7 @@ $ ip netns del net0
.sp
This command assigns a id to a peer network namespace. This id is valid
only in the current network namespace.
If the keyword "auto" is specified an available nsid will be chosen.
This id will be used by the kernel in some netlink messages. If no id is
assigned when the kernel needs it, it will be automatically assigned by
the kernel.