Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Merge branch 'devel' of newskysaw.cs.northwestern.edu:/home/palacios/palacios into...
[palacios.git] / linux_usr / v3_user_host_dev_example.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <string.h>
6 #include <sys/select.h>
7 #include <malloc.h>
8
9 #include "v3_user_host_dev.h"
10
11 void usage()
12 {
13     fprintf(stderr,"v3_host_dev_example /dev/v3-vm0 user:mydev busywait|select\n");
14 }
15
16
17 int do_work(struct palacios_host_dev_host_request_response *req, 
18             struct palacios_host_dev_host_request_response **resp)
19 {
20     uint64_t datasize;
21     
22     //
23     //
24     // Process request here, perhaps calling these functions:
25     //
26     // uint64_t v3_user_host_dev_read_guest_mem(int devfd, void *gpa, void *dest, uint64_t len);
27     // uint64_t v3_user_host_dev_write_guest_mem(int devfd, void *gpa, void *src, uint64_t len);
28     // int      v3_user_host_dev_inject_guest_irq(int devfd, uint8_t irq);
29     //
30     // determine datasize - # bytes to include in response
31     //
32     // now built a response
33     *resp = malloc(sizeof(struct palacios_host_dev_host_request_response) + datasize);
34     (*resp)->data_len = sizeof(struct palacios_host_dev_host_request_response) + datasize;
35
36     //
37     // Fill out the fields of the response - notice that there are six kinds of things to response to:
38     //   - read/write device port
39     //   - read/write device mem
40     //   - read/write device configuration space
41
42     return 0;
43 }
44
45 int main(int argc, char *argv[])
46 {
47     int devfd;
48     int mode=0;
49     char *vm, *url;
50
51     if (argc!=4) { 
52         usage();
53         exit(-1);
54     }
55     
56     vm=argv[1];
57     url=argv[2];
58     mode = argv[3][0]=='s';
59
60     fprintf(stderr,"Attempting to rendezvous with host device %s on vm %s\n", url, vm);
61     
62     if ((devfd = v3_user_host_dev_rendezvous(vm,url))<0) { 
63         perror("failed to rendezvous");
64         exit(-1);
65     }
66     
67     fprintf(stderr,"Rendezvous succeeded, I will now operate in %s mode\n", mode==0 ? "busywait" : "select");
68     
69     if (mode==0) { 
70         //busywait
71
72         struct palacios_host_dev_host_request_response *req;
73         struct palacios_host_dev_host_request_response *resp;
74         uint64_t datasize;
75
76         while (1) { 
77             while (!(v3_user_host_dev_have_request(devfd))) { 
78             }
79             v3_user_host_dev_pull_request(devfd, &req);
80
81             do_work(req, &resp);
82             
83             v3_user_host_dev_push_response(devfd, resp);
84
85             free(resp);
86             free(req);
87         }
88     } else {
89
90         struct palacios_host_dev_host_request_response *req;
91         struct palacios_host_dev_host_request_response *resp;
92         uint64_t datasize;
93         fd_set   readset;
94         int rc;
95
96         // select-based operation so that you can wait for multiple things
97         
98         while (1) { 
99             FD_ZERO(&readset);
100             FD_SET(devfd,&readset);
101
102             rc = select(devfd+1, &readset, 0, 0, 0);  // pick whatever you want to select on, just include devfd
103
104             if (rc>0) { 
105                 if (FD_ISSET(devfd,&readset)) { 
106                     // a request is read for us!
107                     v3_user_host_dev_pull_request(devfd, &req);
108
109                     do_work(req, &resp);
110                     
111                     v3_user_host_dev_push_response(devfd, resp);
112                     
113                     free(resp);
114                     free(req);
115                 }
116             }
117         }
118     }
119
120     v3_user_host_dev_depart(devfd);
121
122     return 0;
123                     
124 }