patchpanel: Support open a new netns via ConnectNamespace
Currently the ConnectNamespace API exposed by patchpanel via d-bus only
supports passing in a pid of a process and doing "ConnectNamespace" for
the netns associated with this process. While in the tast tests,
sometimes we need to open a new netns directly, and execute processes in
the created netns.
For this usage, this patch modifies the ConnectNamespace API so that
patchpanel accepts passing a special pid (i.e., pid==-1) to indicates
the client wants a new netns, invokes `ip netns add` for this case, and
returns the name of the created netns.
BUG=b:185210339
TEST=unit_tests;
TEST=Used this API in test VPN tast test, verified it worked;
TEST=Checked playstore still worked.
Change-Id: I3bbfab89df24899127e6087b0c0533e2c96037dc
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2896672
Reviewed-by: Garrick Evans <garrick@chromium.org>
Reviewed-by: Hugo Benichi <hugobenichi@google.com>
Tested-by: Jie Jiang <jiejiang@chromium.org>
Commit-Queue: Jie Jiang <jiejiang@chromium.org>
diff --git a/patchpanel/datapath.h b/patchpanel/datapath.h
index 37f4de3..7babb95 100644
--- a/patchpanel/datapath.h
+++ b/patchpanel/datapath.h
@@ -28,6 +28,10 @@
// Struct holding parameters for Datapath::StartRoutingNamespace requests.
struct ConnectedNamespace {
+ // The special pid which indicates this namespace is not attached to an
+ // associated process but should be/was created by `ip netns add`.
+ static constexpr pid_t kNewNetnsPid = -1;
+
// The pid of the client network namespace.
pid_t pid;
// The name attached to the client network namespace.
@@ -100,8 +104,9 @@
virtual void Stop();
// Attaches the name |netns_name| to a network namespace identified by
- // |netns_pid|. If |netns_name| had already been created, it will be deleted
- // first.
+ // |netns_pid|. If |netns_pid| is -1, a new namespace with name |netns_name|
+ // will be created instead. If |netns_name| had already been created, it will
+ // be deleted first.
virtual bool NetnsAttachName(const std::string& netns_name, pid_t netns_pid);
// Deletes the name |netns_name| of a network namespace.