Apache Mesos
ip.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __LINUX_ROUTING_FILTER_IP_HPP__
18 #define __LINUX_ROUTING_FILTER_IP_HPP__
19 
20 #include <stdint.h>
21 
22 #include <iosfwd>
23 #include <string>
24 #include <vector>
25 
26 #include <boost/functional/hash.hpp>
27 
28 #include <stout/ip.hpp>
29 #include <stout/net.hpp>
30 #include <stout/mac.hpp>
31 #include <stout/option.hpp>
32 #include <stout/result.hpp>
33 #include <stout/try.hpp>
34 
35 #include "linux/routing/handle.hpp"
36 
40 
41 namespace routing {
42 namespace filter {
43 namespace ip {
44 
45 // Represents a port range that can be used by a single u32 matcher.
46 // The port range [begin, end] (both begin and end are inclusive)
47 // should have size = 2^n (n=0,1,2,...) and its begin is size aligned
48 // (i.e., begin % size == 0).
49 class PortRange
50 {
51 public:
52  // Creates a port range from the specified begin and end. Returns
53  // error if it does not meet the above requirements. All values are
54  // in host order.
55  static Try<PortRange> fromBeginEnd(uint16_t begin, uint16_t end);
56 
57  // Creates a port range from the specified begin and mask. Returns
58  // error if it does not meet the above requirements. All values are
59  // in host order.
60  static Try<PortRange> fromBeginMask(uint16_t begin, uint16_t mask);
61 
62  // Returns the begin (in host order) of this port range.
63  uint16_t begin() const { return begin_; }
64 
65  // Returns the end (in host order) of this port range.
66  uint16_t end() const { return end_; }
67 
68  // Returns the mask (in host order) of this port range.
69  uint16_t mask() const { return ~(end_ - begin_); }
70 
71  bool operator==(const PortRange& that) const
72  {
73  return begin_ == that.begin_ && end_ == that.end_;
74  }
75 
76 private:
77  PortRange(uint16_t _begin, uint16_t _end)
78  : begin_(_begin), end_(_end) {}
79 
80  uint16_t begin_; // In host order.
81  uint16_t end_; // In host order.
82 };
83 
84 
85 std::ostream& operator<<(std::ostream& stream, const PortRange& range);
86 
87 
88 struct Classifier
89 {
91  const Option<net::MAC>& _destinationMAC,
92  const Option<net::IP>& _destinationIP,
93  const Option<PortRange>& _sourcePorts,
94  const Option<PortRange>& _destinationPorts)
95  : destinationMAC(_destinationMAC),
96  destinationIP(_destinationIP),
97  sourcePorts(_sourcePorts),
98  destinationPorts(_destinationPorts) {}
99 
100  bool operator==(const Classifier& that) const
101  {
102  return (destinationMAC == that.destinationMAC &&
103  destinationIP == that.destinationIP &&
104  destinationPorts == that.destinationPorts &&
105  sourcePorts == that.sourcePorts);
106  }
107 
109 
110  // TODO(evelinad): Replace net::IP with net::IP::Network when we will
111  // support classifiers for the entire subnet.
113 
116 };
117 
118 
119 // Returns true if an IP packet filter attached to the given parent
120 // that matches the specified classifier exists on the link.
122  const std::string& link,
123  const Handle& parent,
124  const Classifier& classifier);
125 
126 
127 // Creates an IP packet filter attached to the given parent on the
128 // link which will redirect all the IP packets that satisfy the
129 // conditions specified by the classifier to the target link. Returns
130 // false if an IP packet filter attached to the given parent with the
131 // same classifier already exists.
133  const std::string& link,
134  const Handle& parent,
135  const Classifier& classifier,
136  const Option<Priority>& priority,
137  const action::Redirect& redirect);
138 
139 
140 // Same as above, but allow the user to specify the handle. This
141 // interface is exposed only for testing MESOS-1617.
142 // TODO(jieyu): Revisit this once the kernel bug is fixed.
144  const std::string& link,
145  const Handle& parent,
146  const Classifier& classifier,
147  const Option<Priority>& priority,
148  const Option<Handle>& handle,
149  const action::Redirect& redirect);
150 
151 
152 // Creates an IP packet filter attached to the given parent on the
153 // link which will stop the IP packets from being sent to the next
154 // filter. Returns false if an IP packet filter attached to the given
155 // parent with the same classifier already exists.
157  const std::string& link,
158  const Handle& parent,
159  const Classifier& classifier,
160  const Option<Priority>& priority,
161  const action::Terminal& terminal);
162 
163 
164 // Creates an IP packet filter attached to the given parent on the
165 // link which will set the classid for packets and stop the IP packets
166 // from being sent to the next filter. Returns false if an IP packet
167 // filter attached to the given parent with the same classifier
168 // already exists.
170  const std::string& link,
171  const Handle& parent,
172  const Classifier& classifier,
173  const Option<Priority>& priority,
174  const Option<Handle>& classid);
175 
176 
177 // Removes the IP packet filter attached to the given parent that
178 // matches the specified classifier from the link. Returns false if
179 // such a filter is not found.
180 Try<bool> remove(
181  const std::string& link,
182  const Handle& parent,
183  const Classifier& classifier);
184 
185 
186 // Returns all the IP packet filters attached to the given parent on
187 // the link. Returns none if the link or the parent is not found.
189  const std::string& link,
190  const Handle& parent);
191 
192 
193 // Returns the classifiers of all the IP packet filters attached to
194 // the given parent on the link. Returns none if the link or the
195 // parent is not found.
197  const std::string& link,
198  const Handle& parent);
199 
200 } // namespace ip {
201 } // namespace filter {
202 } // namespace routing {
203 
204 namespace std {
205 
206 template <>
207 struct hash<routing::filter::ip::PortRange>
208 {
209  typedef size_t result_type;
210 
212 
213  result_type operator()(const argument_type& range) const
214  {
215  size_t seed = 0;
216  boost::hash_combine(seed, range.begin());
217  boost::hash_combine(seed, range.end());
218  return seed;
219  }
220 };
221 
222 } // namespace std {
223 
224 #endif // __LINUX_ROUTING_FILTER_IP_HPP__
Classifier(const Option< net::MAC > &_destinationMAC, const Option< net::IP > &_destinationIP, const Option< PortRange > &_sourcePorts, const Option< PortRange > &_destinationPorts)
Definition: ip.hpp:90
Definition: check.hpp:33
uint16_t mask() const
Definition: ip.hpp:69
Result< std::vector< Filter< Classifier > > > filters(const std::string &link, const Handle &parent)
Definition: internal.hpp:769
Option< net::IP > destinationIP
Definition: ip.hpp:112
static Try< PortRange > fromBeginEnd(uint16_t begin, uint16_t end)
Definition: handle.hpp:38
Definition: type_utils.hpp:550
Future< Nothing > redirect(int_fd from, Option< int_fd > to, size_t chunk=4096, const std::vector< lambda::function< void(const std::string &)>> &hooks={})
Redirect output from the &#39;from&#39; file descriptor to the &#39;to&#39; file descriptor (or /dev/null if &#39;to&#39; is ...
routing::filter::ip::PortRange argument_type
Definition: ip.hpp:211
Definition: check.hpp:30
Definition: action.hpp:66
Option< net::MAC > destinationMAC
Definition: ip.hpp:108
Definition: action.hpp:41
bool operator==(const Classifier &that) const
Definition: ip.hpp:100
Option< PortRange > sourcePorts
Definition: ip.hpp:114
std::ostream & operator<<(std::ostream &stream, const PortRange &range)
static Try< PortRange > fromBeginMask(uint16_t begin, uint16_t mask)
Definition: ip.hpp:49
bool operator==(const PortRange &that) const
Definition: ip.hpp:71
Try< bool > exists(const std::string &link, const Handle &parent, const Classifier &classifier)
Result< std::vector< Classifier > > classifiers(const std::string &link, const Handle &parent)
Definition: internal.hpp:809
uint16_t end() const
Definition: ip.hpp:66
Option< PortRange > destinationPorts
Definition: ip.hpp:115
size_t result_type
Definition: ip.hpp:209
Definition: diagnosis.hpp:30
Try< uint32_t > classid(const std::string &hierarchy, const std::string &cgroup)
Try< bool > create(const std::string &link, const Handle &parent, const Classifier &classifier, const Option< Priority > &priority, const action::Redirect &redirect)
uint16_t begin() const
Definition: ip.hpp:63
Definition: ip.hpp:88
result_type operator()(const argument_type &range) const
Definition: ip.hpp:213
void filter(Filter *filter)