Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
zone.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Lukasz Pawelczyk <l.pawelczyk@partner.samsung.com>
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
17  */
18 
26 #ifndef SERVER_ZONE_HPP
27 #define SERVER_ZONE_HPP
28 
29 #include "zone-config.hpp"
30 #include "zone-provision.hpp"
31 
32 #include "lxc/zone.hpp"
33 #include "netdev.hpp"
34 
35 #include <mutex>
36 #include <string>
37 #include <memory>
38 #include <thread>
39 
40 
41 namespace vasum {
42 
43 
44 enum class SchedulerLevel {
45  FOREGROUND,
47 };
48 
49 class Zone {
50 
51 public:
53 
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;
71  ~Zone();
72 
73  typedef std::function<void(bool succeeded)> StartAsyncResultCallback;
74 
78  const std::string& getId() const;
79 
83  int getPrivilege() const;
84 
88  void restore();
89 
93  void start();
94 
99  void stop(bool saveState);
100 
106  bool activateVT();
107 
112  void goForeground();
113 
118  void goBackground();
119 
123  void setDetachOnExit();
124 
128  void setDestroyOnExit();
129 
133  bool isRunning();
134 
142  bool isStopped();
143 
150  void suspend();
151 
155  void resume();
156 
160  bool isPaused();
161 
166 
170  int getVT() const;
171 
181  int createFile(const std::string& path, const std::int32_t flags, const std::int32_t mode);
182 
186  std::string declareFile(const int32_t& type,
187  const std::string& path,
188  const int32_t& flags,
189  const int32_t& mode);
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);
201  std::string declareLink(const std::string& source,
202  const std::string& target);
203 
207  std::vector<std::string> getDeclarations() const;
208 
212  void removeDeclaration(const std::string& declarationId);
213 
217  std::string getRootPath() const;
218 
222  void createNetdevVeth(const std::string& zoneDev,
223  const std::string& hostDev);
224 
228  void createNetdevMacvlan(const std::string& zoneDev,
229  const std::string& hostDev,
230  const uint32_t& mode);
231 
235  void moveNetdev(const std::string& devId);
236 
240  void destroyNetdev(const std::string& devId);
241 
245  void setNetdevAttrs(const std::string& netdev, const NetdevAttrs& attrs);
246 
250  NetdevAttrs getNetdevAttrs(const std::string& netdev);
251 
255  std::vector<std::string> getNetdevList();
256 
260  void deleteNetdevIpAddress(const std::string& netdev, const std::string& ip);
261 
265  void setSchedulerLevel(SchedulerLevel sched);
266 
271  std::int64_t getSchedulerQuota();
272 
273 private:
276  std::unique_ptr<ZoneProvision> mProvision;
277  mutable std::recursive_mutex mReconnectMutex;
278  std::string mRunMountPoint;
279  std::string mRootPath;
280  std::string mDbPath;
282  const std::string mId;
285 
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);
290 };
291 
292 
293 } // namespace vasum
294 
295 #endif // SERVER_ZONE_HPP
std::string mRunMountPoint
Definition: zone.hpp:278
Zone & operator=(const Zone &)=delete
Definition: zone.hpp:49
void goForeground()
Setup this zone to be put in the foreground.
Definition: zone.cpp:310
void updateRequestedState(const std::string &state)
Definition: zone.cpp:154
void setNetdevAttrs(const std::string &netdev, const NetdevAttrs &attrs)
Set network device attributes.
Definition: zone.cpp:425
std::function< void(bool succeeded)> StartAsyncResultCallback
Definition: zone.hpp:73
A class wrapping lxc container.
Definition: zone.hpp:42
Definition: zone-config.hpp:38
std::string declareFile(const int32_t &type, const std::string &path, const int32_t &flags, const int32_t &mode)
Declare file, directory or pipe that will be created while zone startup.
Definition: zone.cpp:392
void moveNetdev(const std::string &devId)
Move network device to zone.
Definition: zone.cpp:298
Lxc zone.
Declaration of the class for storing zone configuration.
Definition: zone-config.hpp:98
void suspend()
Suspends an active zone, the process is frozen without further access to CPU resources and I/O...
Definition: zone.cpp:346
std::vector< std::string > getNetdevList()
Get network device list.
Definition: zone.cpp:437
bool activateVT()
Activate this zone's VT.
Definition: zone.cpp:272
void resume()
Resume zone.
Definition: zone.cpp:359
void setDestroyOnExit()
Set if zone should be destroyed on exit.
Definition: zone.cpp:328
std::string declareLink(const std::string &source, const std::string &target)
Declare link that will be created while zone startup.
Definition: zone.cpp:409
void createNetdevVeth(const std::string &zoneDev, const std::string &hostDev)
Create veth network device.
Definition: zone.cpp:283
std::string mRootPath
Definition: zone.hpp:279
Network devices management functions declaration.
void stop(bool saveState)
Try to shutdown the zone, if failed, destroy it.
Definition: zone.cpp:230
bool mDetachOnExit
Definition: zone.hpp:283
std::string getRootPath() const
Get zone root path.
Definition: zone.cpp:267
const std::string mId
Definition: zone.hpp:282
bool isRunning()
Definition: zone.cpp:334
void setSchedulerParams(std::uint64_t cpuShares, std::uint64_t vcpuPeriod, std::int64_t vcpuQuota)
Definition: zone.cpp:481
void setDetachOnExit()
Set if zone should be detached on exit.
Definition: zone.cpp:322
const std::string & getId() const
Get the zone id.
Definition: zone.cpp:138
bool mDestroyOnExit
Definition: zone.hpp:284
bool isSwitchToDefaultAfterTimeoutAllowed() const
Definition: zone.cpp:378
std::int64_t getSchedulerQuota()
Definition: zone.cpp:449
char data[368]
Definition: initctl.cpp:41
void onNameLostCallback()
bool isStopped()
Check if the zone is stopped.
Definition: zone.cpp:340
ZoneDynamicConfig mDynamicConfig
Definition: zone.hpp:275
SchedulerLevel
Definition: zone.hpp:44
std::string declareMount(const std::string &source, const std::string &target, const std::string &type, const int64_t &flags, const std::string &data)
Declare mount that will be created while zone startup.
Definition: zone.cpp:400
lxc::LxcZone mZone
Definition: zone.hpp:281
void saveDynamicConfig()
Definition: zone.cpp:149
void setSchedulerLevel(SchedulerLevel sched)
Sets the zones scheduler CFS quota.
Definition: zone.cpp:459
ZoneConfig mConfig
Definition: zone.hpp:274
void deleteNetdevIpAddress(const std::string &netdev, const std::string &ip)
Remove ipv4/ipv6 address from network device.
Definition: zone.cpp:443
NetdevAttrs getNetdevAttrs(const std::string &netdev)
Get network device attributes.
Definition: zone.cpp:431
int getVT() const
Get id of VT.
Definition: zone.cpp:261
void removeDeclaration(const std::string &declarationId)
Remove declaration.
Definition: zone.cpp:420
void createNetdevMacvlan(const std::string &zoneDev, const std::string &hostDev, const uint32_t &mode)
Create macvlan network device.
Definition: zone.cpp:290
std::unique_ptr< ZoneProvision > mProvision
Definition: zone.hpp:276
int createFile(const std::string &path, const std::int32_t flags, const std::int32_t mode)
Create file inside zone, return its fd.
Definition: zone.cpp:383
std::vector< std::tuple< std::string, std::string > > Attrs
Definition: netdev.hpp:38
void start()
Boot the zone to the background.
Definition: zone.cpp:185
Zone(const std::string &zoneId, const std::string &zonesPath, const std::string &zoneTemplatePath, const std::string &dbPath, const std::string &zoneTemplateDir, const std::string &baseRunMountPointPath)
Zone constructor.
Definition: zone.cpp:64
std::recursive_mutex mReconnectMutex
Definition: zone.hpp:277
netdev::Attrs NetdevAttrs
Definition: zone.hpp:52
int getPrivilege() const
Get the zone privilege.
Definition: zone.cpp:144
void goBackground()
Setup this zone to be put in the background.
Definition: zone.cpp:316
void restore()
Restore zone to the previous state.
Definition: zone.cpp:164
std::vector< std::string > getDeclarations() const
Gets all declarations.
Definition: zone.cpp:415
~Zone()
Definition: zone.cpp:115
std::string mDbPath
Definition: zone.hpp:280
Declaration of the class for managing zone provision.
bool isPaused()
Definition: zone.cpp:372
void destroyNetdev(const std::string &devId)
Destroy network device in zone.
Definition: zone.cpp:304