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>
49 #include "planetlab.h"
51 #ifndef VC_NXC_RAW_SOCKET
52 # define VC_NXC_RAW_SOCKET 0x00000200ull
54 #ifndef VC_NXC_RAW_SEND
55 # define VC_NXC_RAW_SEND 0x00000400ull
57 #ifndef VC_NXF_LBACK_ALLOW
58 # define VC_NXF_LBACK_ALLOW 0x00000400ull
62 create_context(xid_t ctx, uint64_t bcaps)
64 struct vc_ctx_caps vc_caps;
65 struct vc_net_flags vc_nf;
66 struct vc_net_caps vc_ncaps;
68 /* Create network context */
69 if (vc_net_create(ctx) == VC_NOCTX) {
75 /* Make the network context persistent */
76 vc_nf.mask = vc_nf.flagword = VC_NXF_PERSISTENT | VC_NXF_LBACK_ALLOW;
77 if (vc_set_nflags(ctx, &vc_nf))
80 /* Give it raw sockets capabilities */
81 vc_ncaps.ncaps = vc_ncaps.cmask = VC_NXC_RAW_ICMP | VC_NXC_RAW_SOCKET;
82 if (vc_set_ncaps(ctx, &vc_ncaps))
86 /* Create tag context */
87 if (vc_tag_create(ctx) == VC_NOCTX)
92 * Create context info - this sets the STATE_SETUP and STATE_INIT flags.
94 if (vc_ctx_create(ctx, 0) == VC_NOCTX)
97 /* Set capabilities - these don't take effect until SETUP flag is unset */
98 vc_caps.bcaps = bcaps;
99 vc_caps.bmask = ~0ULL; /* currently unused */
100 vc_caps.ccaps = 0; /* don't want any of these */
101 vc_caps.cmask = ~0ULL;
102 if (vc_set_ccaps(ctx, &vc_caps))
105 if (pl_setsched(ctx, 0, 1) < 0) {
106 PERROR("pl_setsched(%u)", ctx);
114 pl_setup_done(xid_t ctx)
116 struct vc_ctx_flags vc_flags;
118 /* unset SETUP flag - this allows other processes to migrate */
119 /* set the PERSISTENT flag - so the context doesn't vanish */
120 /* Don't clear the STATE_INIT flag, as that would make us the init task. */
121 vc_flags.mask = VC_VXF_STATE_SETUP|VC_VXF_PERSISTENT;
122 vc_flags.flagword = VC_VXF_PERSISTENT;
123 if (vc_set_cflags(ctx, &vc_flags))
129 #define RETRY_LIMIT 10
132 pl_chcontext(xid_t ctx, uint64_t bcaps, const struct sliver_resources *slr)
135 int net_migrated = 0;
141 struct vc_ctx_flags vc_flags;
143 if (vc_get_cflags(ctx, &vc_flags))
148 /* context doesn't exist - create it */
149 if (create_context(ctx, bcaps))
152 /* another process beat us in a race */
155 /* another process is creating - poll the SETUP flag */
160 /* created context and migrated to it i.e., we're done */
164 /* check the SETUP flag */
165 if (vc_flags.flagword & VC_VXF_STATE_SETUP)
167 /* context is still being setup - wait a while then retry */
168 if (retry_count++ >= RETRY_LIMIT)
177 /* context has been setup */
179 if (net_migrated || !vc_net_migrate(ctx))
181 if (!vc_tag_migrate(ctx) && !vc_ctx_migrate(ctx, 0))
186 /* context disappeared - retry */
192 /* it's okay for a syscall to fail because the context doesn't exist */
193 #define VC_SYSCALL(x) \
197 return errno == ESRCH ? 0 : -1; \
202 pl_setsched(xid_t ctx, uint32_t cpu_min, uint32_t cpu_share)
204 struct vc_set_sched vc_sched;
205 struct vc_ctx_flags vc_flags;
207 vc_sched.set_mask = (VC_VXSM_FILL_RATE | VC_VXSM_INTERVAL | VC_VXSM_TOKENS |
208 VC_VXSM_TOKENS_MIN | VC_VXSM_TOKENS_MAX | VC_VXSM_MSEC |
209 VC_VXSM_FILL_RATE2 | VC_VXSM_INTERVAL2 | VC_VXSM_FORCE);
210 vc_sched.fill_rate = cpu_min; /* percent reserved */
211 vc_sched.interval = 100;
212 vc_sched.fill_rate2 = cpu_share; /* best-effort fair share of unreserved */
213 vc_sched.interval2 = 1000; /* milliseconds */
214 vc_sched.tokens = 100; /* initial allocation of tokens */
215 vc_sched.tokens_min = 50; /* need this many tokens to run */
216 vc_sched.tokens_max = 100; /* max accumulated number of tokens */
219 if (cpu_share == (uint32_t)VC_LIM_KEEP)
220 vc_sched.set_mask &= ~(VC_VXSM_FILL_RATE|VC_VXSM_FILL_RATE2);
222 vc_sched.set_mask |= VC_VXSM_IDLE_TIME;
225 VC_SYSCALL(vc_set_sched(ctx, &vc_sched));
227 vc_flags.mask = VC_VXF_SCHED_FLAGS;
228 vc_flags.flagword = VC_VXF_SCHED_HARD;
229 VC_SYSCALL(vc_set_cflags(ctx, &vc_flags));
234 struct pl_resources {
236 unsigned long long *limit;
239 #define WHITESPACE(buffer,index,len) \
240 while(isspace((int)buffer[index])) \
241 if (index < len) index++; else goto out;
243 #define VSERVERCONF "/etc/vservers/"
245 pl_get_limits(const char *context, struct sliver_resources *slr)
249 size_t len = strlen(VSERVERCONF) + strlen(context) + NULLBYTE_SIZE;
250 char *conf = (char *)malloc(len + strlen("rlimits/openfd.hard"));
251 struct pl_resources *r;
252 struct pl_resources sliver_list[] = {
253 {"sched/fill-rate2", &slr->vs_cpu},
255 {"rlimits/nproc.hard", &slr->vs_nproc.hard},
256 {"rlimits/nproc.soft", &slr->vs_nproc.soft},
257 {"rlimits/nproc.min", &slr->vs_nproc.min},
259 {"rlimits/rss.hard", &slr->vs_rss.hard},
260 {"rlimits/rss.soft", &slr->vs_rss.soft},
261 {"rlimits/rss.min", &slr->vs_rss.min},
263 {"rlimits/as.hard", &slr->vs_as.hard},
264 {"rlimits/as.soft", &slr->vs_as.soft},
265 {"rlimits/as.min", &slr->vs_as.min},
267 {"rlimits/openfd.hard", &slr->vs_openfd.hard},
268 {"rlimits/openfd.soft", &slr->vs_openfd.soft},
269 {"rlimits/openfd.min", &slr->vs_openfd.min},
274 sprintf(conf, "%s%s", VSERVERCONF, context);
276 slr->vs_rss.hard = VC_LIM_KEEP;
277 slr->vs_rss.soft = VC_LIM_KEEP;
278 slr->vs_rss.min = VC_LIM_KEEP;
280 slr->vs_as.hard = VC_LIM_KEEP;
281 slr->vs_as.soft = VC_LIM_KEEP;
282 slr->vs_as.min = VC_LIM_KEEP;
284 slr->vs_nproc.hard = VC_LIM_KEEP;
285 slr->vs_nproc.soft = VC_LIM_KEEP;
286 slr->vs_nproc.min = VC_LIM_KEEP;
288 slr->vs_openfd.hard = VC_LIM_KEEP;
289 slr->vs_openfd.soft = VC_LIM_KEEP;
290 slr->vs_openfd.min = VC_LIM_KEEP;
292 cwd = open(".", O_RDONLY);
294 perror("cannot get a handle on .");
297 if (chdir(conf) == -1) {
298 fprintf(stderr, "cannot chdir to ");
303 for (r = &sliver_list[0]; r->name; r++) {
305 fb = fopen(r->name, "r");
308 if (fgets(buf, sizeof(buf), fb) != NULL && isdigit(*buf))
309 *r->limit = atoi(buf);
321 adjust_lim(const struct vc_rlimit *vcr, struct rlimit *lim)
324 if (vcr->min != VC_LIM_KEEP) {
325 if (vcr->min > lim->rlim_cur) {
326 lim->rlim_cur = vcr->min;
329 if (vcr->min > lim->rlim_max) {
330 lim->rlim_max = vcr->min;
335 if (vcr->soft != VC_LIM_KEEP) {
336 switch (vcr->min != VC_LIM_KEEP) {
338 if (vcr->soft < vcr->min)
341 lim->rlim_cur = vcr->soft;
346 if (vcr->hard != VC_LIM_KEEP) {
347 switch (vcr->min != VC_LIM_KEEP) {
349 if (vcr->hard < vcr->min)
352 lim->rlim_cur = vcr->hard;
360 set_one_ulimit(int resource, const struct vc_rlimit *limit)
363 getrlimit(resource, &lim);
364 adjust_lim(limit, &lim);
365 setrlimit(resource, &lim);
369 pl_set_ulimits(const struct sliver_resources *slr)
374 set_one_ulimit(RLIMIT_RSS, &slr->vs_rss);
375 set_one_ulimit(RLIMIT_AS, &slr->vs_as);
376 set_one_ulimit(RLIMIT_NPROC, &slr->vs_nproc);
377 set_one_ulimit(RLIMIT_NOFILE, &slr->vs_openfd);