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.


Added networking section to manual
[palacios.git] / palacios / src / palacios / vmm_socket.c
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) 2008, Jack Lange <jarusl@cs.northwestern.edu> 
11  * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Jack Lange <jarusl@cs.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 #include <palacios/vmm_socket.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_debug.h>
24 #include <palacios/vmm_stddef.h>
25
26
27 struct v3_socket_hooks * sock_hooks = 0;
28
29
30 //int v3_socket_api_test(void);
31
32
33 void V3_Init_Sockets(struct v3_socket_hooks * hooks) {
34   PrintInfo("Initializing Socket Interface\n");
35   sock_hooks = hooks;
36
37   PrintDebug("V3 sockets inited\n");
38
39   v3_socket_api_test();
40   
41   return;
42 }
43
44
45
46 void v3_init_sock_set(struct v3_sock_set * sock_set) {
47   sock_set->num_socks = 0;
48   sock_set->socks = NULL;
49
50   return;
51 }
52
53
54
55
56
57 /* This should probably check if the socket is already added */
58 // adds socket to the sockset
59 void v3_set_sock(struct v3_sock_set * sock_set, V3_SOCK sock) {
60   struct v3_sock_entry * new_entry = V3_Malloc(sizeof(struct v3_sock_entry));
61
62   new_entry->sock = sock;
63   new_entry->is_set = 0;
64
65   if (sock_set->socks) {
66     new_entry->next = sock_set->socks;
67   }
68
69   sock_set->socks = new_entry;
70
71   sock_set->num_socks++;
72 }
73
74
75 // deletes socket from sockset
76 void v3_clr_sock(struct v3_sock_set * sock_set, V3_SOCK sock) {
77   struct v3_sock_entry * iter, * back_ptr;
78
79   iter = sock_set->socks;
80   back_ptr = NULL;
81
82   v3_foreach_sock(sock_set, iter) {
83     if (iter->sock == sock) {
84       if (back_ptr == NULL) {
85         sock_set->socks = iter->next;
86       } else {
87         back_ptr->next = iter->next;
88       }
89
90       V3_Free(iter);
91
92       sock_set->num_socks--;
93       break;
94     }
95
96     back_ptr = iter;
97   }
98 }
99
100 // checks is_set vairable 
101 int v3_isset_sock(struct v3_sock_set * sock_set, V3_SOCK sock) {
102   struct v3_sock_entry * iter;
103
104   v3_foreach_sock(sock_set, iter) {
105     if (iter->sock == sock) {
106       return iter->is_set;
107     }
108   }
109   return -1;
110 }
111
112
113 // clears all is_set variables.
114 void v3_zero_sockset(struct v3_sock_set * sock_set) {
115   struct v3_sock_entry * iter;
116   v3_foreach_sock(sock_set, iter) {
117     iter->is_set = 0;
118   }
119 }
120
121 #if 1
122
123 int
124 v3_socket_api_test(void)
125 {
126         unsigned int port;
127         char buf[1024];
128         int rc = 0;
129         V3_SOCK sock; 
130         V3_SOCK client;
131         unsigned int remote_ip;
132         
133         PrintDebug("\nIn Palacios: Test V3_Socket Macros\n");
134         sock = V3_Create_TCP_Socket();
135         if( ((int)sock) < 0 ){
136                 PrintDebug( "ERROR: tcp_socket() failed!\n");
137                 return -1;
138         }
139
140         port = 80;
141
142         if( V3_Bind_Socket(sock, port) < 0){
143                 PrintDebug("bind error\n");
144                 return -1;
145         }
146
147         if( V3_Listen_Socket(sock, 1) < 0) {
148                 PrintDebug("listen error\n" );
149                 return -1;
150         }
151
152         PrintDebug( "Going into mainloop: server listening on port %d\n", port);
153
154         client = V3_Accept_Socket(sock, &remote_ip, &port);
155
156         PrintDebug(" New connection from %d port: %d\n", remote_ip, port);
157              
158         V3_Send(client, "Welcome!\n", 9);
159
160         while(1)
161         {               
162              V3_Send(client, buf, rc);
163              rc = V3_Recv(client, buf, sizeof(buf)-1);
164              if( rc <= 0 ){
165                                 PrintDebug( "Closed connection\n");
166                                 V3_Close_Socket(client);
167                                 break;
168              }
169
170              buf[rc] = '\0';
171
172              PrintDebug( "Read %d bytes: '%s'\n", rc, buf);
173          }
174
175         PrintDebug("TEST END: Sockets API\n");
176         return 0;
177 }
178
179 #endif
180
181
182 #if 0
183
184 static int
185 socket_api_test(void)
186 {
187         unsigned int port;
188         char buf[1024];
189         int rc = 0;
190         V3_SOCK sock; 
191         V3_SOCK client;
192         unsigned int remote_ip;
193         
194         PrintDebug("\nIn Palacios: TEST BEGIN: Sockets API\n");
195         sock = sock_hooks->tcp_socket(0, 0, 0);
196         if( sock == NULL ){
197                 PrintDebug( "ERROR: tcp_socket() failed!\n");
198                 return -1;
199         }
200
201         port = 80;
202
203         if( sock_hooks->bind_socket(sock, port) < 0){
204                 PrintDebug("bind error\n");
205                 return -1;
206         }
207
208         if( sock_hooks->listen(sock, 1) < 0) {
209                 PrintDebug("listen error\n" );
210                 return -1;
211         }
212
213         PrintDebug( "Going into mainloop: server listening on port %d\n", port);
214
215         client = sock_hooks->accept(sock, &remote_ip , &port);
216
217         PrintDebug(" New connection from %d port: %d\n", remote_ip, port);
218              
219         sock_hooks->send(client, "Welcome!\n", 9);
220
221         while(1)
222         {               
223              sock_hooks->send(client, buf, rc);
224             rc = sock_hooks->recv(client, buf, sizeof(buf)-1);
225              if( rc <= 0 ){
226                                 PrintDebug( "Closed connection\n");
227                                 sock_hooks->close(client);
228                                 break;
229              }
230
231              buf[rc] = '\0';
232
233              PrintDebug( "Read %d bytes: '%s'\n", rc, buf);
234          }
235
236         PrintDebug("TEST END: Sockets API\n");
237         return 0;
238 }
239
240 #endif