6 #include <sys/select.h>
9 #define sint64_t int64_t
11 #include "v3_user_keyed_stream.h"
15 fprintf(stderr,"v3_user_keyed_stream_example /dev/v3-vm0 user:mystreamtype:mystream busywait|select\n");
19 int do_work(struct palacios_user_keyed_stream_op *req,
20 struct palacios_user_keyed_stream_op **resp)
26 // Process request here
28 // req->len : total structure length
29 // req->type : request type (currently open/close key and read/write key
31 // req->user_key : the opaque key previously provided by you by an open key
32 // req->buf_len : length of data
33 // req->data_off : start of data within the buffer
34 // buf[0..data_off-1] is a tag (for read or write key)
35 // buf[data_off..buf_len-1] is data (for write key)
37 // open key: key name (open key)
39 // write key: tag and data
41 // now built a response
42 *resp = malloc(sizeof(struct palacios_user_keyed_stream_op) + datasize);
44 fprintf(stderr, "ERROR: could not allocate space for response\n");
48 (*resp)->len = sizeof(struct palacios_user_keyed_stream_op) + datasize;
49 (*resp)->buf_len = datasize;
50 (*resp)->data_off = 0; // always the case for a response
51 (*resp)->type = req->type;
52 (*resp)->user_key = req->user_key;
57 // resp->len : total structure length
58 // resp->type : response type - must match the request
59 // resp->xfer : contains the size of data read or written (in read key or write key)
60 // resp->user_key : unused
61 // resp->buf_len : length of data following
62 // resp->data_off : offset of the data in the buffer
63 // resp->buf : buffer (contains the data (read key))
71 int main(int argc, char *argv[])
84 mode = argv[3][0]=='s';
86 // The URL should begin with user:
87 // the remainder can be used to demultiplex internally
88 // for example user:file:foo might refer to a user-side file-based implementation
91 if (strncmp(url,"user:",5)) {
92 fprintf(stderr, "URL %s is not a user: url\n",url);
96 fprintf(stderr,"Attempting to attach to vm %s as url %s\n", vm, url);
98 if ((devfd = v3_user_keyed_stream_attach(vm,url))<0) {
99 perror("failed to attach");
103 fprintf(stderr,"Attachment succeeded, I will now operate in %s mode\n", mode==0 ? "busywait" : "select");
108 struct palacios_user_keyed_stream_op *req;
109 struct palacios_user_keyed_stream_op *resp;
113 while (!(v3_user_keyed_stream_have_request(devfd))) {
115 v3_user_keyed_stream_pull_request(devfd, &req);
119 v3_user_keyed_stream_push_response(devfd, resp);
126 struct palacios_user_keyed_stream_op *req;
127 struct palacios_user_keyed_stream_op *resp;
132 // select-based operation so that you can wait for multiple things
136 FD_SET(devfd,&readset);
138 rc = select(devfd+1, &readset, 0, 0, 0); // pick whatever you want to select on, just include devfd
141 if (FD_ISSET(devfd,&readset)) {
142 // a request is read for us!
143 v3_user_keyed_stream_pull_request(devfd, &req);
147 v3_user_keyed_stream_push_response(devfd, resp);
156 v3_user_keyed_stream_detatch(devfd);