Push virtio-net interface renaming into userspace (#2).

The first attempt to do the renaming in userspace did not fully work,
because the framework will enumerate the list of interfaces before they
are up, and then fail if any new interfaces appear up after booting. This
was hitting us because ril-daemon will not rename the eth0 interface to
rmnet0 until very late in the boot sequence, after this enumeration has
already completed.

We can leave the older workaround in place, as it is still very useful
for testing, but this workaround will supersede it once it has been
added to the init script.

Bug: 78482534
Test: "/vendor/bin/rename_netiface wlan0 wlan1" works
Change-Id: I4b6b70b3b1f72fe244f857d8088d8f7647636700
diff --git a/guest/commands/rename_netiface/Android.mk b/guest/commands/rename_netiface/Android.mk
new file mode 100644
index 0000000..67e97f4
--- /dev/null
+++ b/guest/commands/rename_netiface/Android.mk
@@ -0,0 +1,27 @@
+# Copyright (C) 2018 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := rename_netiface
+LOCAL_MODULE_TAGS := optional
+LOCAL_SRC_FILES := main.cpp
+LOCAL_SHARED_LIBRARIES := cuttlefish_net
+LOCAL_C_INCLUDES := device/google/cuttlefish_common
+LOCAL_MULTILIB := first
+LOCAL_VENDOR_MODULE := true
+
+include $(BUILD_EXECUTABLE)
diff --git a/guest/commands/rename_netiface/main.cpp b/guest/commands/rename_netiface/main.cpp
new file mode 100644
index 0000000..3703107
--- /dev/null
+++ b/guest/commands/rename_netiface/main.cpp
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "common/libs/net/netlink_client.h"
+#include "common/libs/net/network_interface.h"
+#include "common/libs/net/network_interface_manager.h"
+
+#include <net/if.h>
+#include <cstdio>
+
+// This command can only rename network interfaces that are *DOWN*
+
+int main(int argc, char *argv[]) {
+  if (argc != 3) {
+    fprintf(stderr, "usage: %s [ethA] [ethB]\n", argv[0]);
+    return -1;
+  }
+  const char *const name = argv[1];
+  int32_t index = if_nametoindex(name);
+  if (index == 0) {
+    fprintf(stderr, "%s: invalid interface name '%s'\n", argv[0], name);
+    return -2;
+  }
+  const char *const new_name = argv[2];
+  auto factory = cvd::NetlinkClientFactory::Default();
+  std::unique_ptr<cvd::NetlinkClient> nl(factory->New(NETLINK_ROUTE));
+  std::unique_ptr<cvd::NetworkInterfaceManager> nm(
+      cvd::NetworkInterfaceManager::New(factory));
+  std::unique_ptr<cvd::NetworkInterface> ni(nm->Open(new_name, name));
+  bool res = false;
+  if (ni) {
+    ni->SetName(new_name);
+    res = nm->ApplyChanges(*ni);
+  }
+  if (!res) {
+    fprintf(stderr, "%s: renaming interface '%s' failed\n", argv[0], name);
+    return -3;
+  }
+  return 0;
+}