You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2014/06/17 18:19:55 UTC

git commit: Added APIs to find host public and loopback interfaces.

Repository: mesos
Updated Branches:
  refs/heads/master cf6b541c9 -> fb1386837


Added APIs to find host public and loopback interfaces.

Review: https://reviews.apache.org/r/22593


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/fb138683
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/fb138683
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/fb138683

Branch: refs/heads/master
Commit: fb138683792a3e97c0879f5f8d8b130cde707c4b
Parents: cf6b541
Author: Chi Zhang <ch...@gmail.com>
Authored: Tue Jun 17 09:14:39 2014 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Tue Jun 17 09:19:26 2014 -0700

----------------------------------------------------------------------
 src/linux/routing/link/link.cpp | 56 ++++++++++++++++++++++++++++++++++++
 src/linux/routing/link/link.hpp | 14 +++++++++
 src/tests/routing_tests.cpp     | 22 ++++++++++++++
 3 files changed, 92 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/fb138683/src/linux/routing/link/link.cpp
----------------------------------------------------------------------
diff --git a/src/linux/routing/link/link.cpp b/src/linux/routing/link/link.cpp
index ebcd119..a912122 100644
--- a/src/linux/routing/link/link.cpp
+++ b/src/linux/routing/link/link.cpp
@@ -32,6 +32,10 @@
 
 #include <netlink/route/link/veth.h>
 
+#include <set>
+#include <string>
+#include <vector>
+
 #include <process/delay.hpp>
 #include <process/pid.hpp>
 #include <process/process.hpp>
@@ -43,17 +47,69 @@
 #include <stout/os.hpp>
 
 #include "linux/routing/internal.hpp"
+#include "linux/routing/route.hpp"
 
 #include "linux/routing/link/internal.hpp"
 #include "linux/routing/link/link.hpp"
 
 using namespace process;
 
+using std::set;
 using std::string;
+using std::vector;
 
 namespace routing {
 namespace link {
 
+Result<string> eth0()
+{
+  Try<vector<route::Rule> > mainRoutingTable = route::table();
+  if (mainRoutingTable.isError()) {
+    return Error(
+        "Failed to retrieve the main routing table on the host: " +
+        mainRoutingTable.error());
+  }
+
+  foreach (const route::Rule& rule, mainRoutingTable.get()) {
+    if (rule.destination().isNone()) {
+      // Check if the public interface exists.
+      Try<bool> exists = link::exists(rule.link());
+      if (exists.isError()) {
+        return Error(
+            "Failed to check if " + rule.link() + " exists: " + exists.error());
+      } else if (!exists.get()) {
+        return Error(
+            rule.link() + " is in the routing table but not in the system");
+      }
+
+      return rule.link();
+    }
+  }
+
+  return None();
+}
+
+
+Result<string> lo()
+{
+  Try<set<string> > links = net::links();
+  if (links.isError()) {
+    return Error("Failed to get all the links: " + links.error());
+  }
+
+  foreach (const string& link, links.get()) {
+    Result<bool> test = link::internal::test(link, IFF_LOOPBACK);
+    if (test.isError()) {
+      return Error("Failed to check the flag on link: " + link);
+    } else if (test.get()) {
+      return link;
+    }
+  }
+
+  return None();
+}
+
+
 Try<bool> exists(const string& _link)
 {
   Result<Netlink<struct rtnl_link> > link = internal::get(_link);

http://git-wip-us.apache.org/repos/asf/mesos/blob/fb138683/src/linux/routing/link/link.hpp
----------------------------------------------------------------------
diff --git a/src/linux/routing/link/link.hpp b/src/linux/routing/link/link.hpp
index 4264a1e..9b0cbe9 100644
--- a/src/linux/routing/link/link.hpp
+++ b/src/linux/routing/link/link.hpp
@@ -37,6 +37,20 @@
 namespace routing {
 namespace link {
 
+// Returns the name of the public facing interface of the host (
+// 'eth0' on most machines). The interface returned is the first
+// interface in the routing table that has an empty 'destination'.
+// Returns None if such an interface cannot be found.
+Result<std::string> eth0();
+
+
+// Returns the name of the loopback interface of the host (the 'lo'
+// device on most machines). The interface returned has flag
+// IFF_LOOPBACK set on it. Returns None if no loopback interface can
+// be found on the host.
+Result<std::string> lo();
+
+
 // Returns true if the link exists.
 Try<bool> exists(const std::string& link);
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/fb138683/src/tests/routing_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/routing_tests.cpp b/src/tests/routing_tests.cpp
index 0e8f61a..dcc4982 100644
--- a/src/tests/routing_tests.cpp
+++ b/src/tests/routing_tests.cpp
@@ -206,6 +206,28 @@ TEST_F(RoutingTest, LinkExists)
 }
 
 
+TEST_F(RoutingTest, Eth0)
+{
+  Result<string> eth0 = link::eth0();
+  EXPECT_FALSE(eth0.isError());
+
+  if (eth0.isSome()) {
+    ASSERT_SOME_TRUE(link::exists(eth0.get()));
+  }
+}
+
+
+TEST_F(RoutingTest, Lo)
+{
+  Result<string> lo = link::lo();
+  EXPECT_FALSE(lo.isError());
+
+  if (lo.isSome()) {
+    ASSERT_SOME_TRUE(link::exists(lo.get()));
+  }
+}
+
+
 TEST_F(RoutingVethTest, ROOT_LinkCreate)
 {
   ASSERT_SOME(link::create(TEST_VETH_LINK, TEST_PEER_LINK, None()));