lxcpp: fix cgroup unit tests
[platform/core/security/vasum.git] / server / zone.hpp
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
19 /**
20  * @file
21  * @author  Lukasz Pawelczyk (l.pawelczyk@partner.samsung.com)
22  * @brief   Declaration of the class for managing one zone
23  */
24
25
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,
46     BACKGROUND
47 };
48
49 class Zone {
50
51 public:
52     typedef netdev::Attrs NetdevAttrs;
53
54     /**
55      * Zone constructor
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
62      */
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
75     /**
76      * Get the zone id
77      */
78     const std::string& getId() const;
79
80     /**
81      * Get the zone privilege
82      */
83     int getPrivilege() const;
84
85     /**
86      * Restore zone to the previous state
87      */
88     void restore();
89
90     /**
91      * Boot the zone to the background.
92      */
93     void start();
94
95     /**
96      * Try to shutdown the zone, if failed, destroy it.
97      * @param saveState save zone's state
98      */
99     void stop(bool saveState);
100
101     /**
102      * Activate this zone's VT
103      *
104      * @return Was activation successful?
105      */
106     bool activateVT();
107
108     /**
109      * Setup this zone to be put in the foreground.
110      * I.e. set appropriate scheduler level.
111      */
112     void goForeground();
113
114     /**
115      * Setup this zone to be put in the background.
116      * I.e. set appropriate scheduler level.
117      */
118     void goBackground();
119
120     /**
121      * Set if zone should be detached on exit.
122      */
123     void setDetachOnExit();
124
125     /**
126      * Set if zone should be destroyed on exit.
127      */
128     void setDestroyOnExit();
129
130     /**
131      * @return Is the zone running?
132      */
133     bool isRunning();
134
135     /**
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.
139      *
140      * @return Is the zone stopped?
141      */
142     bool isStopped();
143
144     /**
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
149      */
150     void suspend();
151
152     /**
153      * Resume zone.
154      */
155     void resume();
156
157     /**
158      * @return Is the zone in a paused state?
159      */
160     bool isPaused();
161
162     /**
163      * @return Is switching to default zone after timeout allowed?
164      */
165     bool isSwitchToDefaultAfterTimeoutAllowed() const;
166
167     /**
168      * Get id of VT
169      */
170     int getVT() const;
171
172     /**
173      * Create file inside zone, return its fd
174      *
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
178      *
179      * @return Created files fd
180      */
181     int createFile(const std::string& path, const std::int32_t flags, const std::int32_t mode);
182
183     /**
184      * Declare file, directory or pipe that will be created while zone startup
185      */
186     std::string declareFile(const int32_t& type,
187                             const std::string& path,
188                             const int32_t& flags,
189                             const int32_t& mode);
190     /**
191      * Declare mount that will be created while zone startup
192      */
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);
198     /**
199      * Declare link that will be created while zone startup
200      */
201     std::string declareLink(const std::string& source,
202                             const std::string& target);
203
204     /**
205      * Gets all declarations
206      */
207     std::vector<std::string> getDeclarations() const;
208
209     /**
210      * Remove declaration
211      */
212     void removeDeclaration(const std::string& declarationId);
213
214     /**
215      * Get zone root path
216      */
217     std::string getRootPath() const;
218
219     /**
220      * Create veth network device
221      */
222     void createNetdevVeth(const std::string& zoneDev,
223                           const std::string& hostDev);
224
225     /**
226      * Create macvlan network device
227      */
228     void createNetdevMacvlan(const std::string& zoneDev,
229                              const std::string& hostDev,
230                              const uint32_t& mode);
231
232     /**
233      * Move network device to zone
234      */
235     void moveNetdev(const std::string& devId);
236
237     /**
238      * Destroy network device in zone
239      */
240     void destroyNetdev(const std::string& devId);
241
242     /**
243      * Set network device attributes
244      */
245     void setNetdevAttrs(const std::string& netdev, const NetdevAttrs& attrs);
246
247     /**
248      * Get network device attributes
249      */
250     NetdevAttrs getNetdevAttrs(const std::string& netdev);
251
252     /**
253      * Get network device list
254      */
255     std::vector<std::string> getNetdevList();
256
257     /**
258      * Remove ipv4/ipv6 address from network device
259      */
260     void deleteNetdevIpAddress(const std::string& netdev, const std::string& ip);
261
262     /**
263      * Sets the zones scheduler CFS quota.
264      */
265     void setSchedulerLevel(SchedulerLevel sched);
266
267     /**
268      * @return Scheduler CFS quota,
269      * TODO: this function is only for UNIT TESTS
270      */
271     std::int64_t getSchedulerQuota();
272
273 private:
274     ZoneConfig mConfig;
275     ZoneDynamicConfig mDynamicConfig;
276     std::unique_ptr<ZoneProvision> mProvision;
277     mutable std::recursive_mutex mReconnectMutex;
278     std::string mRunMountPoint;
279     std::string mRootPath;
280     std::string mDbPath;
281     lxc::LxcZone mZone;
282     const std::string mId;
283     bool mDetachOnExit;
284     bool mDestroyOnExit;
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