patchpanel: add System class to wrap common syscalls

This patch prepares to remove external calls to sysctl and introduces a
wrapper for system calls that can be conveniently mocked in unit tests.
Calls to ioctl() in Datapath are migrated to that wrapper.

BUG=b:178980566
TEST=unit tests. Flashed trogdor, verified ARC and Crostini
connectivity.

Change-Id: Ibc0e493e613d12de059cb57a1d37d2f0d9f81c50
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2947316
Tested-by: Hugo Benichi <hugobenichi@google.com>
Commit-Queue: Hugo Benichi <hugobenichi@google.com>
Reviewed-by: Garrick Evans <garrick@chromium.org>
diff --git a/patchpanel/system.h b/patchpanel/system.h
new file mode 100644
index 0000000..be9249d
--- /dev/null
+++ b/patchpanel/system.h
@@ -0,0 +1,39 @@
+// Copyright 2021 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PATCHPANEL_SYSTEM_H_
+#define PATCHPANEL_SYSTEM_H_
+
+#include <net/if.h>
+#include <net/route.h>
+#include <sys/ioctl.h>
+
+namespace patchpanel {
+
+// cros lint will yell to force using int16/int64 instead of long here, however
+// note that unsigned long IS the correct signature for ioctl in Linux kernel -
+// it's 32 bits on 32-bit platform and 64 bits on 64-bit one.
+using ioctl_req_t = unsigned long;  // NOLINT(runtime/int)
+
+// Stateless class used for holding all utility functions with side
+// effects on the environment. Facilitates mocking these functions in unit
+// tests.
+class System {
+ public:
+  System() = default;
+  System(const System&) = delete;
+  System& operator=(const System&) = delete;
+  virtual ~System() = default;
+
+  virtual int Ioctl(int fd, ioctl_req_t request, const char* argp);
+  int Ioctl(int fd, ioctl_req_t request, uint64_t arg);
+  int Ioctl(int fd, ioctl_req_t request, struct ifreq* ifr);
+  int Ioctl(int fd, ioctl_req_t request, struct rtentry* route);
+
+ private:
+};
+
+}  // namespace patchpanel
+
+#endif  // PATCHPANEL_SYSTEM_H_