Re: Reuse VM ids.
On Fri, 16 Nov 2018 16:15:14 +0100, Reyk Floeter wrote: > On Sat, Oct 27, 2018 at 02:53:16PM -0700, Ori Bernstein wrote: > > On Fri, 26 Oct 2018 01:57:15 +0200, Reyk Floeter wrote: > > > > > On Tue, Oct 23, 2018 at 10:21:08PM -0700, Ori Bernstein wrote: > > > > On Mon, 8 Oct 2018 07:59:15 -0700, Bob Beck wrote: > > > > > > > > > works here and I like it. but probably for after unlock > > > > > > > > > > > > > It's after unlock -- pinging for OKs. > > > > > > > > > > Not yet. Please include the VM's uid in the claim, e.g. > > > > > > claim_vmid(const char *name, uid_t uid) > > > > > > It is not a strong protection, but it doesn't make sense that other > > > users can run a VM with the same name and get the claimed Id. > > > > > > Reyk > > > > > > > Updated. > > > > Ok? > > > > Sorry for the delay... > > Two minor things: > > - I think config_purge() should clear env->vmd_known as it does with > vmd_vms and vmd_switches. > > - The use of static functions (static uint32_t vm_claimid) is disputable > but somewhat uncommon in OpenBSD. But even static functions do need a > prototype ("All functions are prototyped somewhere." - style(9)). Done. Anyone want to give a second ok? diff --git usr.sbin/vmd/config.c usr.sbin/vmd/config.c index 20a16f85442..c662d329eb4 100644 --- usr.sbin/vmd/config.c +++ usr.sbin/vmd/config.c @@ -87,7 +87,10 @@ config_init(struct vmd *env) if (what & CONFIG_VMS) { if ((env->vmd_vms = calloc(1, sizeof(*env->vmd_vms))) == NULL) return (-1); + if ((env->vmd_known = calloc(1, sizeof(*env->vmd_known))) == NULL) + return (-1); TAILQ_INIT(env->vmd_vms); + TAILQ_INIT(env->vmd_known); } if (what & CONFIG_SWITCHES) { if ((env->vmd_switches = calloc(1, @@ -109,6 +112,7 @@ void config_purge(struct vmd *env, unsigned int reset) { struct privsep *ps = &env->vmd_ps; + struct name2id *n2i; struct vmd_vm *vm; struct vmd_switch *vsw; unsigned int what; @@ -125,6 +129,10 @@ config_purge(struct vmd *env, unsigned int reset) while ((vm = TAILQ_FIRST(env->vmd_vms)) != NULL) { vm_remove(vm, __func__); } + while ((n2i = TAILQ_FIRST(env->vmd_known)) != NULL) { + TAILQ_REMOVE(env->vmd_known, n2i, entry); + free(n2i); + } env->vmd_nvm = 0; } if (what & CONFIG_SWITCHES && env->vmd_switches != NULL) { diff --git usr.sbin/vmd/vmd.c usr.sbin/vmd/vmd.c index 9423081df1e..5bb751511d0 100644 --- usr.sbin/vmd/vmd.c +++ usr.sbin/vmd/vmd.c @@ -62,6 +62,7 @@ intvmd_check_vmh(struct vm_dump_header *); int vm_instance(struct privsep *, struct vmd_vm **, struct vmop_create_params *, uid_t); int vm_checkinsflag(struct vmop_create_params *, unsigned int, uid_t); +uint32_t vm_claimid(const char *, int); struct vmd *env; @@ -1169,6 +1170,28 @@ vm_remove(struct vmd_vm *vm, const char *caller) free(vm); } +uint32_t +vm_claimid(const char *name, int uid) +{ + struct name2id *n2i = NULL; + + TAILQ_FOREACH(n2i, env->vmd_known, entry) + if (strcmp(n2i->name, name) == 0 && n2i->uid == uid) + return n2i->id; + + if (++env->vmd_nvm == 0) + fatalx("too many vms"); + if ((n2i = calloc(1, sizeof(struct name2id))) == NULL) + fatalx("could not alloc vm name"); + n2i->id = env->vmd_nvm; + n2i->uid = uid; + if (strlcpy(n2i->name, name, sizeof(n2i->name)) >= sizeof(n2i->name)) + fatalx("overlong vm name"); + TAILQ_INSERT_TAIL(env->vmd_known, n2i, entry); + + return n2i->id; +} + int vm_register(struct privsep *ps, struct vmop_create_params *vmc, struct vmd_vm **ret_vm, uint32_t id, uid_t uid) @@ -1300,11 +1323,8 @@ vm_register(struct privsep *ps, struct vmop_create_params *vmc, vm->vm_cdrom = -1; vm->vm_iev.ibuf.fd = -1; - if (++env->vmd_nvm == 0) - fatalx("too many vms"); - /* Assign a new internal Id if not specified */ - vm->vm_vmid = id == 0 ? env->vmd_nvm : id; + vm->vm_vmid = (id == 0) ? vm_claimid(vcp->vcp_name, uid) : id; log_debug("%s: registering vm %d", __func__, vm->vm_vmid); TAILQ_INSERT_TAIL(env->vmd_vms, vm, vm_entry); diff --git usr.sbin/vmd/vmd.h usr.sbin/vmd/vmd.h index de87e4337ee..6000dd6d63f 100644 --- usr.sbin/vmd/vmd.h +++ usr.sbin/vmd/vmd.h @@ -290,6 +290,14 @@ struct vmd_user { }; TAILQ_HEAD(userlist, vmd_user); +struct name2id { + charname[VMM_MAX_NAME_LEN]; + int uid; + int32_t id; + TAILQ_ENTRY(name2id)entry; +}; +TAILQ_HEAD(name2idlist, name2id); + struct addres
Re: Reuse VM ids.
On Sat, Oct 27, 2018 at 02:53:16PM -0700, Ori Bernstein wrote: > On Fri, 26 Oct 2018 01:57:15 +0200, Reyk Floeter wrote: > > > On Tue, Oct 23, 2018 at 10:21:08PM -0700, Ori Bernstein wrote: > > > On Mon, 8 Oct 2018 07:59:15 -0700, Bob Beck wrote: > > > > > > > works here and I like it. but probably for after unlock > > > > > > > > > > It's after unlock -- pinging for OKs. > > > > > > > Not yet. Please include the VM's uid in the claim, e.g. > > > > claim_vmid(const char *name, uid_t uid) > > > > It is not a strong protection, but it doesn't make sense that other > > users can run a VM with the same name and get the claimed Id. > > > > Reyk > > > > Updated. > > Ok? > Sorry for the delay... Two minor things: - I think config_purge() should clear env->vmd_known as it does with vmd_vms and vmd_switches. - The use of static functions (static uint32_t vm_claimid) is disputable but somewhat uncommon in OpenBSD. But even static functions do need a prototype ("All functions are prototyped somewhere." - style(9)). Otherwise OK reyk@ > diff --git usr.sbin/vmd/config.c usr.sbin/vmd/config.c > index a749e3595b5..31f46bf3c5b 100644 > --- usr.sbin/vmd/config.c > +++ usr.sbin/vmd/config.c > @@ -60,7 +60,10 @@ config_init(struct vmd *env) > if (what & CONFIG_VMS) { > if ((env->vmd_vms = calloc(1, sizeof(*env->vmd_vms))) == NULL) > return (-1); > + if ((env->vmd_known = calloc(1, sizeof(*env->vmd_known))) == > NULL) > + return (-1); > TAILQ_INIT(env->vmd_vms); > + TAILQ_INIT(env->vmd_known); > } > if (what & CONFIG_SWITCHES) { > if ((env->vmd_switches = calloc(1, > diff --git usr.sbin/vmd/vmd.c usr.sbin/vmd/vmd.c > index 8053b02620f..0683812f9f0 100644 > --- usr.sbin/vmd/vmd.c > +++ usr.sbin/vmd/vmd.c > @@ -1169,6 +1169,28 @@ vm_remove(struct vmd_vm *vm, const char *caller) > free(vm); > } > > +static uint32_t > +vm_claimid(const char *name, int uid) > +{ > + struct name2id *n2i = NULL; > + > + TAILQ_FOREACH(n2i, env->vmd_known, entry) > + if (strcmp(n2i->name, name) == 0 && n2i->uid == uid) > + return n2i->id; > + > + if (++env->vmd_nvm == 0) > + fatalx("too many vms"); > + if ((n2i = calloc(1, sizeof(struct name2id))) == NULL) > + fatalx("could not alloc vm name"); > + n2i->id = env->vmd_nvm; > + n2i->uid = uid; > + if (strlcpy(n2i->name, name, sizeof(n2i->name)) >= sizeof(n2i->name)) > + fatalx("overlong vm name"); > + TAILQ_INSERT_TAIL(env->vmd_known, n2i, entry); > + > + return n2i->id; > +} > + > int > vm_register(struct privsep *ps, struct vmop_create_params *vmc, > struct vmd_vm **ret_vm, uint32_t id, uid_t uid) > @@ -1300,11 +1322,8 @@ vm_register(struct privsep *ps, struct > vmop_create_params *vmc, > vm->vm_cdrom = -1; > vm->vm_iev.ibuf.fd = -1; > > - if (++env->vmd_nvm == 0) > - fatalx("too many vms"); > - > /* Assign a new internal Id if not specified */ > - vm->vm_vmid = id == 0 ? env->vmd_nvm : id; > + vm->vm_vmid = (id == 0) ? vm_claimid(vcp->vcp_name, uid) : id; > > log_debug("%s: registering vm %d", __func__, vm->vm_vmid); > TAILQ_INSERT_TAIL(env->vmd_vms, vm, vm_entry); > diff --git usr.sbin/vmd/vmd.h usr.sbin/vmd/vmd.h > index 7ae4e4bd65e..047b5bb3e00 100644 > --- usr.sbin/vmd/vmd.h > +++ usr.sbin/vmd/vmd.h > @@ -284,6 +284,14 @@ struct vmd_user { > }; > TAILQ_HEAD(userlist, vmd_user); > > +struct name2id { > + charname[VMM_MAX_NAME_LEN]; > + int uid; > + int32_t id; > + TAILQ_ENTRY(name2id)entry; > +}; > +TAILQ_HEAD(name2idlist, name2id); > + > struct address { > struct sockaddr_storage ss; > int prefixlen; > @@ -308,6 +316,7 @@ struct vmd { > > uint32_t vmd_nvm; > struct vmlist *vmd_vms; > + struct name2idlist *vmd_known; > uint32_t vmd_nswitches; > struct switchlist *vmd_switches; > struct userlist *vmd_users; > > -- > Ori Bernstein
Re: Reuse VM ids.
On Sat, 27 Oct 2018 14:53:16 -0700, Ori Bernstein wrote: > On Fri, 26 Oct 2018 01:57:15 +0200, Reyk Floeter wrote: > > > On Tue, Oct 23, 2018 at 10:21:08PM -0700, Ori Bernstein wrote: > > > On Mon, 8 Oct 2018 07:59:15 -0700, Bob Beck wrote: > > > > > > > works here and I like it. but probably for after unlock > > > > > > > > > > It's after unlock -- pinging for OKs. > > > > > > > Not yet. Please include the VM's uid in the claim, e.g. > > > > claim_vmid(const char *name, uid_t uid) > > > > It is not a strong protection, but it doesn't make sense that other > > users can run a VM with the same name and get the claimed Id. > > > > Reyk > > > > Updated. > > Ok? > Ping? -- Ori Bernstein
Re: Reuse VM ids.
On Fri, 26 Oct 2018 01:57:15 +0200, Reyk Floeter wrote: > On Tue, Oct 23, 2018 at 10:21:08PM -0700, Ori Bernstein wrote: > > On Mon, 8 Oct 2018 07:59:15 -0700, Bob Beck wrote: > > > > > works here and I like it. but probably for after unlock > > > > > > > It's after unlock -- pinging for OKs. > > > > Not yet. Please include the VM's uid in the claim, e.g. > > claim_vmid(const char *name, uid_t uid) > > It is not a strong protection, but it doesn't make sense that other > users can run a VM with the same name and get the claimed Id. > > Reyk > Updated. Ok? diff --git usr.sbin/vmd/config.c usr.sbin/vmd/config.c index a749e3595b5..31f46bf3c5b 100644 --- usr.sbin/vmd/config.c +++ usr.sbin/vmd/config.c @@ -60,7 +60,10 @@ config_init(struct vmd *env) if (what & CONFIG_VMS) { if ((env->vmd_vms = calloc(1, sizeof(*env->vmd_vms))) == NULL) return (-1); + if ((env->vmd_known = calloc(1, sizeof(*env->vmd_known))) == NULL) + return (-1); TAILQ_INIT(env->vmd_vms); + TAILQ_INIT(env->vmd_known); } if (what & CONFIG_SWITCHES) { if ((env->vmd_switches = calloc(1, diff --git usr.sbin/vmd/vmd.c usr.sbin/vmd/vmd.c index 8053b02620f..0683812f9f0 100644 --- usr.sbin/vmd/vmd.c +++ usr.sbin/vmd/vmd.c @@ -1169,6 +1169,28 @@ vm_remove(struct vmd_vm *vm, const char *caller) free(vm); } +static uint32_t +vm_claimid(const char *name, int uid) +{ + struct name2id *n2i = NULL; + + TAILQ_FOREACH(n2i, env->vmd_known, entry) + if (strcmp(n2i->name, name) == 0 && n2i->uid == uid) + return n2i->id; + + if (++env->vmd_nvm == 0) + fatalx("too many vms"); + if ((n2i = calloc(1, sizeof(struct name2id))) == NULL) + fatalx("could not alloc vm name"); + n2i->id = env->vmd_nvm; + n2i->uid = uid; + if (strlcpy(n2i->name, name, sizeof(n2i->name)) >= sizeof(n2i->name)) + fatalx("overlong vm name"); + TAILQ_INSERT_TAIL(env->vmd_known, n2i, entry); + + return n2i->id; +} + int vm_register(struct privsep *ps, struct vmop_create_params *vmc, struct vmd_vm **ret_vm, uint32_t id, uid_t uid) @@ -1300,11 +1322,8 @@ vm_register(struct privsep *ps, struct vmop_create_params *vmc, vm->vm_cdrom = -1; vm->vm_iev.ibuf.fd = -1; - if (++env->vmd_nvm == 0) - fatalx("too many vms"); - /* Assign a new internal Id if not specified */ - vm->vm_vmid = id == 0 ? env->vmd_nvm : id; + vm->vm_vmid = (id == 0) ? vm_claimid(vcp->vcp_name, uid) : id; log_debug("%s: registering vm %d", __func__, vm->vm_vmid); TAILQ_INSERT_TAIL(env->vmd_vms, vm, vm_entry); diff --git usr.sbin/vmd/vmd.h usr.sbin/vmd/vmd.h index 7ae4e4bd65e..047b5bb3e00 100644 --- usr.sbin/vmd/vmd.h +++ usr.sbin/vmd/vmd.h @@ -284,6 +284,14 @@ struct vmd_user { }; TAILQ_HEAD(userlist, vmd_user); +struct name2id { + charname[VMM_MAX_NAME_LEN]; + int uid; + int32_t id; + TAILQ_ENTRY(name2id)entry; +}; +TAILQ_HEAD(name2idlist, name2id); + struct address { struct sockaddr_storage ss; int prefixlen; @@ -308,6 +316,7 @@ struct vmd { uint32_t vmd_nvm; struct vmlist *vmd_vms; + struct name2idlist *vmd_known; uint32_t vmd_nswitches; struct switchlist *vmd_switches; struct userlist *vmd_users; -- Ori Bernstein
Re: Reuse VM ids.
On Tue, Oct 23, 2018 at 10:21:08PM -0700, Ori Bernstein wrote: > On Mon, 8 Oct 2018 07:59:15 -0700, Bob Beck wrote: > > > works here and I like it. but probably for after unlock > > > > It's after unlock -- pinging for OKs. > Not yet. Please include the VM's uid in the claim, e.g. claim_vmid(const char *name, uid_t uid) It is not a strong protection, but it doesn't make sense that other users can run a VM with the same name and get the claimed Id. Reyk > diff --git usr.sbin/vmd/config.c usr.sbin/vmd/config.c > index af12b790002..522bae32501 100644 > --- usr.sbin/vmd/config.c > +++ usr.sbin/vmd/config.c > @@ -61,7 +61,10 @@ config_init(struct vmd *env) > if (what & CONFIG_VMS) { > if ((env->vmd_vms = calloc(1, sizeof(*env->vmd_vms))) == NULL) > return (-1); > + if ((env->vmd_known = calloc(1, sizeof(*env->vmd_known))) == > NULL) > + return (-1); > TAILQ_INIT(env->vmd_vms); > + TAILQ_INIT(env->vmd_known); > } > if (what & CONFIG_SWITCHES) { > if ((env->vmd_switches = calloc(1, > diff --git usr.sbin/vmd/vmd.c usr.sbin/vmd/vmd.c > index 18a5e0d3d5d..732691b4381 100644 > --- usr.sbin/vmd/vmd.c > +++ usr.sbin/vmd/vmd.c > @@ -1169,6 +1169,27 @@ vm_remove(struct vmd_vm *vm, const char *caller) > free(vm); > } > > +static uint32_t > +claim_vmid(const char *name) > +{ > + struct name2id *n2i = NULL; > + > + TAILQ_FOREACH(n2i, env->vmd_known, entry) > + if (strcmp(n2i->name, name) == 0) > + return n2i->id; > + > + if (++env->vmd_nvm == 0) > + fatalx("too many vms"); > + if ((n2i = calloc(1, sizeof(struct name2id))) == NULL) > + fatalx("could not alloc vm name"); > + n2i->id = env->vmd_nvm; > + if (strlcpy(n2i->name, name, sizeof(n2i->name)) >= sizeof(n2i->name)) > + fatalx("overlong vm name"); > + TAILQ_INSERT_TAIL(env->vmd_known, n2i, entry); > + > + return n2i->id; > +} > + > int > vm_register(struct privsep *ps, struct vmop_create_params *vmc, > struct vmd_vm **ret_vm, uint32_t id, uid_t uid) > @@ -1300,11 +1321,8 @@ vm_register(struct privsep *ps, struct > vmop_create_params *vmc, > vm->vm_cdrom = -1; > vm->vm_iev.ibuf.fd = -1; > > - if (++env->vmd_nvm == 0) > - fatalx("too many vms"); > - > /* Assign a new internal Id if not specified */ > - vm->vm_vmid = id == 0 ? env->vmd_nvm : id; > + vm->vm_vmid = (id == 0) ? claim_vmid(vcp->vcp_name) : id; > > log_debug("%s: registering vm %d", __func__, vm->vm_vmid); > TAILQ_INSERT_TAIL(env->vmd_vms, vm, vm_entry); > diff --git usr.sbin/vmd/vmd.h usr.sbin/vmd/vmd.h > index b7c012854e8..86fad536e59 100644 > --- usr.sbin/vmd/vmd.h > +++ usr.sbin/vmd/vmd.h > @@ -276,6 +276,13 @@ struct vmd_user { > }; > TAILQ_HEAD(userlist, vmd_user); > > +struct name2id { > + charname[VMM_MAX_NAME_LEN]; > + int32_t id; > + TAILQ_ENTRY(name2id)entry; > +}; > +TAILQ_HEAD(name2idlist, name2id); > + > struct address { > struct sockaddr_storage ss; > int prefixlen; > @@ -300,6 +307,7 @@ struct vmd { > > uint32_t vmd_nvm; > struct vmlist *vmd_vms; > + struct name2idlist *vmd_known; > uint32_t vmd_nswitches; > struct switchlist *vmd_switches; > struct userlist *vmd_users; >
Re: Reuse VM ids.
On Tue, Oct 23, 2018 at 10:21:08PM -0700, Ori Bernstein wrote: > On Mon, 8 Oct 2018 07:59:15 -0700, Bob Beck wrote: > > > works here and I like it. but probably for after unlock > > > > It's after unlock -- pinging for OKs. > > -- > Ori Bernstein > ok ccardenas@ +--+ Carlos
Re: Reuse VM ids.
On Mon, 8 Oct 2018 07:59:15 -0700, Bob Beck wrote: > works here and I like it. but probably for after unlock > It's after unlock -- pinging for OKs. -- Ori Bernstein
Re: Reuse VM ids.
On Sun, Oct 07, 2018 at 07:31:45PM -0700, Ori Bernstein wrote: > Keep a list of known vms, and reuse the VM IDs. This means that when using > '-L', the IP addresses of the VMs are stable. > After you conviced me about its use case, 3 comments: 1. it has to wait until after release 2. please following the naming vm_claimid instead of claim_vmid 3. you should bind it to name + vm_uid, not just name I was actually wondering if we should use randomized non-linear vm ids (e.g. with knuth shuffle) in the future, but that's a different topic for a different time. This diff wouldn't conflict with it. Reyk > diff --git usr.sbin/vmd/config.c usr.sbin/vmd/config.c > index af12b790002..522bae32501 100644 > --- usr.sbin/vmd/config.c > +++ usr.sbin/vmd/config.c > @@ -61,7 +61,10 @@ config_init(struct vmd *env) > if (what & CONFIG_VMS) { > if ((env->vmd_vms = calloc(1, sizeof(*env->vmd_vms))) == NULL) > return (-1); > + if ((env->vmd_known = calloc(1, sizeof(*env->vmd_known))) == > NULL) > + return (-1); > TAILQ_INIT(env->vmd_vms); > + TAILQ_INIT(env->vmd_known); > } > if (what & CONFIG_SWITCHES) { > if ((env->vmd_switches = calloc(1, > diff --git usr.sbin/vmd/vmd.c usr.sbin/vmd/vmd.c > index 18a5e0d3d5d..732691b4381 100644 > --- usr.sbin/vmd/vmd.c > +++ usr.sbin/vmd/vmd.c > @@ -1169,6 +1169,27 @@ vm_remove(struct vmd_vm *vm, const char *caller) > free(vm); > } > > +static uint32_t > +claim_vmid(const char *name) > +{ > + struct name2id *n2i = NULL; > + > + TAILQ_FOREACH(n2i, env->vmd_known, entry) > + if (strcmp(n2i->name, name) == 0) > + return n2i->id; > + > + if (++env->vmd_nvm == 0) > + fatalx("too many vms"); > + if ((n2i = calloc(1, sizeof(struct name2id))) == NULL) > + fatalx("could not alloc vm name"); > + n2i->id = env->vmd_nvm; > + if (strlcpy(n2i->name, name, sizeof(n2i->name)) >= sizeof(n2i->name)) > + fatalx("overlong vm name"); > + TAILQ_INSERT_TAIL(env->vmd_known, n2i, entry); > + > + return n2i->id; > +} > + > int > vm_register(struct privsep *ps, struct vmop_create_params *vmc, > struct vmd_vm **ret_vm, uint32_t id, uid_t uid) > @@ -1300,11 +1321,8 @@ vm_register(struct privsep *ps, struct > vmop_create_params *vmc, > vm->vm_cdrom = -1; > vm->vm_iev.ibuf.fd = -1; > > - if (++env->vmd_nvm == 0) > - fatalx("too many vms"); > - > /* Assign a new internal Id if not specified */ > - vm->vm_vmid = id == 0 ? env->vmd_nvm : id; > + vm->vm_vmid = (id == 0) ? claim_vmid(vcp->vcp_name) : id; > > log_debug("%s: registering vm %d", __func__, vm->vm_vmid); > TAILQ_INSERT_TAIL(env->vmd_vms, vm, vm_entry); > diff --git usr.sbin/vmd/vmd.h usr.sbin/vmd/vmd.h > index b7c012854e8..86fad536e59 100644 > --- usr.sbin/vmd/vmd.h > +++ usr.sbin/vmd/vmd.h > @@ -276,6 +276,13 @@ struct vmd_user { > }; > TAILQ_HEAD(userlist, vmd_user); > > +struct name2id { > + charname[VMM_MAX_NAME_LEN]; > + int32_t id; > + TAILQ_ENTRY(name2id)entry; > +}; > +TAILQ_HEAD(name2idlist, name2id); > + > struct address { > struct sockaddr_storage ss; > int prefixlen; > @@ -300,6 +307,7 @@ struct vmd { > > uint32_t vmd_nvm; > struct vmlist *vmd_vms; > + struct name2idlist *vmd_known; > uint32_t vmd_nswitches; > struct switchlist *vmd_switches; > struct userlist *vmd_users; > > -- > Ori Bernstein --
Re: Reuse VM ids.
works here and I like it. but probably for after unlock On Sun, Oct 7, 2018 at 22:11 Mischa Peters wrote: > No idea if the code works yet. > Hopefully I can try later. But love the idea. > > Mischa > > > On 8 Oct 2018, at 04:31, Ori Bernstein wrote: > > > > Keep a list of known vms, and reuse the VM IDs. This means that when > using > > '-L', the IP addresses of the VMs are stable. > > > > diff --git usr.sbin/vmd/config.c usr.sbin/vmd/config.c > > index af12b790002..522bae32501 100644 > > --- usr.sbin/vmd/config.c > > +++ usr.sbin/vmd/config.c > > @@ -61,7 +61,10 @@ config_init(struct vmd *env) > >if (what & CONFIG_VMS) { > >if ((env->vmd_vms = calloc(1, sizeof(*env->vmd_vms))) == NULL) > >return (-1); > > +if ((env->vmd_known = calloc(1, sizeof(*env->vmd_known))) == > NULL) > > +return (-1); > >TAILQ_INIT(env->vmd_vms); > > +TAILQ_INIT(env->vmd_known); > >} > >if (what & CONFIG_SWITCHES) { > >if ((env->vmd_switches = calloc(1, > > diff --git usr.sbin/vmd/vmd.c usr.sbin/vmd/vmd.c > > index 18a5e0d3d5d..732691b4381 100644 > > --- usr.sbin/vmd/vmd.c > > +++ usr.sbin/vmd/vmd.c > > @@ -1169,6 +1169,27 @@ vm_remove(struct vmd_vm *vm, const char *caller) > >free(vm); > > } > > > > +static uint32_t > > +claim_vmid(const char *name) > > +{ > > +struct name2id *n2i = NULL; > > + > > +TAILQ_FOREACH(n2i, env->vmd_known, entry) > > +if (strcmp(n2i->name, name) == 0) > > +return n2i->id; > > + > > +if (++env->vmd_nvm == 0) > > +fatalx("too many vms"); > > +if ((n2i = calloc(1, sizeof(struct name2id))) == NULL) > > +fatalx("could not alloc vm name"); > > +n2i->id = env->vmd_nvm; > > +if (strlcpy(n2i->name, name, sizeof(n2i->name)) >= > sizeof(n2i->name)) > > +fatalx("overlong vm name"); > > +TAILQ_INSERT_TAIL(env->vmd_known, n2i, entry); > > + > > +return n2i->id; > > +} > > + > > int > > vm_register(struct privsep *ps, struct vmop_create_params *vmc, > > struct vmd_vm **ret_vm, uint32_t id, uid_t uid) > > @@ -1300,11 +1321,8 @@ vm_register(struct privsep *ps, struct > vmop_create_params *vmc, > >vm->vm_cdrom = -1; > >vm->vm_iev.ibuf.fd = -1; > > > > -if (++env->vmd_nvm == 0) > > -fatalx("too many vms"); > > - > >/* Assign a new internal Id if not specified */ > > -vm->vm_vmid = id == 0 ? env->vmd_nvm : id; > > +vm->vm_vmid = (id == 0) ? claim_vmid(vcp->vcp_name) : id; > > > >log_debug("%s: registering vm %d", __func__, vm->vm_vmid); > >TAILQ_INSERT_TAIL(env->vmd_vms, vm, vm_entry); > > diff --git usr.sbin/vmd/vmd.h usr.sbin/vmd/vmd.h > > index b7c012854e8..86fad536e59 100644 > > --- usr.sbin/vmd/vmd.h > > +++ usr.sbin/vmd/vmd.h > > @@ -276,6 +276,13 @@ struct vmd_user { > > }; > > TAILQ_HEAD(userlist, vmd_user); > > > > +struct name2id { > > +charname[VMM_MAX_NAME_LEN]; > > +int32_tid; > > +TAILQ_ENTRY(name2id)entry; > > +}; > > +TAILQ_HEAD(name2idlist, name2id); > > + > > struct address { > >struct sockaddr_storage ss; > >int prefixlen; > > @@ -300,6 +307,7 @@ struct vmd { > > > >uint32_t vmd_nvm; > >struct vmlist*vmd_vms; > > +struct name2idlist*vmd_known; > >uint32_t vmd_nswitches; > >struct switchlist*vmd_switches; > >struct userlist*vmd_users; > > > > -- > >Ori Bernstein > > > >
Re: Reuse VM ids.
No idea if the code works yet. Hopefully I can try later. But love the idea. Mischa > On 8 Oct 2018, at 04:31, Ori Bernstein wrote: > > Keep a list of known vms, and reuse the VM IDs. This means that when using > '-L', the IP addresses of the VMs are stable. > > diff --git usr.sbin/vmd/config.c usr.sbin/vmd/config.c > index af12b790002..522bae32501 100644 > --- usr.sbin/vmd/config.c > +++ usr.sbin/vmd/config.c > @@ -61,7 +61,10 @@ config_init(struct vmd *env) >if (what & CONFIG_VMS) { >if ((env->vmd_vms = calloc(1, sizeof(*env->vmd_vms))) == NULL) >return (-1); > +if ((env->vmd_known = calloc(1, sizeof(*env->vmd_known))) == NULL) > +return (-1); >TAILQ_INIT(env->vmd_vms); > +TAILQ_INIT(env->vmd_known); >} >if (what & CONFIG_SWITCHES) { >if ((env->vmd_switches = calloc(1, > diff --git usr.sbin/vmd/vmd.c usr.sbin/vmd/vmd.c > index 18a5e0d3d5d..732691b4381 100644 > --- usr.sbin/vmd/vmd.c > +++ usr.sbin/vmd/vmd.c > @@ -1169,6 +1169,27 @@ vm_remove(struct vmd_vm *vm, const char *caller) >free(vm); > } > > +static uint32_t > +claim_vmid(const char *name) > +{ > +struct name2id *n2i = NULL; > + > +TAILQ_FOREACH(n2i, env->vmd_known, entry) > +if (strcmp(n2i->name, name) == 0) > +return n2i->id; > + > +if (++env->vmd_nvm == 0) > +fatalx("too many vms"); > +if ((n2i = calloc(1, sizeof(struct name2id))) == NULL) > +fatalx("could not alloc vm name"); > +n2i->id = env->vmd_nvm; > +if (strlcpy(n2i->name, name, sizeof(n2i->name)) >= sizeof(n2i->name)) > +fatalx("overlong vm name"); > +TAILQ_INSERT_TAIL(env->vmd_known, n2i, entry); > + > +return n2i->id; > +} > + > int > vm_register(struct privsep *ps, struct vmop_create_params *vmc, > struct vmd_vm **ret_vm, uint32_t id, uid_t uid) > @@ -1300,11 +1321,8 @@ vm_register(struct privsep *ps, struct > vmop_create_params *vmc, >vm->vm_cdrom = -1; >vm->vm_iev.ibuf.fd = -1; > > -if (++env->vmd_nvm == 0) > -fatalx("too many vms"); > - >/* Assign a new internal Id if not specified */ > -vm->vm_vmid = id == 0 ? env->vmd_nvm : id; > +vm->vm_vmid = (id == 0) ? claim_vmid(vcp->vcp_name) : id; > >log_debug("%s: registering vm %d", __func__, vm->vm_vmid); >TAILQ_INSERT_TAIL(env->vmd_vms, vm, vm_entry); > diff --git usr.sbin/vmd/vmd.h usr.sbin/vmd/vmd.h > index b7c012854e8..86fad536e59 100644 > --- usr.sbin/vmd/vmd.h > +++ usr.sbin/vmd/vmd.h > @@ -276,6 +276,13 @@ struct vmd_user { > }; > TAILQ_HEAD(userlist, vmd_user); > > +struct name2id { > +charname[VMM_MAX_NAME_LEN]; > +int32_tid; > +TAILQ_ENTRY(name2id)entry; > +}; > +TAILQ_HEAD(name2idlist, name2id); > + > struct address { >struct sockaddr_storage ss; >int prefixlen; > @@ -300,6 +307,7 @@ struct vmd { > >uint32_t vmd_nvm; >struct vmlist*vmd_vms; > +struct name2idlist*vmd_known; >uint32_t vmd_nswitches; >struct switchlist*vmd_switches; >struct userlist*vmd_users; > > -- >Ori Bernstein >