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.


e79f093d974a4e095b215d25ee81f3fab0dcb81d
[palacios.git] / linux_module / palacios-stream.c
1
2 /* 
3  * VM specific Controls
4  * (c) Lei Xia, 2010
5  */
6 #include <linux/errno.h>
7 #include <linux/percpu.h>
8 #include <linux/sched.h>
9
10 #include <interfaces/vmm_stream.h>
11 #include "palacios-stream.h"
12
13 static struct list_head global_streams;
14
15 int stream_enqueue(struct stream_buffer * stream, char * buf, int len) {
16     int bytes = 0;
17
18     bytes = ringbuf_write(stream->buf, buf, len);
19
20     return bytes;
21 }
22
23
24 int stream_dequeue(struct stream_buffer * stream, char * buf, int len) {
25     int bytes = 0;
26
27     bytes = ringbuf_read(stream->buf, buf, len);
28
29     return bytes;
30 }
31
32 int stream_datalen(struct stream_buffer * stream){
33     return ringbuf_data_len(stream->buf);
34 }
35
36
37 struct stream_buffer * find_stream_by_name(struct v3_guest * guest, const char * name) {
38     struct stream_buffer * stream = NULL;
39     struct list_head * stream_list = NULL;
40
41     if (guest == NULL) {
42         stream_list = &global_streams;
43     } else {
44         stream_list = &(guest->streams);
45     }
46
47     list_for_each_entry(stream,  stream_list, stream_node) {
48         if (strncmp(stream->name, name, STREAM_NAME_LEN) == 0) {
49             return stream;
50         }
51     }
52
53     return NULL;
54 }
55
56
57 static void * palacios_stream_open(const char * name, void * private_data) {
58     struct v3_guest * guest = (struct v3_guest *)private_data;
59     struct stream_buffer * stream = NULL;
60
61     if (find_stream_by_name(guest, name) != NULL) {
62         printk("Stream already exists\n");
63         return NULL;
64     }
65
66     stream = kmalloc(sizeof(struct stream_buffer), GFP_KERNEL);
67         
68     stream->buf = create_ringbuf(STREAM_BUF_SIZE);
69     stream->guest = guest;
70
71     strncpy(stream->name, name, STREAM_NAME_LEN - 1);
72
73     init_waitqueue_head(&(stream->intr_queue));
74     spin_lock_init(&(stream->lock));
75
76     if (guest == NULL) {
77         list_add(&(stream->stream_node), &(global_streams));
78     } else {
79         list_add(&(stream->stream_node), &(guest->streams));
80     } 
81
82     return stream;
83 }
84
85
86 static int palacios_stream_write(void * stream_ptr, char * buf, int len) {
87     struct stream_buffer * stream = (struct stream_buffer *)stream_ptr;
88     int ret = 0;
89
90     ret = stream_enqueue(stream, buf, len);
91
92     if (ret > 0) {
93         wake_up_interruptible(&(stream->intr_queue));
94     }
95
96     return ret;
97 }
98
99
100 static void palacios_stream_close(void * stream_ptr) {
101     struct stream_buffer * stream = (struct stream_buffer *)stream_ptr;
102
103     free_ringbuf(stream->buf);
104     list_del(&(stream->stream_node));
105     kfree(stream);
106
107 }
108
109 struct v3_stream_hooks palacios_stream_hooks = {
110     .open = palacios_stream_open,
111     .write = palacios_stream_write,
112     .close = palacios_stream_close,
113 };
114
115
116 void palacios_init_stream() {
117     INIT_LIST_HEAD(&(global_streams));
118     V3_Init_Stream(&palacios_stream_hooks);
119 }
120
121
122 void palacios_deinit_stream() {
123     if (!list_empty(&(global_streams))) {
124         printk("Error removing module with open streams\n");
125     }
126 }