Skip to content

Add support for establishing a Xen event channel from outside LibVMI - #450

Merged
tklengyel merged 1 commit into
libvmi:masterfrom
flyn-org:get_fd
Jul 6, 2017
Merged

tklengyel merged 1 commit into
libvmi:masterfrom
flyn-org:get_fd

Conversation

@flyn-org

@flyn-org flyn-org commented Feb 5, 2017

Copy link
Copy Markdown
Contributor

Signed-off-by: W. Michael Petullo mike@flyn.org

@libvmi-jenkins

Copy link
Copy Markdown

Can one of the admins verify this patch?

@flyn-org

flyn-org commented Feb 5, 2017 •

Copy link
Copy Markdown
Contributor Author

This is the first attempt at making it possible to integrate libvmi into a select/poll event loop. This implementation covers Xen 4.6/4.7 and 4.8, but I have not yet tested 4.8. I have not attempted to cover KVM because I do not have experience with KVM.

For example (the new function, used below, is vmi_event_get_fd):

static gboolean
gt_handle_vmi_event(GIOChannel *chan, GIOCondition condition, gpointer user_data)
{
status_t status;
GtLoop *loop = user_data;

    switch (condition) {
    case G_IO_IN:
            status = vmi_events_listen(loop->vmi, 0);
            if (status != VMI_SUCCESS) {
                    fprintf(stderr, "error waiting for events\n");
                    gt_running = FALSE;
            }
            break;
    default:
            fprintf(stderr, "error reading fd\n");
            break;
    }

    if (!gt_running) {
            g_main_loop_quit(loop->g_main_loop);
    }

    return gt_running;

}

int
main(int argc, char *argv[])
{
...
vmi_fd = vmi_event_get_fd(vmi);
channel_vmi = g_io_channel_unix_new(vmi_fd);
gt_loop_add_watch(channel_vmi, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, gt_handle_vmi_event, loop);
vmi_resume_vm(loop->vmi);
g_main_loop_run(loop->g_main_loop);
...
}

@tklengyel

Copy link
Copy Markdown
Contributor

Exposing the file descriptor through LibVMI is a hypervisor specific feature-creep into the LibVMI API. As such I'm not a fan of this approach.

I would suggest that if your application wants to access the file-descriptor to have it setup the events channels using the Xen API, then use the new init system in PR #435 (https://github.com/libvmi/libvmi/pull/435/files#diff-0af4637539ddf2fd76051b0ac977e009L620) to pass in that information to LibVMI. The new init system has been specifically setup to address cases where a user would want to pass extra information for the hypervisor driver, like handles and descriptors it wouldn't be able or wouldn't want to replicate.

@smaresca

smaresca commented Feb 15, 2017 via email

Copy link
Copy Markdown
Member

@tklengyel

Copy link
Copy Markdown
Contributor

@smaresca: adding an extra callback interface for fd changes is also very hypervisor specific, even if we abstract away the headers/errnos. That API would be unlikely to be used for other hypervisors. So I'm not really a fan of that approach either. IMHO having the user pass in the fd for the driver during init is a much cleaner solution, using an API that was specifically made for that type of thing.

@flyn-org

Copy link
Copy Markdown
Contributor Author

I think @tklengyel's approach would be fine. Just so I understand, this particular capability is not yet in the init_rework branch; is this correct? If so, would you be willing to add this functionality to the branch? If not, could you provide a simple example of what this would look like. (In the latter case, I must be overlooking something in the branch's code.)

@drakvuf-jenkins

drakvuf-jenkins commented Feb 16, 2017 via email

Copy link
Copy Markdown

@tklengyel

Copy link
Copy Markdown
Contributor

(That was me before just replied via the first email that hit my inbox.)

@flyn-org

Copy link
Copy Markdown
Contributor Author

Okay, so the struct you describe might have an "fd" field among others. Perhaps if the field was not equal to -1, then the Xen driver would not initialize Xen itself, but would instead use the file descriptor fd. We would be very happy with this, and I agree it keeps the abstraction provided by libvmi at the right level. The application could take responsibility for peering down into the Xen level before passing things along to libvmi, but libvmi would not be responsible for itself exporting any of the Xen details back out.

We are hoping to release a prototype of the system that would use this in April. Might we be able to make progress on this before then? Or, should we plan to ship a locally-modified libvmi for the time being? I do not intend to apply any pressure; I just want to be able to plan our activities leading up to the prototype.

@tklengyel

Copy link
Copy Markdown
Contributor

Absolutely. The init rework PR will get merged shortly so you can treat it as semi-stable API that we will be migrating to. So you can start using it and basing your work on it. Also, feel free to open a PR for your work if you would want early feedback even before the init rework PR is merged.

@libvmi-jenkins

Copy link
Copy Markdown

Can one of the admins verify this patch?

@flyn-org

flyn-org commented Jul 5, 2017

Copy link
Copy Markdown
Contributor Author

I just pushed a prototype which explores my understanding of @tklengyel's design. I added a new, example VMI_INIT_XEN_EVTCHN flag and a vmi_init_data_t structure. A program can set fields in the vmi_init_data_t structure and pass it to vmi_init() along with flags that indicate which fields are set.

I was able to use this to permit guestrace access to the Xen event channel file descriptor.

Comment thread libvmi/libvmi.h Outdated
void* domain,
uint32_t init_flags,
void *init_data,
vmi_init_data_t *init_data,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks to me like it would be fine to keep this as a void* in the API. My reasoning with having this as a generic pointer was that the drivers can cast it to whatever type they need it as, and as long as its not a complex structure that needs to be passed in, there is no need for a struct definition in the public header.

@tklengyel

Copy link
Copy Markdown
Contributor

This is OK to test.

@flyn-org

flyn-org commented Jul 6, 2017

Copy link
Copy Markdown
Contributor Author

I removed the vmi_init_data_t structure.

@tklengyel

Copy link
Copy Markdown
Contributor

Looks good to me! One last thing, once LibVMI closes it will close this fd as well that was passed in. I think the public header should point this out so there are no surprises to anyone who later use this feature.

@flyn-org flyn-org changed the title Added accessor function to get underlying VMM file descriptor Add support for establishing a Xen event channel from outside LibVMI Jul 6, 2017
This gives an application access to the Xen event channel associated with
an introspected domain. For example, an application might use this to
integrate the event channel's file descriptor into a select or poll loop.

Signed-off-by: W. Michael Petullo <mike@flyn.org>
@flyn-org

flyn-org commented Jul 6, 2017

Copy link
Copy Markdown
Contributor Author

Just pushed a new version which adds some documentation and provides a more descriptive commit log.

@tklengyel

Copy link
Copy Markdown
Contributor

Great, thanks!

@tklengyel
tklengyel merged commit 25c536c into libvmi:master Jul 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants