2 #include "palacios-hashtable.h"
3 #include "linux-exts.h"
5 #define sint64_t int64_t
6 #include <interfaces/vmm_keyed_stream.h>
9 Streams are stored in a hash table
10 The values for this hash table are hash tables associted with
11 each stream. A keyed stream for a "mem:" stream is
12 an instance of the structure given here
15 #define DEF_NUM_STREAMS 16
16 #define DEF_NUM_KEYS 128
26 static struct mem_stream *create_mem_stream(void)
28 struct mem_stream *m = kmalloc(sizeof(struct mem_stream),GFP_KERNEL);
34 m->data = kmalloc(DEF_SIZE,GFP_KERNEL);
48 static void destroy_mem_stream(struct mem_stream *m)
59 static int expand_mem_stream(struct mem_stream *m, uint32_t new_size)
61 void *data = kmalloc(new_size,GFP_KERNEL);
68 nc = (new_size<m->data_max) ? new_size : m->data_max;
70 memcpy(data,m->data,nc);
76 if (m->size<m->data_max) {
83 static uint32_t write_mem_stream(struct mem_stream *m,
87 if ((m->ptr + len) > m->size) {
88 if (expand_mem_stream(m,m->ptr + len)) {
92 memcpy(m->data+m->ptr,data,len);
100 static uint32_t read_mem_stream(struct mem_stream *m,
104 if ((m->ptr + len) > m->data_max) {
107 memcpy(data,m->data+m->ptr,len);
115 static void reset_mem_stream(struct mem_stream *m)
121 static inline uint_t hash_func(addr_t key)
123 return palacios_hash_buffer((uchar_t*)key,strlen((uchar_t*)key));
126 static inline int hash_comp(addr_t k1, addr_t k2)
128 return strcasecmp((char*)k1,(char*)k2)==0;
132 // This stores all the streams
133 static struct hashtable *streams=0;
136 static v3_keyed_stream_t open_stream(char *url,
137 v3_keyed_stream_open_t ot)
139 if (strncasecmp(url,"mem:",4)) {
140 printk("Only in-memory streams are currently supported\n");
147 return (v3_keyed_stream_t) palacios_htable_search(streams,(addr_t)(url+4));
149 case V3_KS_WR_ONLY_CREATE: {
150 struct hashtable *s = (struct hashtable *) palacios_htable_search(streams,(addr_t)(url+4));
153 s = palacios_create_htable(DEF_NUM_KEYS,hash_func,hash_comp);
155 printk("Cannot allocate in-memory keyed stream %s\n",url);
158 if (!palacios_htable_insert(streams,(addr_t)(url+4),(addr_t)s)) {
159 printk("Cannot insert in-memory keyed stream %s\n",url);
176 static void close_stream(v3_keyed_stream_t stream)
182 static v3_keyed_stream_key_t open_key(v3_keyed_stream_t stream,
185 struct hashtable *s = (struct hashtable *) stream;
187 struct mem_stream *m;
189 m = (struct mem_stream *) palacios_htable_search(s,(addr_t)key);
192 m = create_mem_stream();
195 printk("Cannot allocate keyed stream for key %s\n",key);
199 if (!palacios_htable_insert(s,(addr_t)key,(addr_t)m)) {
200 printk("Cannot insert keyed stream for key %s\n",key);
201 destroy_mem_stream(m);
211 static void close_key(v3_keyed_stream_t stream,
212 v3_keyed_stream_key_t key)
218 static sint64_t write_key(v3_keyed_stream_t stream,
219 v3_keyed_stream_key_t key,
223 struct mem_stream *m = (struct mem_stream *) key;
231 mylen = (uint32_t) len;
233 writelen=write_mem_stream(m,buf,mylen);
235 if (writelen!=mylen) {
236 printk("Failed to write all data for key\n");
239 return (sint64_t)writelen;
243 static sint64_t read_key(v3_keyed_stream_t stream,
244 v3_keyed_stream_key_t key,
248 struct mem_stream *m = (struct mem_stream *) key;
256 mylen = (uint32_t) len;
258 readlen=read_mem_stream(m,buf,mylen);
260 if (readlen!=mylen) {
261 printk("Failed to read all data for key\n");
264 return (sint64_t)readlen;
268 static struct v3_keyed_stream_hooks hooks = {
270 .close = close_stream,
271 .open_key = open_key,
272 .close_key = close_key,
273 .read_key = read_key,
274 .write_key = write_key
278 static int init_keyed_streams( void )
280 streams = palacios_create_htable(DEF_NUM_STREAMS,hash_func,hash_comp);
283 printk("Failed to allocated stream pool\n");
287 V3_Init_Keyed_Streams(&hooks);
293 static int deinit_keyed_streams( void )
295 printk("DEINIT OF PALACIOS KEYED STREAMS NOT IMPLEMENTED - WE HAVE JUST LEAKED MEMORY!\n");
300 static struct linux_ext key_stream_ext = {
301 .name = "KEYED_STREAM_INTERFACE",
302 .init = init_keyed_streams,
303 .deinit = deinit_keyed_streams,
309 register_extension(&key_stream_ext);