lxcpp: provisioning implementation (part 2)
[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 #include <boost/regex.hpp>
40
41
42 namespace vasum {
43
44
45 enum class SchedulerLevel {
46     FOREGROUND,
47     BACKGROUND
48 };
49
50 class Zone {
51
52 public:
53     typedef netdev::Attrs NetdevAttrs;
54
55     /**
56      * Zone constructor
57      * @param zoneId zone id
58      * @param zonesPath directory where zones are defined (configs, rootfs etc)
59      * @param zoneTemplatePath path for zones config template
60      * @param dbPath path to dynamic config db file
61      * @param zoneTemplateDir directory where templates are stored
62      * @param baseRunMountPointPath base directory for run mount point
63      */
64     Zone(const std::string& zoneId,
65          const std::string& zonesPath,
66          const std::string& zoneTemplatePath,
67          const std::string& dbPath,
68          const std::string& zoneTemplateDir,
69          const std::string& baseRunMountPointPath);
70     Zone(const Zone&) = delete;
71     Zone& operator=(const Zone&) = delete;
72     ~Zone();
73
74     typedef std::function<void(bool succeeded)> StartAsyncResultCallback;
75
76     /**
77      * Get the zone id
78      */
79     const std::string& getId() const;
80
81     /**
82      * Get the zone privilege
83      */
84     int getPrivilege() const;
85
86     /**
87      * Restore zone to the previous state
88      */
89     void restore();
90
91     /**
92      * Boot the zone to the background.
93      */
94     void start();
95
96     /**
97      * Try to shutdown the zone, if failed, destroy it.
98      * @param saveState save zone's state
99      */
100     void stop(bool saveState);
101
102     /**
103      * Activate this zone's VT
104      *
105      * @return Was activation successful?
106      */
107     bool activateVT();
108
109     /**
110      * Setup this zone to be put in the foreground.
111      * I.e. set appropriate scheduler level.
112      */
113     void goForeground();
114
115     /**
116      * Setup this zone to be put in the background.
117      * I.e. set appropriate scheduler level.
118      */
119     void goBackground();
120
121     /**
122      * Set if zone should be detached on exit.
123      */
124     void setDetachOnExit();
125
126     /**
127      * Set if zone should be destroyed on exit.
128      */
129     void setDestroyOnExit();
130
131     /**
132      * @return Is the zone running?
133      */
134     bool isRunning();
135
136     /**
137      * Check if the zone is stopped. It's NOT equivalent to !isRunning,
138      * because it checks different internal zone states. There are other states,
139      * (e.g. paused) when the zone isn't running nor stopped.
140      *
141      * @return Is the zone stopped?
142      */
143     bool isStopped();
144
145     /**
146      * Suspends an active zone, the process is frozen
147      * without further access to CPU resources and I/O,
148      * but the memory used by the zone
149      * at the hypervisor level will stay allocated
150      */
151     void suspend();
152
153     /**
154      * Resume zone.
155      */
156     void resume();
157
158     /**
159      * @return Is the zone in a paused state?
160      */
161     bool isPaused();
162
163     /**
164      * @return Is switching to default zone after timeout allowed?
165      */
166     bool isSwitchToDefaultAfterTimeoutAllowed() const;
167
168     /**
169      * Get id of VT
170      */
171     int getVT() const;
172
173     /**
174      * Create file inside zone, return its fd
175      *
176      * @param path  Path where the file should be created
177      * @param flags Flags used when opening the file. See common/lxc/zone.hpp for more info.
178      * @param mode  Permissions with which file is created
179      *
180      * @return Created files fd
181      */
182     int createFile(const std::string& path, const std::int32_t flags, const std::int32_t mode);
183
184     /**
185      * Declare file, directory or pipe that will be created while zone startup
186      */
187     std::string declareFile(const int32_t& type,
188                             const std::string& path,
189                             const int32_t& flags,
190                             const int32_t& mode);
191     /**
192      * Declare mount that will be created while zone startup
193      */
194     std::string declareMount(const std::string& source,
195                              const std::string& target,
196                              const std::string& type,
197                              const int64_t& flags,
198                              const std::string& data);
199     /**
200      * Declare link that will be created while zone startup
201      */
202     std::string declareLink(const std::string& source,
203                             const std::string& target);
204
205     /**
206      * Gets all declarations
207      */
208     std::vector<std::string> getDeclarations() const;
209
210     /**
211      * Remove declaration
212      */
213     void removeDeclaration(const std::string& declarationId);
214
215     /**
216      * Get zone root path
217      */
218     std::string getRootPath() const;
219
220     /**
221      * Create veth network device
222      */
223     void createNetdevVeth(const std::string& zoneDev,
224                           const std::string& hostDev);
225
226     /**
227      * Create macvlan network device
228      */
229     void createNetdevMacvlan(const std::string& zoneDev,
230                              const std::string& hostDev,
231                              const uint32_t& mode);
232
233     /**
234      * Move network device to zone
235      */
236     void moveNetdev(const std::string& devId);
237
238     /**
239      * Destroy network device in zone
240      */
241     void destroyNetdev(const std::string& devId);
242
243     /**
244      * Set network device attributes
245      */
246     void setNetdevAttrs(const std::string& netdev, const NetdevAttrs& attrs);
247
248     /**
249      * Get network device attributes
250      */
251     NetdevAttrs getNetdevAttrs(const std::string& netdev);
252
253     /**
254      * Get network device list
255      */
256     std::vector<std::string> getNetdevList();
257
258     /**
259      * Remove ipv4/ipv6 address from network device
260      */
261     void deleteNetdevIpAddress(const std::string& netdev, const std::string& ip);
262
263     /**
264      * Sets the zones scheduler CFS quota.
265      */
266     void setSchedulerLevel(SchedulerLevel sched);
267
268     /**
269      * @return Scheduler CFS quota,
270      * TODO: this function is only for UNIT TESTS
271      */
272     std::int64_t getSchedulerQuota();
273
274 private:
275     ZoneConfig mConfig;
276     ZoneDynamicConfig mDynamicConfig;
277     std::unique_ptr<ZoneProvision> mProvision;
278     mutable std::recursive_mutex mReconnectMutex;
279     std::string mRunMountPoint;
280     std::string mRootPath;
281     std::string mDbPath;
282     lxc::LxcZone mZone;
283     const std::string mId;
284     bool mDetachOnExit;
285     bool mDestroyOnExit;
286
287     void onNameLostCallback();
288     void saveDynamicConfig();
289     void updateRequestedState(const std::string& state);
290     void setSchedulerParams(std::uint64_t cpuShares, std::uint64_t vcpuPeriod, std::int64_t vcpuQuota);
291 };
292
293
294 } // namespace vasum
295
296 #endif // SERVER_ZONE_HPP