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/05/15 03:21:36 UTC

[6/9] git commit: Allowed to get/set MTU for a link.

Allowed to get/set MTU for a link.

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


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

Branch: refs/heads/master
Commit: d50f487e3bf010d7bb20695903e2a0b61c83c957
Parents: 27e458e
Author: Jie Yu <yu...@gmail.com>
Authored: Fri May 9 14:45:48 2014 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Wed May 14 17:38:43 2014 -0700

----------------------------------------------------------------------
 src/linux/routing/link/link.cpp | 51 ++++++++++++++++++++++++++++++++++++
 src/linux/routing/link/link.hpp | 10 +++++++
 src/tests/routing_tests.cpp     | 18 +++++++++++++
 3 files changed, 79 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/d50f487e/src/linux/routing/link/link.cpp
----------------------------------------------------------------------
diff --git a/src/linux/routing/link/link.cpp b/src/linux/routing/link/link.cpp
index 97d03bf..62996bd 100644
--- a/src/linux/routing/link/link.cpp
+++ b/src/linux/routing/link/link.cpp
@@ -200,6 +200,57 @@ Try<bool> setMAC(const string& link, const net::MAC& mac)
 }
 
 
+Result<unsigned int> mtu(const string& _link)
+{
+  Result<Netlink<struct rtnl_link> > link = internal::get(_link);
+  if (link.isError()) {
+    return Error(link.error());
+  } else if (link.isNone()) {
+    return None();
+  }
+
+  return rtnl_link_get_mtu(link.get().get());
+}
+
+
+Try<bool> setMTU(const string& _link, unsigned int mtu)
+{
+  Result<Netlink<struct rtnl_link> > link = internal::get(_link);
+  if (link.isError()) {
+    return Error(link.error());
+  } else if (link.isNone()) {
+    return false;
+  }
+
+  // TODO(jieyu): We use ioctl to set the MTU because libnl has some
+  // issues with rtnl_link_change.
+  struct ifreq ifr;
+  memset(&ifr, 0, sizeof(ifr));
+
+  strncpy(ifr.ifr_name, _link.c_str(), IFNAMSIZ);
+  ifr.ifr_mtu = mtu;
+
+  int fd = ::socket(AF_INET, SOCK_STREAM, 0);
+  if (fd == -1) {
+    return ErrnoError();
+  }
+
+  if (ioctl(fd, SIOCSIFMTU, &ifr) == -1) {
+    if (errno == ENODEV) {
+      os::close(fd);
+      return false;
+    }
+
+    // Save the error string as os::close may overwrite errno.
+    const string message = strerror(errno);
+    os::close(fd);
+    return Error(message);
+  }
+
+  return true;
+}
+
+
 Result<hashmap<string, uint64_t> > statistics(const string& _link)
 {
   Result<Netlink<struct rtnl_link> > link = internal::get(_link);

http://git-wip-us.apache.org/repos/asf/mesos/blob/d50f487e/src/linux/routing/link/link.hpp
----------------------------------------------------------------------
diff --git a/src/linux/routing/link/link.hpp b/src/linux/routing/link/link.hpp
index fe19ff2..ef982e3 100644
--- a/src/linux/routing/link/link.hpp
+++ b/src/linux/routing/link/link.hpp
@@ -77,6 +77,16 @@ Try<bool> setUp(const std::string& link);
 Try<bool> setMAC(const std::string& link, const net::MAC& mac);
 
 
+// Returns the Maximum Transmission Unit (MTU) of the link. Returns
+// None if the link is not found.
+Result<unsigned int> mtu(const std::string& link);
+
+
+// Sets the Maximum Transmission Unit (MTU) of the link. Returns false
+// if the link is not found.
+Try<bool> setMTU(const std::string& link, unsigned int mtu);
+
+
 // Returns the statistics of the link.
 Result<hashmap<std::string, uint64_t> > statistics(const std::string& link);
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/d50f487e/src/tests/routing_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/routing_tests.cpp b/src/tests/routing_tests.cpp
index 8dfc23e..c95dcec 100644
--- a/src/tests/routing_tests.cpp
+++ b/src/tests/routing_tests.cpp
@@ -247,3 +247,21 @@ TEST_F(RoutingVethTest, ROOT_LinkSetMAC)
 
   EXPECT_ERROR(link::setMAC(TEST_VETH_LINK, net::MAC(multicast)));
 }
+
+
+TEST_F(RoutingVethTest, ROOT_LinkMTU)
+{
+  ASSERT_SOME(link::create(TEST_VETH_LINK, TEST_PEER_LINK, None()));
+
+  EXPECT_SOME_TRUE(link::exists(TEST_VETH_LINK));
+  EXPECT_SOME_TRUE(link::exists(TEST_PEER_LINK));
+
+  EXPECT_SOME_TRUE(link::setMTU(TEST_VETH_LINK, 10000));
+
+  Result<unsigned int> mtu = link::mtu(TEST_VETH_LINK);
+  ASSERT_SOME(mtu);
+  EXPECT_EQ(10000u, mtu.get());
+
+  EXPECT_NONE(link::mtu("not-exist"));
+  EXPECT_SOME_FALSE(link::setMTU("not-exist", 1500));
+}