Apache Mesos
net_cls.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 __CGROUPS_ISOLATOR_SUBSYSTEMS_NET_CLS_HPP__
18 #define __CGROUPS_ISOLATOR_SUBSYSTEMS_NET_CLS_HPP__
19 
20 #include <bitset>
21 #include <ostream>
22 #include <string>
23 
24 #include <mesos/resources.hpp>
25 
26 #include <process/future.hpp>
27 #include <process/owned.hpp>
28 
29 #include <stout/error.hpp>
30 #include <stout/hashmap.hpp>
31 #include <stout/interval.hpp>
32 #include <stout/none.hpp>
33 #include <stout/nothing.hpp>
34 #include <stout/option.hpp>
35 #include <stout/result.hpp>
36 #include <stout/try.hpp>
37 
38 #include "slave/flags.hpp"
39 
42 
43 namespace mesos {
44 namespace internal {
45 namespace slave {
46 
47 // This defines the net_cls handle. The handle is composed of two
48 // parts, a 16-bit primary handle and a 16-bit secondary handle.
49 //
50 // TODO(asridharan): Currently we need to define the net_cls handle
51 // here, since we cannot use the definitions in
52 // `src/linux/routing/handle.hpp` due to its dependency on `libnl`,
53 // which is under GPL. Once we have been able to resolve these issues
54 // we should remove this definition and use the definition presented
55 // in `src/linux/routing/handle.hpp`.
57 {
58  NetClsHandle(uint16_t _primary, uint16_t _secondary)
59  : primary(_primary), secondary(_secondary) {};
60 
61  explicit NetClsHandle(uint32_t handle)
62  {
63  primary = handle >> 16;
64  secondary = handle & 0xffff;
65  };
66 
67  // Get the 32-bit representation of the handle in the form of
68  // 0xAAAABBBB. Where 0xAAAA is the primary handle and 0xBBBB is the
69  // secondary handle.
70  uint32_t get() const
71  {
72  uint32_t handle = primary;
73 
74  handle <<= 16;
75  handle |= secondary;
76 
77  return handle;
78  };
79 
80  uint16_t primary;
81  uint16_t secondary;
82 };
83 
84 
85 std::ostream& operator<<(std::ostream& stream, const NetClsHandle& obj);
86 
87 
88 // This manages the net_cls handles for the `cgroup/net_cls` isolator.
89 // The isolator can use this with a range of primary handles, which
90 // will be managed by this class. For each primary handle there are
91 // 64K possible secondary handles. For a given primary handle the
92 // isolator can get a secondary handle by calling `alloc` and release
93 // an allocated handle by calling `free` on the secondary handle. For
94 // a given primary handle, the isolator can also explicitly reserve a
95 // secondary handle by calling `reserve`.
97 {
98 public:
100  const IntervalSet<uint32_t>& _primaries,
101  const IntervalSet<uint32_t>& _secondaries = IntervalSet<uint32_t>());
102 
104 
105  // Allocates a primary handle from the given interval set.
106  Try<uint16_t> allocPrimary() { return Error("Not Implemented"); }
108 
109  Try<Nothing> reserve(const NetClsHandle& handle);
110  Try<Nothing> free(const NetClsHandle& handle);
111 
112  // Check if a handle is used.
113  Try<bool> isUsed(const NetClsHandle& handle);
114 
115 private:
116  // The key to this hashmap is the 16-bit primary handle.
118 
119  // NOTE: Though the primary and secondary handles are 16 bit, we
120  // cannot use an `IntervalSet` specialization of type `uint16_t`
121  // since the intervals are stored in right openf format -- [x,y) --
122  // and setting the type to `uint16_t` would lead to overflow errors.
123  // For e.g., we would not be able to store the range [0xffff,0xffff]
124  // in `IntervalSet<uint16_t>` due to overflow error.
125  IntervalSet<uint32_t> primaries;
126  IntervalSet<uint32_t> secondaries;
127 };
128 
129 
134 {
135 public:
137  const Flags& flags,
138  const std::string& hierarchy);
139 
140  ~NetClsSubsystemProcess() override = default;
141 
142  std::string name() const override
143  {
145  }
146 
148  const ContainerID& containerId,
149  const std::string& cgroup) override;
150 
152  const ContainerID& containerId,
153  const std::string& cgroup) override;
154 
156  const ContainerID& containerId,
157  const std::string& cgroup,
158  pid_t pid) override;
159 
161  const ContainerID& containerId,
162  const std::string& cgroup) override;
163 
165  const ContainerID& containerId,
166  const std::string& cgroup) override;
167 
168 private:
170  const Flags& flags,
171  const std::string& hierarchy,
172  const IntervalSet<uint32_t>& primaries,
173  const IntervalSet<uint32_t>& secondaries);
174 
175  struct Info
176  {
177  Info() {}
178 
179  Info(const NetClsHandle &_handle)
180  : handle(_handle) {}
181 
182  const Option<NetClsHandle> handle;
183  };
184 
185  Result<NetClsHandle> recoverHandle(
186  const std::string& hierarchy,
187  const std::string& cgroup);
188 
189  Option<NetClsHandleManager> handleManager;
190 
191  // Stores cgroups associated information for container.
193 };
194 
195 } // namespace slave {
196 } // namespace internal {
197 } // namespace mesos {
198 
199 #endif // __CGROUPS_ISOLATOR_SUBSYSTEMS_NET_CLS_HPP__
~NetClsHandleManager()
Definition: net_cls.hpp:103
Protocol< RecoverRequest, RecoverResponse > recover
Try< Nothing > isolate(const std::string &hierarchy, const std::string &cgroup, pid_t pid)
NetClsHandle(uint16_t _primary, uint16_t _secondary)
Definition: net_cls.hpp:58
Definition: errorbase.hpp:36
std::ostream & operator<<(std::ostream &stream, const MesosContainerizerProcess::State &state)
Definition: check.hpp:33
const std::string CGROUP_SUBSYSTEM_NET_CLS_NAME
Definition: constants.hpp:49
process::Future< bool > cleanup(const std::string &hierarchy)
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
Definition: flags.hpp:39
Definition: check.hpp:30
Definition: hashmap.hpp:38
NetClsHandle(uint32_t handle)
Definition: net_cls.hpp:61
DWORD pid_t
Definition: windows.hpp:181
Try< uint16_t > allocPrimary()
Definition: net_cls.hpp:106
Try< std::vector< Info > > infos(int familiy, int states)
Definition: net_cls.hpp:56
std::string name() const override
Definition: net_cls.hpp:142
Definition: spec.hpp:30
Result< std::string > cgroup(pid_t pid)
uint16_t secondary
Definition: net_cls.hpp:81
Definition: subsystem.hpp:186
Represent cgroups net_cls subsystem.
Definition: net_cls.hpp:133
Definition: none.hpp:27
Definition: attributes.hpp:24
uint16_t primary
Definition: net_cls.hpp:78
Try< std::string > prepare(const std::string &baseHierarchy, const std::string &subsystem, const std::string &cgroup)
Try< Nothing > create(const std::string &hierarchy, const std::string &cgroup, bool recursive=false)
Result< std::string > hierarchy(const std::string &subsystems)
Definition: parse.hpp:33