hard == rlim_max
[util-vserver-pl.git] / src / 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 #ifdef HAVE_CONFIG_H
35 #  include <config.h>
36 #endif
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <stdint.h>
42 #include <stdarg.h>
43 #include <unistd.h>
44 #include <ctype.h>
45 #include <sys/resource.h>
46 #include <fcntl.h>
47
48 #include "vserver.h"
49 #include "planetlab.h"
50
51 #ifndef VC_NXC_RAW_SOCKET
52 #  define VC_NXC_RAW_SOCKET     0x00000200ull
53 #endif
54 #ifndef VC_NXC_RAW_SEND
55 #  define VC_NXC_RAW_SEND       0x00000400ull
56 #endif
57 #ifndef VC_NXF_LBACK_ALLOW
58 #  define VC_NXF_LBACK_ALLOW    0x00000400ull
59 #endif
60
61 static int
62 create_context(xid_t ctx, uint64_t bcaps)
63 {
64   struct vc_ctx_caps   vc_caps;
65   struct vc_net_flags  vc_nf;
66   struct vc_net_caps   vc_ncaps;
67
68   /* Create network context */
69   if (vc_net_create(ctx) == VC_NOCTX) {
70     if (errno == EEXIST)
71       goto tag;
72     return -1;
73   }
74
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))
78     return -1;
79
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))
83     return -1;
84
85 tag:
86   /* Create tag context */
87   if (vc_tag_create(ctx) == VC_NOCTX)
88     return -1;
89
90 process:
91   /*
92    * Create context info - this sets the STATE_SETUP and STATE_INIT flags.
93    */
94   if (vc_ctx_create(ctx, 0) == VC_NOCTX)
95     return -1;
96
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))
103     return -1;
104
105   if (pl_setsched(ctx, 0, 1) < 0) {
106     PERROR("pl_setsched(%u)", ctx);
107     exit(1);
108   }
109
110   return 0;
111 }
112
113 int
114 pl_setup_done(xid_t ctx)
115 {
116   struct vc_ctx_flags  vc_flags;
117
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))
124     return -1;
125
126   return 0;
127 }
128
129 #define RETRY_LIMIT  10
130
131 int
132 pl_chcontext(xid_t ctx, uint64_t bcaps, const struct sliver_resources *slr)
133 {
134   int  retry_count = 0;
135   int  net_migrated = 0;
136
137   pl_set_ulimits(slr);
138
139   for (;;)
140     {
141       struct vc_ctx_flags  vc_flags;
142
143       if (vc_get_cflags(ctx, &vc_flags))
144         {
145           if (errno != ESRCH)
146             return -1;
147
148           /* context doesn't exist - create it */
149           if (create_context(ctx, bcaps))
150             {
151               if (errno == EEXIST)
152                 /* another process beat us in a race */
153                 goto migrate;
154               if (errno == EBUSY)
155                 /* another process is creating - poll the SETUP flag */
156                 continue;
157               return -1;
158             }
159
160           /* created context and migrated to it i.e., we're done */
161           return 1;
162         }
163
164       /* check the SETUP flag */
165       if (vc_flags.flagword & VC_VXF_STATE_SETUP)
166         {
167           /* context is still being setup - wait a while then retry */
168           if (retry_count++ >= RETRY_LIMIT)
169             {
170               errno = EBUSY;
171               return -1;
172             }
173           sleep(1);
174           continue;
175         }
176
177       /* context has been setup */
178     migrate:
179       if (net_migrated || !vc_net_migrate(ctx))
180         {
181           if (!vc_tag_migrate(ctx) && !vc_ctx_migrate(ctx, 0))
182             break;  /* done */
183           net_migrated = 1;
184         }
185
186       /* context disappeared - retry */
187     }
188
189   return 0;
190 }
191
192 /* it's okay for a syscall to fail because the context doesn't exist */
193 #define VC_SYSCALL(x)                           \
194 do                                              \
195 {                                               \
196   if (x)                                        \
197     return errno == ESRCH ? 0 : -1;             \
198 }                                               \
199 while (0)
200
201 int
202 pl_setsched(xid_t ctx, uint32_t cpu_min, uint32_t cpu_share)
203 {
204   struct vc_set_sched  vc_sched;
205   struct vc_ctx_flags  vc_flags;
206
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 */
217
218   if (cpu_share) {
219     if (cpu_share == (uint32_t)VC_LIM_KEEP)
220       vc_sched.set_mask &= ~(VC_VXSM_FILL_RATE|VC_VXSM_FILL_RATE2);
221     else
222       vc_sched.set_mask |= VC_VXSM_IDLE_TIME;
223   }
224
225   VC_SYSCALL(vc_set_sched(ctx, &vc_sched));
226
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));
230
231   return 0;
232 }
233
234 struct pl_resources {
235         char *name;
236         unsigned long long *limit;
237 };
238
239 #define WHITESPACE(buffer,index,len)     \
240   while(isspace((int)buffer[index])) \
241         if (index < len) index++; else goto out;
242
243 #define VSERVERCONF "/etc/vservers/"
244 void
245 pl_get_limits(const char *context, struct sliver_resources *slr)
246 {
247   FILE *fb;
248   int cwd;
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},
254
255     {"rlimits/nproc.hard", &slr->vs_nproc.hard},
256     {"rlimits/nproc.soft", &slr->vs_nproc.soft},
257     {"rlimits/nproc.min", &slr->vs_nproc.min},
258   
259     {"rlimits/rss.hard", &slr->vs_rss.hard},
260     {"rlimits/rss.soft", &slr->vs_rss.soft},
261     {"rlimits/rss.min", &slr->vs_rss.min},
262   
263     {"rlimits/as.hard", &slr->vs_as.hard},
264     {"rlimits/as.soft", &slr->vs_as.soft},
265     {"rlimits/as.min", &slr->vs_as.min},
266   
267     {"rlimits/openfd.hard", &slr->vs_openfd.hard},
268     {"rlimits/openfd.soft", &slr->vs_openfd.soft},
269     {"rlimits/openfd.min", &slr->vs_openfd.min},
270
271     {0,0}
272   };
273
274   sprintf(conf, "%s%s", VSERVERCONF, context);
275
276   slr->vs_rss.hard = VC_LIM_KEEP;
277   slr->vs_rss.soft = VC_LIM_KEEP;
278   slr->vs_rss.min = VC_LIM_KEEP;
279
280   slr->vs_as.hard = VC_LIM_KEEP;
281   slr->vs_as.soft = VC_LIM_KEEP;
282   slr->vs_as.min = VC_LIM_KEEP;
283
284   slr->vs_nproc.hard = VC_LIM_KEEP;
285   slr->vs_nproc.soft = VC_LIM_KEEP;
286   slr->vs_nproc.min = VC_LIM_KEEP;
287
288   slr->vs_openfd.hard = VC_LIM_KEEP;
289   slr->vs_openfd.soft = VC_LIM_KEEP;
290   slr->vs_openfd.min = VC_LIM_KEEP;
291
292   cwd = open(".", O_RDONLY);
293   if (cwd == -1) {
294     perror("cannot get a handle on .");
295     goto out;
296   }
297   if (chdir(conf) == -1) {
298     fprintf(stderr, "cannot chdir to ");
299     perror(conf);
300     goto out_fd;
301   }
302
303   for (r = &sliver_list[0]; r->name; r++) {
304     char buf[1000];
305     fb = fopen(r->name, "r");
306     if (fb == NULL)
307       continue;
308     if (fgets(buf, sizeof(buf), fb) != NULL && isdigit(*buf))
309       *r->limit = atoi(buf);
310     fclose(fb);
311   }
312
313   fchdir(cwd);
314 out_fd:
315   close(cwd);
316 out:
317   free(conf);
318 }
319
320 int
321 adjust_lim(const struct vc_rlimit *vcr, struct rlimit *lim)
322 {
323   int adjusted = 0;
324   if (vcr->min != VC_LIM_KEEP) {
325     if (vcr->min > lim->rlim_cur) {
326       lim->rlim_cur = vcr->min;
327       adjusted = 1;
328     }
329     if (vcr->min > lim->rlim_max) {
330       lim->rlim_max = vcr->min;
331       adjusted = 1;
332     }
333   }
334
335   if (vcr->soft != VC_LIM_KEEP) {
336     switch (vcr->min != VC_LIM_KEEP) {
337     case 1:
338       if (vcr->soft < vcr->min)
339         break;
340     case 0:
341         lim->rlim_cur = vcr->soft;
342         adjusted = 1;
343     }
344   }
345
346   if (vcr->hard != VC_LIM_KEEP) {
347     switch (vcr->min != VC_LIM_KEEP) {
348     case 1:
349       if (vcr->hard < vcr->min)
350         break;
351     case 0:
352         lim->rlim_max = vcr->hard;
353         adjusted = 1;
354     }
355   }
356   return adjusted;
357 }
358
359 static inline void
360 set_one_ulimit(int resource, const struct vc_rlimit *limit)
361 {
362   struct rlimit lim;
363   getrlimit(resource, &lim);
364   adjust_lim(limit, &lim);
365   setrlimit(resource, &lim);
366 }
367
368 void
369 pl_set_ulimits(const struct sliver_resources *slr)
370 {
371   if (!slr)
372     return;
373
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);
378 }