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.


reformatting include files
[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
27 #ifdef __V3VEE__
28
29 #define V3_Create_UDP_Socket() ({                               \
30             extern struct v3_socket_hooks * sock_hooks;         \
31             int sock = 0;                                       \
32             if ((sock_hooks) && (sock_hooks)->udp_socket) {     \
33                 sock = (sock_hooks)->udp_socket(0,0);           \
34             }                                                   \
35             sock;                                               \
36         })
37
38
39
40 #define V3_Create_TCP_Socket() ({                               \
41             extern struct v3_socket_hooks * sock_hooks;         \
42             int sock = 0;                                       \
43             if ((sock_hooks) && (sock_hooks)->tcp_socket) {     \
44                 sock = (sock_hooks)->tcp_socket(0,0,0);         \
45             }                                                   \
46             sock;                                               \
47         })
48
49
50 #define V3_Close_Socket(sock)                           \
51     do {                                                \
52         extern struct v3_socket_hooks * sock_hooks;     \
53         if ((sock_hooks) && (sock_hooks)->close) {      \
54             (sock_hooks)->close(sock);                  \
55         }                                               \
56     } while (0);
57
58
59
60 #define V3_Bind_Socket(sock, port) ({                           \
61             extern struct v3_socket_hooks * sock_hooks;         \
62             int ret = -1;                                       \
63             if ((sock_hooks) && (sock_hooks)->bind_socket) {    \
64                 ret = (sock_hooks)->bind_socket(sock, port);    \
65             }                                                   \
66             ret;                                                \
67         })
68
69
70 #define V3_Listen_Socket(sock, backlog) ({                      \
71             extern struct v3_socket_hooks * sock_hooks;         \
72             int ret = -1;                                       \
73             if ((sock_hooks) && (sock_hooks)->listen) {         \
74                 ret = (sock_hooks)->listen(sock, backlog);      \
75             }                                                   \
76             ret;                                                \
77         })
78
79
80 #define V3_Accept_Socket(sock, ip_ptr, port_ptr) ({                     \
81             extern struct v3_socket_hooks * sock_hooks;                 \
82             int client_sock = 0;                                        \
83             if ((sock_hooks) && (sock_hooks)->accept) {                 \
84                 client_sock = (sock_hooks)->accept(sock, ip_ptr, port_ptr); \
85             }                                                           \
86             client_sock;                                                \
87         })
88
89
90 #define V3_Select_Socket(rset,wset,eset,tv) ({                          \
91             extern struct v3_socket_hooks * sock_hooks;                 \
92             int ret = -1;                                               \
93             if ((sock_hooks) && (sock_hooks)->select) {                 \
94                 ret = (sock_hooks)->select(rset, wset, eset, tv);       \
95             }                                                           \
96             ret;                                                        \
97         })
98
99
100
101 #define V3_Connect_To_IP(sock, ip, port) ({                             \
102             extern struct v3_socket_hooks * sock_hooks;                 \
103             int ret = -1;                                               \
104             if ((sock_hooks) && (sock_hooks)->connect_to_ip) {          \
105                 ret = (sock_hooks)->connect_to_ip(sock, ip, port);      \
106             }                                                           \
107             ret;                                                        \
108         })
109
110
111 #define V3_Connect_To_Host(sock, hostname, port) ({                     \
112             extern struct v3_socket_hooks * sock_hooks;                 \
113             int ret = -1;                                               \
114             if ((sock_hooks) && (sock_hooks)->connect_to_host) {        \
115                 ret = (sock_hooks)->connect_to_host(sock, hostname, port); \
116             }                                                           \
117             ret;                                                        \
118         })
119
120
121 #define V3_Send(sock, buf, len) ({                              \
122             extern struct v3_socket_hooks * sock_hooks;         \
123             int ret = -1;                                       \
124             if ((sock_hooks) && (sock_hooks)->send) {           \
125                 ret = (sock_hooks)->send(sock, buf, len);       \
126             }                                                   \
127             ret;                                                \
128         })
129
130 #define V3_Recv(sock, buf, len) ({                              \
131             extern struct v3_socket_hooks * sock_hooks;         \
132             int ret = -1;                                       \
133             if ((sock_hooks) && (sock_hooks)->recv) {           \
134                 ret = (sock_hooks)->recv(sock, buf, len);       \
135             }                                                   \
136             ret;                                                \
137         })
138
139 #define V3_SendTo_Host(sock, hostname, port, buf, len) ({               \
140             extern struct v3_socket_hooks * sock_hooks;                 \
141             int ret = -1;                                               \
142             if ((sock_hooks) && (sock_hooks)->sendto_host) {            \
143                 ret = (sock_hooks)->sendto_host(sock, hostname, port, buf, len); \
144             }                                                           \
145             ret;                                                        \
146         })
147
148
149 #define V3_SendTo_IP(sock, ip, port, buf, len) ({                       \
150             extern struct v3_socket_hooks * sock_hooks;                 \
151             int ret = -1;                                               \
152             if ((sock_hooks) && (sock_hooks)->sendto_ip) {              \
153                 ret = (sock_hooks)->sendto_ip(sock, ip, port, buf, len); \
154             }                                                           \
155             ret;                                                        \
156         })
157
158
159 #define V3_RecvFrom_Host(sock, hostname, port, buf, len) ({             \
160             extern struct v3_socket_hooks * sock_hooks;                 \
161             int ret = -1;                                               \
162             if ((sock_hooks) && (sock_hooks)->recvfrom_host) {          \
163                 ret = (sock_hooks)->recvfrom_host(sock, hostname, port, buf, len); \
164             }                                                           \
165             ret;                                                        \
166         })
167
168
169 #define V3_RecvFrom_IP(sock, ip, port, buf, len) ({                     \
170             extern struct v3_socket_hooks * sock_hooks;                 \
171             int ret = -1;                                               \
172             if ((sock_hooks) && (sock_hooks)->recvfrom_ip) {            \
173                 ret = (sock_hooks)->recvfrom_ip(sock, ip, port, buf, len); \
174             }                                                           \
175             ret;                                                        \
176         })
177
178
179
180 #define V3_SOCK_SET(n, p)  ((p)->fd_bits[(n)/8] |=  (1 << ((n) & 7)))
181 #define V3_SOCK_CLR(n, p)  ((p)->fd_bits[(n)/8] &= ~(1 << ((n) & 7)))
182 #define V3_SOCK_ISSET(n,p) ((p)->fd_bits[(n)/8] &   (1 << ((n) & 7)))
183 #define V3_SOCK_ZERO(p)    memset((void*)(p), 0, sizeof(*(p)))
184
185
186 #endif
187
188
189 struct v3_timeval {
190     long    tv_sec;         /* seconds */
191     long    tv_usec;        /* and microseconds */
192 };
193
194
195 #define V3_SOCK_SETSIZE    1000
196
197 typedef struct v3_sock_set {
198     // This format needs to match the standard posix FD_SET format, so it can be cast
199     unsigned char fd_bits [(V3_SOCK_SETSIZE + 7) / 8];
200 } v3_sock_set;
201
202
203
204 struct v3_socket_hooks {
205     // Socket creation routines
206     int (*tcp_socket)(const int bufsize, const int nodelay, const int nonblocking);
207     int (*udp_socket)(const int bufsize, const int nonblocking);
208
209     // Socket Destruction
210     void (*close)(int sock);
211
212     // Network Server Calls
213     int (*bind_socket)(const int sock, const int port);
214
215     int (*listen)(const int sock, int backlog);
216   
217     int (*accept)(const int 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 int sock, const int hostip, const int port);
226     int (*connect_to_host)(const int sock, const char * hostname, const int port);
227
228     // TCP Data Transfer
229     int (*send)(const int sock, const char * buf, const int len);
230     int (*recv)(const int sock, char * buf, const int len);
231   
232     // UDP Data Transfer
233     int (*sendto_host)(const int sock, const char * hostname, const int port, 
234                        const char * buf, const int len);
235     int (*sendto_ip)(const int sock, const int ip_addr, const int port, 
236                      const char * buf, const int len);
237   
238     int (*recvfrom_host)(const int sock, const char * hostname, const int port, 
239                          char * buf, const int len);
240     int (*recvfrom_ip)(const int 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 #endif