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.


New keyed stream interface
[palacios.git] / palacios / include / interfaces / vmm_keyed_stream.h
1 /*
2  * This file is part of the Palacios Virtual Machine Monitor developed
3  * by the V3VEE Project with funding from the United States National 
4  * Science Foundation and the Department of Energy.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2011, Peter Dinda <pdinda@northwestern.edu> 
11  * Copyright (c) 2011, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Peter Dinda <pdinda@northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20
21 #ifndef __VMM_KEYED_STREAM_H__
22 #define __VMM_KEYED_STREAM_H__
23
24 #include <palacios/vmm.h>
25
26
27 /*
28   A keyed stream essentially supports this:
29
30   URL => {collection of key->stream pairs}
31
32   If you open a key for reading, you get the read pointer set to its beginning.   
33   You can then make repeated reads to advance the read pointer.  You cannot seek.
34   Reading is done with a tag supplied, and we will match the requested tag
35   against the actual tag.  If they do not match, we immediately return
36   with an error.
37
38   Writing works similarly.
39
40   You cannot both read and write. 
41
42 */
43
44 /* A keyed stream and its components are opaque to palacios */
45 typedef void * v3_keyed_stream_t;
46 typedef void * v3_keyed_stream_key_t;
47
48 typedef enum {V3_KS_RD_ONLY,V3_KS_WR_ONLY,V3_KS_WR_ONLY_CREATE} v3_keyed_stream_open_t;
49
50 #ifdef __V3VEE__
51
52
53 v3_keyed_stream_t     v3_keyed_stream_open(char *url, v3_keyed_stream_open_t open_type);
54 void                  v3_keyed_stream_close(v3_keyed_stream_t stream);
55 void                  v3_keyed_stream_preallocate_hint_key(v3_keyed_stream_t stream, char *key, uint64_t size);
56 v3_keyed_stream_key_t v3_keyed_stream_open_key(v3_keyed_stream_t stream, char *key);
57 void                  v3_keyed_stream_close_key(v3_keyed_stream_t stream,  char *key);
58 sint64_t              v3_keyed_stream_write_key(v3_keyed_stream_t stream,  
59                                                 v3_keyed_stream_key_t key,
60                                                 void *tag,
61                                                 sint64_t taglen,
62                                                 void *buf, 
63                                                 sint64_t len);
64 sint64_t              v3_keyed_stream_read_key(v3_keyed_stream_t stream,
65                                                v3_keyed_stream_key_t key,
66                                                void *tag,
67                                                sint64_t taglen,
68                                                void *buf, 
69                                                sint64_t len);
70
71
72
73 /* We will kill this stuff since it's now in the interface 
74
75 #define STD_SAVE_RAW(stream,ks,x)                       \
76     do { \
77         if (sizeof((x)) != v3_keyed_stream_write_key((stream), (ks), &(x), sizeof((x)))) { \
78             v3_keyed_stream_close_key((stream),(ks)); \
79             return -1;                                \
80         } \
81     } while (0)
82
83 #define STD_LOAD_RAW(stream,ks,x)                       \
84     do {                                                                \
85         if (sizeof((x)) != v3_keyed_stream_read_key((stream), (ks), &(x), sizeof((x)))) { \
86             v3_keyed_stream_close_key((stream),(ks)); \
87             return -1;                                 \
88         } \
89     } while (0)
90 #endif
91
92 #define KSTREAM_MAGIC_COOKIE 0xabcd0123
93
94
95 #define STD_SAVE_TAGGED(stream,ks,tag,size,x)                           \
96 do {                                                                    \
97 uint32_t temp;                                                          \
98 temp=KSTREAM_MAGIC_COOKIE;                                              \
99 if (sizeof(temp) != v3_keyed_stream_write_key((stream),(ks),&temp,sizeof(temp))) { \
100     v3_keyed_stream_close_key((stream),(ks));                           \
101     return -1;                                                          \
102  }                                                                      \
103 temp=strlen(tag);                                                       \
104 if (sizeof(temp) != v3_keyed_stream_write_key((stream),(ks),&temp,sizeof(temp))) { \
105     v3_keyed_stream_close_key((stream),(ks));                           \
106     return -1;                                                          \
107  }                                                                      \
108 if (temp != v3_keyed_stream_write_key((stream),(ks),tag,temp)) {        \
109     v3_keyed_stream_close_key((stream),(ks));                           \
110     return -1;                                                          \
111  }                                                                      \
112 temp=(size);                                                            \
113 if (sizeof(temp) != v3_keyed_stream_write_key((stream),(ks),&temp,sizeof(temp))) { \
114     v3_keyed_stream_close_key((stream),(ks));                           \
115     return -1;                                                          \
116  }                                                                      \
117 if ((size) != v3_keyed_stream_write_key((stream),(ks),&(x),(size))) {   \
118     v3_keyed_stream_close_key((stream),(ks));                           \
119     return -1;                                                          \
120  }                                                                      \
121 } while (0)
122
123 #define STD_LOAD_TAGGED(stream,ks,tag,size,x)                           \
124 do {                                                                    \
125 uint32_t temp;                                                          \
126 if (sizeof(temp) != v3_keyed_stream_read_key((stream),(ks),&temp,sizeof(temp))) { \
127     v3_keyed_stream_close_key((stream),(ks));                           \
128     return -1;                                                          \
129  }                                                                      \
130 if (temp!=KSTREAM_MAGIC_COOKIE) {                                       \
131     v3_keyed_stream_close_key((stream),(ks));                           \
132     return -1;                                                          \
133 }                                                                       \
134 if (sizeof(temp) != v3_keyed_stream_read_key((stream),(ks),&temp,sizeof(temp))) { \
135     v3_keyed_stream_close_key((stream),(ks));                           \
136     return -1;                                                          \
137  }                                                                      \
138 if (strlen((tag))!=temp) {                                              \
139     v3_keyed_stream_close_key((stream),(ks));                           \
140     return -1;                                                          \
141 }                                                                       \
142 { char buf[temp+1];                                                     \
143     if (temp != v3_keyed_stream_read_key((stream),(ks),buf,temp)) {     \
144         v3_keyed_stream_close_key((stream),(ks));                       \
145         return -1;                                                      \
146     }                                                                   \
147     buf[temp]=0;                                                        \
148     if (strncasecmp(buf,tag,temp)) {                                    \
149         v3_keyed_stream_close_key((stream),(ks));                       \
150         return -1;                                                      \
151     }                                                                   \
152 }                                                                       \
153 if (sizeof(temp) != v3_keyed_stream_read_key((stream),(ks),&temp,sizeof(temp))) { \
154     v3_keyed_stream_close_key((stream),(ks));                           \
155     return -1;                                                          \
156  }                                                                      \
157 if (temp!=(size)) {                                                     \
158     v3_keyed_stream_close_key((stream),(ks));                           \
159     return -1;                                                          \
160 }                                                                       \
161 if ((size) != v3_keyed_stream_read_key((stream),(ks),&(x),(size))) {    \
162     v3_keyed_stream_close_key((stream),(ks));                           \
163     return -1;                                                          \
164  }                                                                      \
165 } while (0)
166
167 #ifdef V3_CONFIG_KEYED_STREAMS_WITH_TAGS
168 #define STD_SAVE(stream,ks,x) STD_SAVE_TAGGED(stream,ks,#x,sizeof(x),x)
169 #define STD_LOAD(stream,ks,x) STD_LOAD_TAGGED(stream,ks,#x,sizeof(x),x)
170 #else
171 #define STD_SAVE(stream,ks,x) STD_SAVE_RAW(stream,ks,x)
172 #define STD_LOAD(stream,ks,x) STD_LOAD_RAW(stream,ks,x)
173 #endif
174
175 */
176
177 #endif
178
179
180 struct v3_keyed_stream_hooks {
181     // url is meaningful only to the host implementation
182     v3_keyed_stream_t (*open)(char *url,
183                               v3_keyed_stream_open_t open_type);
184                               
185     void (*close)(v3_keyed_stream_t stream);
186
187     void (*preallocate_hint_key)(v3_keyed_stream_t stream,
188                                  char *key,
189                                  uint64_t size);
190
191     v3_keyed_stream_key_t (*open_key)(v3_keyed_stream_t stream,
192                                       char *key);
193
194     void (*close_key)(v3_keyed_stream_t stream, 
195                       v3_keyed_stream_key_t key);
196     
197
198     sint64_t (*write_key)(v3_keyed_stream_t stream,
199                           v3_keyed_stream_key_t key,
200                           void *tag,
201                           sint64_t taglen,
202                           void *buf, 
203                           sint64_t len);
204
205     sint64_t (*read_key)(v3_keyed_stream_t stream,
206                          v3_keyed_stream_key_t key,
207                          void *tag,
208                          sint64_t taglen,
209                          void *buf, 
210                          sint64_t len);
211     
212 };
213
214
215 extern void V3_Init_Keyed_Streams(struct v3_keyed_stream_hooks *hooks);
216
217 #endif