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.


Finish the VMM Socket Layer
[palacios.git] / palacios / include / palacios / vmm_socket.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) 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 #ifndef __VMM_SOCKET_H__
22 #define __VMM_SOCKET_H__
23
24 #include <palacios/vmm.h>
25
26 #define V3_SOCK void *
27
28 #ifdef __V3VEE__
29
30 #define V3_Create_UDP_Socket() ({                       \
31       extern struct v3_socket_hooks * sock_hooks;       \
32       V3_SOCK sock = 0;                                 \
33       if ((sock_hooks) && (sock_hooks)->udp_socket) {   \
34         sock = (sock_hooks)->udp_socket(0,0);           \
35       }                                                 \
36       sock;                                             \
37     })
38
39
40
41 #define V3_Create_TCP_Socket() ({                       \
42       extern struct v3_socket_hooks * sock_hooks;       \
43       V3_SOCK sock = 0;                                 \
44       if ((sock_hooks) && (sock_hooks)->tcp_socket) {   \
45         sock = (sock_hooks)->tcp_socket(0,0,0);         \
46       }                                                 \
47       sock;                                             \
48     })
49
50
51 #define V3_Close_Socket(sock) \
52   do {                                                  \
53     extern struct v3_socket_hooks * sock_hooks;         \
54     if ((sock_hooks) && (sock_hooks)->close) {          \
55       (sock_hooks)->close(sock);                        \
56     }                                                   \
57   } while (0);
58
59
60
61 #define V3_Bind_Socket(sock, port) ({                           \
62       extern struct v3_socket_hooks * sock_hooks;               \
63       int ret = -1;                                             \
64       if ((sock_hooks) && (sock_hooks)->bind) {                 \
65         ret = (sock_hooks)->bind(sock, port);                   \
66       }                                                         \
67       ret;                                                      \
68     })
69
70
71 #define V3_Accept_Socket(sock, ip_ptr) ({                       \
72       extern struct v3_socket_hooks * sock_hooks;               \
73       int ret = -1;                                             \
74       if ((sock_hooks) && (sock_hooks)->accept) {               \
75         ret = (sock_hooks)->accept(sock, ip_ptr);               \
76       }                                                         \
77       ret;                                                      \
78     })
79
80
81 #define V3_Select_Socket(rset,wset,eset,tv) ({                  \
82       extern struct v3_socket_hooks * sock_hooks;               \
83       int ret = -1;                                             \
84       if ((sock_hooks) && (sock_hooks)->select) {               \
85         ret = (sock_hooks)->select(rset, wset, eset, tv);       \
86       }                                                         \
87       ret;                                                      \
88     })
89
90
91
92 #define V3_Connect_To_IP(sock, ip, port) ({                     \
93       extern struct v3_socket_hooks * sock_hooks;               \
94       int ret = -1;                                             \
95       if ((sock_hooks) && (sock_hooks)->connect_to_ip) {        \
96         ret = (sock_hooks)->connect_to_ip(sock, ip, port);      \
97       }                                                         \
98       ret;                                                      \
99     })
100
101
102 #define V3_Connect_To_Host(sock, hostname, port) ({                     \
103       extern struct v3_socket_hooks * sock_hooks;                       \
104       int ret = -1;                                                     \
105       if ((sock_hooks) && (sock_hooks)->connect_to_host) {              \
106         ret = (sock_hooks)->connect_to_host(sock, hostname, port);      \
107       }                                                                 \
108       ret;                                                              \
109     })
110
111
112 #define V3_Send(sock, buf, len) ({                              \
113       extern struct v3_socket_hooks * sock_hooks;               \
114       int ret = -1;                                             \
115       if ((sock_hooks) && (sock_hooks)->send) {                 \
116         ret = (sock_hooks)->send(sock, buf, len);               \
117       }                                                         \
118       ret;                                                      \
119     })
120
121 #define V3_Recv(sock, buf, len) ({                              \
122       extern struct v3_socket_hooks * sock_hooks;               \
123       int ret = -1;                                             \
124       if ((sock_hooks) && (sock_hooks)->recv) {                 \
125         ret = (sock_hooks)->recv(sock, buf, len);               \
126       }                                                         \
127       ret;                                                      \
128     })
129
130 #define V3_SendTo_Host(sock, hostname, port, buf, len) ({               \
131       extern struct v3_socket_hooks * sock_hooks;                       \
132       int ret = -1;                                                     \
133       if ((sock_hooks) && (sock_hooks)->sendto_host) {                  \
134         ret = (sock_hooks)->sendto_host(sock, hostname, port, buf, len); \
135       }                                                                 \
136       ret;                                                              \
137     })
138
139
140 #define V3_SendTo_IP(sock, ip, port, buf, len) ({                       \
141       extern struct v3_socket_hooks * sock_hooks;                       \
142       int ret = -1;                                                     \
143       if ((sock_hooks) && (sock_hooks)->sendto_ip) {                    \
144         ret = (sock_hooks)->sendto_ip(sock, ip, port, buf, len);        \
145       }                                                                 \
146       ret;                                                              \
147     })
148
149
150 #define V3_RecvFrom_Host(sock, hostname, port, buf, len) ({             \
151       extern struct v3_socket_hooks * sock_hooks;                       \
152       int ret = -1;                                                     \
153       if ((sock_hooks) && (sock_hooks)->recvfrom_host) {                \
154         ret = (sock_hooks)->recvfrom_host(sock, hostname, port, buf, len); \
155       }                                                                 \
156       ret;                                                              \
157     })
158
159
160 #define V3_RecvFrom_IP(sock, ip, port, buf, len) ({                     \
161       extern struct v3_socket_hooks * sock_hooks;                       \
162       int ret = -1;                                                     \
163       if ((sock_hooks) && (sock_hooks)->recvfrom_ip) {                  \
164         ret = (sock_hooks)->recvfrom_ip(sock, ip, port, buf, len);      \
165       }                                                                 \
166       ret;                                                              \
167     })
168
169
170 #endif
171
172
173 struct v3_timeval {
174   long    tv_sec;         /* seconds */
175   long    tv_usec;        /* and microseconds */
176 };
177
178 struct v3_sock_entry {
179   V3_SOCK sock;
180   unsigned int is_set;
181   struct v3_sock_entry * next;
182 };
183
184 struct v3_sock_set {
185   unsigned int num_socks;
186   struct v3_sock_entry * socks;
187 };
188
189
190 void v3_init_sock_set(struct v3_sock_set * sock_set);
191
192 void v3_set_sock(struct v3_sock_set * sock_set, V3_SOCK sock); // adds socket to the sockset
193 void v3_clr_sock(struct v3_sock_set * sock_set, V3_SOCK sock); // deletes socket from sockset
194 int v3_isset_sock(struct v3_sock_set * sock_set, V3_SOCK sock);  // checks is_set vairable 
195 void v3_zero_sockset(struct v3_sock_set * sock_set);    // clears all is_set variables.
196
197
198
199 #define v3_foreach_sock(/* (struct v3_sock_set *) */ sock_set, /* (struct v3_sock_entry *) */ iter) \
200        for (iter = sock_set->socks; iter != NULL; iter = iter->next)
201
202
203
204 struct v3_socket_hooks {
205   // Socket creation routines
206 xsxsxsxsxs  V3_SOCK (*tcp_socket)(const int bufsize, const int nodelay, const int nonblocking);
207   V3_SOCK (*udp_socket)(const int bufsize, const int nonblocking);
208
209   // Socket Destruction
210   void (*close)(V3_SOCK sock);
211
212   // Network Server Calls
213   int (*bind_socket)(const V3_SOCK sock, const int port);
214
215   int (*listen)(const V3_SOCK sock, int backlog);
216   
217   V3_SOCK (*accept)(const V3_SOCK const sock, unsigned int * remote_ip, unsigned int * port);
218   // This going to suck
219   int (*select)(struct v3_sock_set * rset, \
220                 struct v3_sock_set * wset, \
221                 struct v3_sock_set * eset, \
222                 struct v3_timeval tv);
223
224   // Connect calls
225   int (*connect_to_ip)(const V3_SOCK sock, const int hostip, const int port);
226   int (*connect_to_host)(const V3_SOCK sock, const char * hostname, const int port);
227
228   // TCP Data Transfer
229   int (*send)(const V3_SOCK sock, const char * buf, const int len);
230   int (*recv)(const V3_SOCK sock, char * buf, const int len);
231   
232   // UDP Data Transfer
233   int (*sendto_host)(const V3_SOCK sock, const char * hostname, const int port, 
234                     const char * buf, const int len);
235   int (*sendto_ip)(const V3_SOCK sock, const int ip_addr, const int port, 
236                   const char * buf, const int len);
237   
238   int (*recvfrom_host)(const V3_SOCK sock, const char * hostname, const int port, 
239                          char * buf, const int len);
240   int (*recvfrom_ip)(const V3_SOCK sock, const int ip_addr, const int port, 
241                          char * buf, const int len);
242 };
243
244
245 extern void V3_Init_Sockets(struct v3_socket_hooks * hooks);
246
247
248 #endif