6b5d936450f7983b937b3317660af47490b3258c
[util-vserver.git] / lib / planetlab.c
1 /* Copyright 2005 Princeton University
2
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
5 are met: 
6
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9       
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.
14       
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.
18       
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. 
31
32 */
33
34 #include <errno.h>
35 #include <stdint.h>
36 #include <unistd.h>
37 #include <sys/resource.h>
38
39 #include "config.h"
40 #include "planetlab.h"
41 #include "sched_cmd.h"
42 #include "virtual.h"
43 #include "vserver.h"
44
45 static int
46 create_context(xid_t ctx, uint32_t flags, uint64_t bcaps, const rspec_t *rspec)
47 {
48   struct vc_ctx_caps  vc_caps;
49   struct vc_ctx_flags  vc_flags;
50   struct vc_rlimit  vc_rlimit;
51
52   /* create context info */
53   if (vc_ctx_create(ctx) == VC_NOCTX)
54     return -1;
55
56   /* set capabilities - these don't take effect until SETUP flags is unset */
57   vc_caps.bcaps = bcaps;
58   vc_caps.bmask = ~0ULL;  /* currently unused */
59   vc_caps.ccaps = 0;      /* don't want any of these */
60   vc_caps.cmask = ~0ULL;
61   if (vc_set_ccaps(ctx, &vc_caps))
62     return -1;
63
64   /* ignore all flags except SETUP and scheduler flags */
65   vc_flags.mask = VC_VXF_STATE_SETUP | VC_VXF_SCHED_FLAGS;
66   /* don't let user change scheduler flags */
67   vc_flags.flagword = flags & ~VC_VXF_SCHED_FLAGS;  /* SETUP not set */
68
69   /* set scheduler parameters */
70   vc_flags.flagword |= rspec->cpu_sched_flags;
71   pl_setsched(ctx, rspec->cpu_share, rspec->cpu_sched_flags);
72
73   /* set resource limits */
74   vc_rlimit.min = VC_LIM_KEEP;
75   vc_rlimit.soft = VC_LIM_KEEP;
76   vc_rlimit.hard = rspec->mem_limit;
77   if (vc_set_rlimit(ctx, RLIMIT_RSS, &vc_rlimit))
78     return -1;
79
80   /* assume min and soft unchanged by set_rlimit */
81   vc_rlimit.hard = rspec->task_limit;
82   if (vc_set_rlimit(ctx, RLIMIT_NPROC, &vc_rlimit))
83     return -1;
84
85   /* set flags, unset SETUP flag - this allows other processes to migrate */
86   if (vc_set_cflags(ctx, &vc_flags))
87     return -1;
88
89   return 0;
90 }
91
92 int
93 pl_chcontext(xid_t ctx, uint32_t flags, uint64_t bcaps, const rspec_t *rspec)
94 {
95   for (;;)
96     {
97       struct vc_ctx_flags  vc_flags;
98
99       if (vc_get_cflags(ctx, &vc_flags))
100         {
101           /* context doesn't exist - create it */
102           if (create_context(ctx, flags, bcaps, rspec))
103             {
104               if (errno == EEXIST)
105                 /* another process beat us in a race */
106                 goto migrate;
107               if (errno == EBUSY)
108                 /* another process is creating - poll the SETUP flag */
109                 continue;
110               return -1;
111             }
112
113           /* created context and migrated to it i.e., we're done */
114           break;
115         }
116
117       /* check the SETUP flag */
118       if (vc_flags.flagword & VC_VXF_STATE_SETUP)
119         {
120           /* context is still being setup - wait a while then retry */
121           sleep(1);
122           continue;
123         }
124
125       /* context has been setup */
126     migrate:
127       if (!vc_ctx_migrate(ctx))
128         break;  /* done */
129
130       /* context disappeared - retry */
131     }
132
133   return 0;
134 }
135
136 int
137 pl_setsched(xid_t ctx, uint32_t cpu_share, uint32_t cpu_sched_flags)
138 {
139   struct vc_set_sched  vc_sched;
140
141   vc_sched.set_mask = (VC_VXSM_FILL_RATE | VC_VXSM_INTERVAL | VC_VXSM_TOKENS |
142                        VC_VXSM_TOKENS_MIN | VC_VXSM_TOKENS_MAX);
143   vc_sched.fill_rate = cpu_share;  /* tokens accumulated per interval */
144   vc_sched.interval = 1000;  /* milliseconds */
145   vc_sched.tokens = 100;     /* initial allocation of tokens */
146   vc_sched.tokens_min = 50;  /* need this many tokens to run */
147   vc_sched.tokens_max = 100;  /* max accumulated number of tokens */
148
149   return vc_set_sched(ctx, &vc_sched);
150 }