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.


4f8144e0ccdd96d9dc97f4921f6bfc0d6d0a954f
[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     if (!*resp) {
35         fprintf(stderr, "ERROR: could not allocate memory for response\n");
36         return -1;
37     }
38     (*resp)->data_len = sizeof(struct palacios_host_dev_host_request_response) + datasize;
39
40     //
41     // Fill out the fields of the response - notice that there are six kinds of things to response to:
42     //   - read/write device port
43     //   - read/write device mem
44     //   - read/write device configuration space
45
46     return 0;
47 }
48
49 int main(int argc, char *argv[])
50 {
51     int devfd;
52     int mode=0;
53     char *vm, *url;
54
55     if (argc!=4) { 
56         usage();
57         exit(-1);
58     }
59     
60     vm=argv[1];
61     url=argv[2];
62     mode = argv[3][0]=='s';
63
64     fprintf(stderr,"Attempting to rendezvous with host device %s on vm %s\n", url, vm);
65     
66     if ((devfd = v3_user_host_dev_rendezvous(vm,url))<0) { 
67         perror("failed to rendezvous");
68         exit(-1);
69     }
70     
71     fprintf(stderr,"Rendezvous succeeded, I will now operate in %s mode\n", mode==0 ? "busywait" : "select");
72     
73     if (mode==0) { 
74         //busywait
75
76         struct palacios_host_dev_host_request_response *req;
77         struct palacios_host_dev_host_request_response *resp;
78         uint64_t datasize;
79
80         while (1) { 
81             while (!(v3_user_host_dev_have_request(devfd))) { 
82             }
83             v3_user_host_dev_pull_request(devfd, &req);
84
85             do_work(req, &resp);
86             
87             v3_user_host_dev_push_response(devfd, resp);
88
89             free(resp);
90             free(req);
91         }
92     } else {
93
94         struct palacios_host_dev_host_request_response *req;
95         struct palacios_host_dev_host_request_response *resp;
96         uint64_t datasize;
97         fd_set   readset;
98         int rc;
99
100         // select-based operation so that you can wait for multiple things
101         
102         while (1) { 
103             FD_ZERO(&readset);
104             FD_SET(devfd,&readset);
105
106             rc = select(devfd+1, &readset, 0, 0, 0);  // pick whatever you want to select on, just include devfd
107
108             if (rc>0) { 
109                 if (FD_ISSET(devfd,&readset)) { 
110                     // a request is read for us!
111                     v3_user_host_dev_pull_request(devfd, &req);
112
113                     do_work(req, &resp);
114                     
115                     v3_user_host_dev_push_response(devfd, resp);
116                     
117                     free(resp);
118                     free(req);
119                 }
120             }
121         }
122     }
123
124     v3_user_host_dev_depart(devfd);
125
126     return 0;
127                     
128 }