2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@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 Jan Olszak (j.olszak@samsung.com)
22 * @brief Implementation of class for administrating one zone
27 #include "zone-admin.hpp"
28 #include "exception.hpp"
31 #include "logger/logger.hpp"
32 #include "utils/paths.hpp"
33 #include "utils/c-array.hpp"
34 #include "lxc/cgroup.hpp"
44 // TODO: this should be in zone's configuration file
45 const int SHUTDOWN_WAIT = 10;
49 const std::uint64_t DEFAULT_CPU_SHARES = 1024;
50 const std::uint64_t DEFAULT_VCPU_PERIOD_MS = 100000;
52 ZoneAdmin::ZoneAdmin(const std::string& zoneId,
53 const std::string& zonesPath,
54 const std::string& lxcTemplatePrefix,
55 const ZoneConfig& config,
56 const ZoneDynamicConfig& dynamicConfig)
58 mZone(zonesPath, zoneId),
63 LOGD(mId << ": Instantiating ZoneAdmin object");
65 if (!mZone.isDefined()) {
67 const std::string lxcTemplate = utils::getAbsolutePath(config.lxcTemplate,
69 LOGI(mId << ": Creating zone from template: " << lxcTemplate);
70 utils::CStringArrayBuilder args;
71 if (!dynamicConfig.ipv4Gateway.empty()) {
72 args.add("--ipv4-gateway");
73 args.add(dynamicConfig.ipv4Gateway.c_str());
75 if (!dynamicConfig.ipv4.empty()) {
77 args.add(dynamicConfig.ipv4.c_str());
79 const std::string vt = std::to_string(dynamicConfig.vt);
80 if (dynamicConfig.vt > 0) {
84 if (!mZone.create(lxcTemplate, args.c_array())) {
85 throw ZoneOperationException("Could not create zone");
91 ZoneAdmin::~ZoneAdmin()
93 LOGD(mId << ": Destroying ZoneAdmin object...");
97 LOGE(mId << ": Failed to stop the zone");
99 if (!mZone.destroy()) {
100 LOGE(mId << ": Failed to destroy the zone");
104 if (!mDetachOnExit) {
105 // Try to forcefully stop
107 LOGE(mId << ": Failed to stop the zone");
111 LOGD(mId << ": ZoneAdmin object destroyed");
115 const std::string& ZoneAdmin::getId() const
121 void ZoneAdmin::start()
123 LOGD(mId << ": Starting...");
125 LOGD(mId << ": Already running - nothing to do...");
129 utils::CStringArrayBuilder args;
130 for (const std::string& arg : mConfig.initWithArgs) {
131 args.add(arg.c_str());
134 args.add("/sbin/init");
137 if (!mZone.start(args.c_array())) {
138 throw ZoneOperationException("Could not start zone");
141 LOGD(mId << ": Started");
145 void ZoneAdmin::stop()
147 LOGD(mId << ": Stopping procedure started...");
149 LOGD(mId << ": Already crashed/down/off - nothing to do");
153 if (!mZone.shutdown(SHUTDOWN_WAIT)) {
156 throw ZoneOperationException("Could not stop zone");
160 LOGD(mId << ": Stopping procedure ended");
164 void ZoneAdmin::destroy()
166 LOGD(mId << ": Destroying procedure started...");
168 if (!mZone.destroy()) {
169 throw ZoneOperationException("Could not destroy zone");
172 LOGD(mId << ": Destroying procedure ended");
176 bool ZoneAdmin::isRunning()
178 return mZone.getState() == lxc::LxcZone::State::RUNNING;
182 bool ZoneAdmin::isStopped()
184 return mZone.getState() == lxc::LxcZone::State::STOPPED;
188 void ZoneAdmin::suspend()
190 LOGD(mId << ": Pausing...");
191 if (!mZone.freeze()) {
192 throw ZoneOperationException("Could not pause zone");
194 LOGD(mId << ": Paused");
198 void ZoneAdmin::resume()
200 LOGD(mId << ": Resuming...");
201 if (!mZone.unfreeze()) {
202 throw ZoneOperationException("Could not resume zone");
204 LOGD(mId << ": Resumed");
208 bool ZoneAdmin::isPaused()
210 return mZone.getState() == lxc::LxcZone::State::FROZEN;
214 void ZoneAdmin::setSchedulerLevel(SchedulerLevel sched)
219 case SchedulerLevel::FOREGROUND:
220 LOGD(mId << ": Setting SchedulerLevel::FOREGROUND");
221 setSchedulerParams(DEFAULT_CPU_SHARES,
222 DEFAULT_VCPU_PERIOD_MS,
223 mConfig.cpuQuotaForeground);
225 case SchedulerLevel::BACKGROUND:
226 LOGD(mId << ": Setting SchedulerLevel::BACKGROUND");
227 setSchedulerParams(DEFAULT_CPU_SHARES,
228 DEFAULT_VCPU_PERIOD_MS,
229 mConfig.cpuQuotaBackground);
232 assert(0 && "Unknown sched parameter value");
237 void ZoneAdmin::setSchedulerParams(std::uint64_t cpuShares,
238 std::uint64_t vcpuPeriod,
239 std::int64_t vcpuQuota)
241 assert(vcpuPeriod >= 1000 && vcpuPeriod <= 1000000);
242 assert(vcpuQuota == -1 ||
243 (vcpuQuota >= 1000 && vcpuQuota <= static_cast<std::int64_t>(ULLONG_MAX / 1000)));
245 if (!lxc::setCgroup(mId, "cpu", "cpu.shares", std::to_string(cpuShares)) ||
246 !lxc::setCgroup(mId, "cpu", "cpu.cfs_period_us", std::to_string(vcpuPeriod)) ||
247 !lxc::setCgroup(mId, "cpu", "cpu.cfs_quota_us", std::to_string(vcpuQuota))) {
249 LOGE(mId << ": Error while setting the zone's scheduler params");
250 throw ZoneOperationException("Could not set scheduler params");
254 void ZoneAdmin::setDetachOnExit()
256 mDetachOnExit = true;
259 void ZoneAdmin::setDestroyOnExit()
261 mDestroyOnExit = true;
264 std::int64_t ZoneAdmin::getSchedulerQuota()
267 if (!lxc::getCgroup(mId, "cpu", "cpu.cfs_quota_us", ret)) {
268 LOGE(mId << ": Error while getting the zone's scheduler quota param");
269 throw ZoneOperationException("Could not get scheduler quota param");
271 return std::stoll(ret);
274 void ZoneAdmin::createNetdevVeth(const std::string& zoneDev,
275 const std::string& hostDev)
277 netdev::createVeth(mZone.getInitPid(), zoneDev, hostDev);
280 void ZoneAdmin::createNetdevMacvlan(const std::string& zoneDev,
281 const std::string& hostDev,
282 const uint32_t& mode)
284 netdev::createMacvlan(mZone.getInitPid(), zoneDev, hostDev, static_cast<macvlan_mode>(mode));
287 void ZoneAdmin::moveNetdev(const std::string& devId)
289 netdev::movePhys(mZone.getInitPid(), devId);
292 void ZoneAdmin::setNetdevAttrs(const std::string& /* netdev */, const NetdevAttrs& /* attrs */)
294 throw ZoneOperationException("Not implemented");
297 ZoneAdmin::NetdevAttrs ZoneAdmin::getNetdevAttrs(const std::string& /* netdev */)
299 throw ZoneOperationException("Not implemented");
302 std::vector<std::string> ZoneAdmin::getNetdevList()
304 throw ZoneOperationException("Not implemented");