1 /* Copyright 2005 Princeton University
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above
11 copyright notice, this list of conditions and the following
12 disclaimer in the documentation and/or other materials provided
13 with the distribution.
15 * Neither the name of the copyright holder nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PRINCETON
23 UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29 WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
45 #include <sys/resource.h>
51 #include "planetlab.h"
53 #ifndef VC_NXC_RAW_SOCKET
54 # define VC_NXC_RAW_SOCKET 0x00000200ull
56 #ifndef VC_NXC_RAW_SEND
57 # define VC_NXC_RAW_SEND 0x00000400ull
59 #ifndef VC_NXF_LBACK_ALLOW
60 # define VC_NXF_LBACK_ALLOW 0x00000400ull
64 create_context(xid_t ctx, uint64_t bcaps)
66 struct vc_ctx_caps vc_caps;
67 struct vc_net_flags vc_nf;
68 struct vc_net_caps vc_ncaps;
69 uint32_t unshare_mask;
71 /* Create network context */
72 if (vc_net_create(ctx) == VC_NOCTX) {
78 /* Make the network context persistent */
79 vc_nf.mask = vc_nf.flagword = VC_NXF_PERSISTENT | VC_NXF_LBACK_ALLOW;
80 if (vc_set_nflags(ctx, &vc_nf))
83 /* Give it raw sockets capabilities */
84 vc_ncaps.ncaps = vc_ncaps.cmask = VC_NXC_RAW_ICMP | VC_NXC_RAW_SOCKET;
85 if (vc_set_ncaps(ctx, &vc_ncaps))
89 /* Create tag context */
90 if (vc_tag_create(ctx) == VC_NOCTX)
96 * Create context info - this sets the STATE_SETUP and STATE_INIT flags.
98 if (vc_ctx_create(ctx, 0) == VC_NOCTX)
101 /* Unshare the net namespace if the slice if requested in the local slice configuration */
102 unshare_mask = get_space_flag(ctx);
103 if (unshare_mask != 0) {
104 sys_unshare(unshare_mask);
105 vc_set_namespace(ctx, unshare_mask);
108 /* Set capabilities - these don't take effect until SETUP flag is unset */
109 vc_caps.bcaps = bcaps;
110 vc_caps.bmask = ~0ULL; /* currently unused */
111 vc_caps.ccaps = 0; /* don't want any of these */
112 vc_caps.cmask = ~0ULL;
113 if (vc_set_ccaps(ctx, &vc_caps))
116 if (pl_setsched(ctx, 0, 1) < 0) {
117 PERROR("pl_setsched(%u)", ctx);
125 pl_setup_done(xid_t ctx)
127 struct vc_ctx_flags vc_flags;
129 /* unset SETUP flag - this allows other processes to migrate */
130 /* set the PERSISTENT flag - so the context doesn't vanish */
131 /* Don't clear the STATE_INIT flag, as that would make us the init task. */
132 vc_flags.mask = VC_VXF_STATE_SETUP|VC_VXF_PERSISTENT;
133 vc_flags.flagword = VC_VXF_PERSISTENT;
134 if (vc_set_cflags(ctx, &vc_flags))
140 #define RETRY_LIMIT 10
143 pl_chcontext(xid_t ctx, uint64_t bcaps, const struct sliver_resources *slr)
146 int net_migrated = 0;
148 if (pl_set_ulimits(slr) != 0)
153 struct vc_ctx_flags vc_flags;
155 if (vc_get_cflags(ctx, &vc_flags))
160 /* context doesn't exist - create it */
161 if (create_context(ctx, bcaps))
164 /* another process beat us in a race */
167 /* another process is creating - poll the SETUP flag */
172 /* created context and migrated to it i.e., we're done */
176 /* check the SETUP flag */
177 if (vc_flags.flagword & VC_VXF_STATE_SETUP)
179 /* context is still being setup - wait a while then retry */
180 if (retry_count++ >= RETRY_LIMIT)
189 /* context has been setup */
191 if (net_migrated || !vc_net_migrate(ctx))
193 /* Unshare the net namespace if the slice if requested in the local slice configuration */
194 unshare_mask = get_space_flag(ctx);
195 if (unshare_mask != 0) {
196 vc_enter_namespace(ctx, unshare_mask);
199 if (!vc_tag_migrate(ctx) && !vc_ctx_migrate(ctx, 0))
204 /* context disappeared - retry */
210 /* it's okay for a syscall to fail because the context doesn't exist */
211 #define VC_SYSCALL(x) \
215 return errno == ESRCH ? 0 : -1; \
220 pl_setsched(xid_t ctx, uint32_t cpu_min, uint32_t cpu_share)
222 struct vc_set_sched vc_sched;
223 struct vc_ctx_flags vc_flags;
225 vc_sched.set_mask = (VC_VXSM_FILL_RATE | VC_VXSM_INTERVAL | VC_VXSM_TOKENS |
226 VC_VXSM_TOKENS_MIN | VC_VXSM_TOKENS_MAX | VC_VXSM_MSEC |
227 VC_VXSM_FILL_RATE2 | VC_VXSM_INTERVAL2 | VC_VXSM_FORCE);
228 vc_sched.fill_rate = cpu_min; /* percent reserved */
229 vc_sched.interval = 100;
230 vc_sched.fill_rate2 = cpu_share; /* best-effort fair share of unreserved */
231 vc_sched.interval2 = 1000; /* milliseconds */
232 vc_sched.tokens = 100; /* initial allocation of tokens */
233 vc_sched.tokens_min = 50; /* need this many tokens to run */
234 vc_sched.tokens_max = 100; /* max accumulated number of tokens */
237 if (cpu_share == (uint32_t)VC_LIM_KEEP)
238 vc_sched.set_mask &= ~(VC_VXSM_FILL_RATE|VC_VXSM_FILL_RATE2);
240 vc_sched.set_mask |= VC_VXSM_IDLE_TIME;
243 VC_SYSCALL(vc_set_sched(ctx, &vc_sched));
245 vc_flags.mask = VC_VXF_SCHED_FLAGS;
246 vc_flags.flagword = VC_VXF_SCHED_HARD;
247 VC_SYSCALL(vc_set_cflags(ctx, &vc_flags));
257 struct pl_resources {
261 unsigned long long *limit;
262 unsigned long int *personality;
266 #define WHITESPACE(buffer,index,len) \
267 while(isspace((int)buffer[index])) \
268 if (index < len) index++; else goto out;
270 #define VSERVERCONF "/etc/vservers/"
273 pl_get_limits(const char *context, struct sliver_resources *slr)
277 size_t len = strlen(VSERVERCONF) + strlen(context) + NULLBYTE_SIZE;
278 char *conf = (char *)malloc(len + strlen("rlimits/openfd.hard"));
279 struct pl_resources *r;
280 struct pl_resources sliver_list[] = {
281 {"sched/fill-rate2", TYPE_LONG, &slr->vs_cpu},
283 {"rlimits/nproc.hard", TYPE_LONG, &slr->vs_nproc.hard},
284 {"rlimits/nproc.soft", TYPE_LONG, &slr->vs_nproc.soft},
285 {"rlimits/nproc.min", TYPE_LONG, &slr->vs_nproc.min},
287 {"rlimits/rss.hard", TYPE_LONG, &slr->vs_rss.hard},
288 {"rlimits/rss.soft", TYPE_LONG, &slr->vs_rss.soft},
289 {"rlimits/rss.min", TYPE_LONG, &slr->vs_rss.min},
291 {"rlimits/as.hard", TYPE_LONG, &slr->vs_as.hard},
292 {"rlimits/as.soft", TYPE_LONG, &slr->vs_as.soft},
293 {"rlimits/as.min", TYPE_LONG, &slr->vs_as.min},
295 {"rlimits/openfd.hard", TYPE_LONG, &slr->vs_openfd.hard},
296 {"rlimits/openfd.soft", TYPE_LONG, &slr->vs_openfd.soft},
297 {"rlimits/openfd.min", TYPE_LONG, &slr->vs_openfd.min},
299 {"personality", TYPE_PERS, &slr->personality},
304 sprintf(conf, "%s%s", VSERVERCONF, context);
306 slr->vs_rss.hard = VC_LIM_KEEP;
307 slr->vs_rss.soft = VC_LIM_KEEP;
308 slr->vs_rss.min = VC_LIM_KEEP;
310 slr->vs_as.hard = VC_LIM_KEEP;
311 slr->vs_as.soft = VC_LIM_KEEP;
312 slr->vs_as.min = VC_LIM_KEEP;
314 slr->vs_nproc.hard = VC_LIM_KEEP;
315 slr->vs_nproc.soft = VC_LIM_KEEP;
316 slr->vs_nproc.min = VC_LIM_KEEP;
318 slr->vs_openfd.hard = VC_LIM_KEEP;
319 slr->vs_openfd.soft = VC_LIM_KEEP;
320 slr->vs_openfd.min = VC_LIM_KEEP;
322 slr->personality = 0;
324 cwd = open(".", O_RDONLY);
326 perror("cannot get a handle on .");
329 if (chdir(conf) == -1) {
330 fprintf(stderr, "cannot chdir to ");
335 for (r = &sliver_list[0]; r->name; r++) {
337 fb = fopen(r->name, "r");
340 if (fgets(buf, sizeof(buf), fb) != NULL) {
342 /* remove trailing newline */
343 if (buf[len-1] == '\n') {
347 if ( (r->type == TYPE_LONG) && isdigit(*buf)) {
348 *r->limit = atoi(buf);
349 } else if ( (r->type == TYPE_PERS) && isalpha(*buf)) {
350 unsigned long int res;
351 res = vc_str2personalitytype(buf,len);
352 if (res != VC_BAD_PERSONALITY) {
353 *r->personality = res;
369 adjust_lim(const struct vc_rlimit *vcr, struct rlimit *lim)
372 if (vcr->min != VC_LIM_KEEP) {
373 if (vcr->min > lim->rlim_cur) {
374 lim->rlim_cur = vcr->min;
377 if (vcr->min > lim->rlim_max) {
378 lim->rlim_max = vcr->min;
383 if (vcr->soft != VC_LIM_KEEP) {
384 switch (vcr->min != VC_LIM_KEEP) {
386 if (vcr->soft < vcr->min)
389 lim->rlim_cur = vcr->soft;
394 if (vcr->hard != VC_LIM_KEEP) {
395 switch (vcr->min != VC_LIM_KEEP) {
397 if (vcr->hard < vcr->min)
400 lim->rlim_max = vcr->hard;
408 set_one_ulimit(int resource, const struct vc_rlimit *limit)
411 getrlimit(resource, &lim);
412 adjust_lim(limit, &lim);
413 setrlimit(resource, &lim);
417 set_personality(unsigned long int personality_arg)
419 if (personality_arg == 0)
421 if (personality(personality_arg) < 0) {
428 pl_set_ulimits(const struct sliver_resources *slr)
433 set_one_ulimit(RLIMIT_RSS, &slr->vs_rss);
434 set_one_ulimit(RLIMIT_AS, &slr->vs_as);
435 set_one_ulimit(RLIMIT_NPROC, &slr->vs_nproc);
436 set_one_ulimit(RLIMIT_NOFILE, &slr->vs_openfd);
437 return set_personality(slr->personality);