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/06 19:43:22 UTC

git commit: Added terminal action to routing lib.

Repository: mesos
Updated Branches:
  refs/heads/master 10a9dac59 -> 3541b8194


Added terminal action to routing lib.

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


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

Branch: refs/heads/master
Commit: 3541b819470a41cc7ae40fdcf6c99e4b3774c18b
Parents: 10a9dac
Author: Jie Yu <yu...@gmail.com>
Authored: Mon Jun 2 09:34:50 2014 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Jun 6 10:43:03 2014 -0700

----------------------------------------------------------------------
 src/linux/routing/filter/action.hpp   |  5 +++
 src/linux/routing/filter/internal.hpp | 68 +++++++++++++++++++++++-------
 src/linux/routing/filter/ip.cpp       | 17 ++++++++
 src/linux/routing/filter/ip.hpp       | 12 ++++++
 4 files changed, 87 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/3541b819/src/linux/routing/filter/action.hpp
----------------------------------------------------------------------
diff --git a/src/linux/routing/filter/action.hpp b/src/linux/routing/filter/action.hpp
index 8cc6594..410c15a 100644
--- a/src/linux/routing/filter/action.hpp
+++ b/src/linux/routing/filter/action.hpp
@@ -70,6 +70,11 @@ private:
   std::set<std::string> links_;
 };
 
+
+// Represents an action that stops the packet from being sent to the
+// next filter.
+class Terminal : public Action {};
+
 } // namespace action {
 } // namespace routing {
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/3541b819/src/linux/routing/filter/internal.hpp
----------------------------------------------------------------------
diff --git a/src/linux/routing/filter/internal.hpp b/src/linux/routing/filter/internal.hpp
index cd3279a..6836d1f 100644
--- a/src/linux/routing/filter/internal.hpp
+++ b/src/linux/routing/filter/internal.hpp
@@ -131,6 +131,15 @@ inline Try<Nothing> attach(
       rtnl_act_put(act);
       return Error(std::string(nl_geterror(err)));
     }
+
+    // Automatically set the 'terminal' flag for u32 filters if a
+    // redirect action is attached.
+    err = rtnl_u32_set_cls_terminal(cls.get());
+    if (err != 0) {
+      return Error(
+          "Failed to set the terminal flag: " +
+          std::string(nl_geterror(err)));
+    }
   } else {
     rtnl_act_put(act);
     return Error("Unsupported classifier kind: " + kind);
@@ -145,6 +154,8 @@ inline Try<Nothing> attach(
     const Netlink<struct rtnl_cls>& cls,
     const action::Mirror& mirror)
 {
+  const std::string kind = rtnl_tc_get_kind(TC_CAST(cls.get()));
+
   foreach (const std::string& _link, mirror.links()) {
     Result<Netlink<struct rtnl_link> > link = link::internal::get(_link);
     if (link.isError()) {
@@ -173,7 +184,6 @@ inline Try<Nothing> attach(
     rtnl_mirred_set_action(act, TCA_EGRESS_MIRROR);
     rtnl_mirred_set_policy(act, TC_ACT_PIPE);
 
-    const std::string kind = rtnl_tc_get_kind(TC_CAST(cls.get()));
     if (kind == "basic") {
       err = rtnl_basic_add_action(cls.get(), act);
       if (err != 0) {
@@ -192,6 +202,42 @@ inline Try<Nothing> attach(
     }
   }
 
+  // Automatically set the 'terminal' flag for u32 filters if a mirror
+  // action is attached.
+  if (kind == "u32") {
+    int err = rtnl_u32_set_cls_terminal(cls.get());
+    if (err != 0) {
+      return Error(
+          "Failed to set the terminal flag: " +
+          std::string(nl_geterror(err)));
+    }
+  }
+
+  return Nothing();
+}
+
+
+// Attaches a terminal action to the libnl filter (rtnl_cls). It will
+// stop the packet from being passed to the next filter if a match is
+// found. This is only applied to u32 filters (which can match any
+// 32-bit value in a packet). This function will return error if the
+// user tries to attach a terminal action to a non-u32 filter.
+inline Try<Nothing> attach(
+    const Netlink<struct rtnl_cls>& cls,
+    const action::Terminal& terminal)
+{
+  const std::string kind = rtnl_tc_get_kind(TC_CAST(cls.get()));
+  if (kind != "u32") {
+    return Error("Cannot attach terminal action to a non-u32 filter.");
+  }
+
+  int err = rtnl_u32_set_cls_terminal(cls.get());
+  if (err != 0) {
+    return Error(
+        "Failed to set the terminal flag: " +
+        std::string(nl_geterror(err)));
+  }
+
   return Nothing();
 }
 
@@ -215,6 +261,12 @@ inline Try<Nothing> attach(
     return attach(cls, *mirror);
   }
 
+  const action::Terminal* terminal =
+    dynamic_cast<const action::Terminal*>(action.get());
+  if (terminal != NULL) {
+    return attach(cls, *terminal);
+  }
+
   return Error("Unsupported action type");
 }
 
@@ -248,20 +300,6 @@ Try<Netlink<struct rtnl_cls> > encodeFilter(
     return Error("Failed to encode the classifier " + encoding.error());
   }
 
-  // Stop the packet from being passed to the next filter if a match
-  // is found. This is only applied to u32 filters (which can match
-  // any 32-bit value in a packet), and is not needed for other types
-  // fo fitlers.
-  // TODO(jieyu): Consider setting flowid.
-  if (rtnl_tc_get_kind(TC_CAST(cls.get())) == std::string("u32")) {
-    int err = rtnl_u32_set_cls_terminal(cls.get());
-    if (err != 0) {
-      return Error(
-          "Failed to mark the libnl filter as a terminal" +
-          std::string(nl_geterror(err)));
-    }
-  }
-
   // Attach actions to the libnl filter.
   foreach (const process::Shared<action::Action>& action, filter.actions()) {
     Try<Nothing> attaching = attach(cls, action);

http://git-wip-us.apache.org/repos/asf/mesos/blob/3541b819/src/linux/routing/filter/ip.cpp
----------------------------------------------------------------------
diff --git a/src/linux/routing/filter/ip.cpp b/src/linux/routing/filter/ip.cpp
index 86dbf35..2623deb 100644
--- a/src/linux/routing/filter/ip.cpp
+++ b/src/linux/routing/filter/ip.cpp
@@ -440,6 +440,23 @@ Try<bool> create(
 }
 
 
+Try<bool> create(
+    const string& link,
+    const queueing::Handle& parent,
+    const Classifier& classifier,
+    const Option<Priority>& priority,
+    const action::Terminal& terminal)
+{
+  return internal::create(
+      link,
+      Filter<Classifier>(
+          parent,
+          classifier,
+          priority,
+          terminal));
+}
+
+
 Try<bool> remove(
     const string& link,
     const queueing::Handle& parent,

http://git-wip-us.apache.org/repos/asf/mesos/blob/3541b819/src/linux/routing/filter/ip.hpp
----------------------------------------------------------------------
diff --git a/src/linux/routing/filter/ip.hpp b/src/linux/routing/filter/ip.hpp
index 8f46ee5..52b7af7 100644
--- a/src/linux/routing/filter/ip.hpp
+++ b/src/linux/routing/filter/ip.hpp
@@ -147,6 +147,18 @@ Try<bool> create(
     const action::Redirect& redirect);
 
 
+// Creates an IP packet filter attached to the given parent on the
+// link which will stop the IP packets from being sent to the next
+// filter. Returns false if an IP packet filter attached to the given
+// parent with the same classifier already exists.
+Try<bool> create(
+    const std::string& link,
+    const queueing::Handle& parent,
+    const Classifier& classifier,
+    const Option<Priority>& priority,
+    const action::Terminal& terminal);
+
+
 // Removes the IP packet filter attached to the given parent that
 // matches the specified classifier from the link. Returns false if
 // such a filter is not found.