2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
22 * @brief Declaration of the class for managing one zone
26 #ifndef SERVER_ZONE_HPP
27 #define SERVER_ZONE_HPP
29 #include "zone-config.hpp"
30 #include "zone-provision.hpp"
32 #include "lxc/zone.hpp"
44 enum class SchedulerLevel {
52 typedef netdev::Attrs NetdevAttrs;
56 * @param zoneId zone id
57 * @param zonesPath directory where zones are defined (configs, rootfs etc)
58 * @param zoneTemplatePath path for zones config template
59 * @param dbPath path to dynamic config db file
60 * @param zoneTemplateDir directory where templates are stored
61 * @param baseRunMountPointPath base directory for run mount point
63 Zone(const std::string& zoneId,
64 const std::string& zonesPath,
65 const std::string& zoneTemplatePath,
66 const std::string& dbPath,
67 const std::string& zoneTemplateDir,
68 const std::string& baseRunMountPointPath);
69 Zone(const Zone&) = delete;
70 Zone& operator=(const Zone&) = delete;
73 typedef std::function<void(bool succeeded)> StartAsyncResultCallback;
78 const std::string& getId() const;
81 * Get the zone privilege
83 int getPrivilege() const;
86 * Restore zone to the previous state
91 * Boot the zone to the background.
96 * Try to shutdown the zone, if failed, destroy it.
97 * @param saveState save zone's state
99 void stop(bool saveState);
102 * Activate this zone's VT
104 * @return Was activation successful?
109 * Setup this zone to be put in the foreground.
110 * I.e. set appropriate scheduler level.
115 * Setup this zone to be put in the background.
116 * I.e. set appropriate scheduler level.
121 * Set if zone should be detached on exit.
123 void setDetachOnExit();
126 * Set if zone should be destroyed on exit.
128 void setDestroyOnExit();
131 * @return Is the zone running?
136 * Check if the zone is stopped. It's NOT equivalent to !isRunning,
137 * because it checks different internal zone states. There are other states,
138 * (e.g. paused) when the zone isn't running nor stopped.
140 * @return Is the zone stopped?
145 * Suspends an active zone, the process is frozen
146 * without further access to CPU resources and I/O,
147 * but the memory used by the zone
148 * at the hypervisor level will stay allocated
158 * @return Is the zone in a paused state?
163 * @return Is switching to default zone after timeout allowed?
165 bool isSwitchToDefaultAfterTimeoutAllowed() const;
173 * Create file inside zone, return its fd
175 * @param path Path where the file should be created
176 * @param flags Flags used when opening the file. See common/lxc/zone.hpp for more info.
177 * @param mode Permissions with which file is created
179 * @return Created files fd
181 int createFile(const std::string& path, const std::int32_t flags, const std::int32_t mode);
184 * Declare file, directory or pipe that will be created while zone startup
186 std::string declareFile(const int32_t& type,
187 const std::string& path,
188 const int32_t& flags,
189 const int32_t& mode);
191 * Declare mount that will be created while zone startup
193 std::string declareMount(const std::string& source,
194 const std::string& target,
195 const std::string& type,
196 const int64_t& flags,
197 const std::string& data);
199 * Declare link that will be created while zone startup
201 std::string declareLink(const std::string& source,
202 const std::string& target);
205 * Gets all declarations
207 std::vector<std::string> getDeclarations() const;
212 void removeDeclaration(const std::string& declarationId);
217 std::string getRootPath() const;
220 * Create veth network device
222 void createNetdevVeth(const std::string& zoneDev,
223 const std::string& hostDev);
226 * Create macvlan network device
228 void createNetdevMacvlan(const std::string& zoneDev,
229 const std::string& hostDev,
230 const uint32_t& mode);
233 * Move network device to zone
235 void moveNetdev(const std::string& devId);
238 * Destroy network device in zone
240 void destroyNetdev(const std::string& devId);
243 * Set network device attributes
245 void setNetdevAttrs(const std::string& netdev, const NetdevAttrs& attrs);
248 * Get network device attributes
250 NetdevAttrs getNetdevAttrs(const std::string& netdev);
253 * Get network device list
255 std::vector<std::string> getNetdevList();
258 * Remove ipv4/ipv6 address from network device
260 void deleteNetdevIpAddress(const std::string& netdev, const std::string& ip);
263 * Sets the zones scheduler CFS quota.
265 void setSchedulerLevel(SchedulerLevel sched);
268 * @return Scheduler CFS quota,
269 * TODO: this function is only for UNIT TESTS
271 std::int64_t getSchedulerQuota();
275 ZoneDynamicConfig mDynamicConfig;
276 std::unique_ptr<ZoneProvision> mProvision;
277 mutable std::recursive_mutex mReconnectMutex;
278 std::string mRunMountPoint;
279 std::string mRootPath;
282 const std::string mId;
286 void onNameLostCallback();
287 void saveDynamicConfig();
288 void updateRequestedState(const std::string& state);
289 void setSchedulerParams(std::uint64_t cpuShares, std::uint64_t vcpuPeriod, std::int64_t vcpuQuota);
295 #endif // SERVER_ZONE_HPP