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.


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