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.


V3_Sockets Macros tested
[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_socket) {                  \
65         ret = (sock_hooks)->bind_socket(sock, port);                    \
66       }                                                         \
67       ret;                                                      \
68     })
69
70
71 #define V3_Listen_Socket(sock, backlog) ({                              \
72       extern struct v3_socket_hooks * sock_hooks;               \
73       int ret = -1;                                             \
74       if ((sock_hooks) && (sock_hooks)->listen) {                       \
75         ret = (sock_hooks)->listen(sock, backlog);                      \
76       }                                                         \
77       ret;                                                      \
78     })
79
80
81 #define V3_Accept_Socket(sock, ip_ptr, port_ptr) ({                     \
82       extern struct v3_socket_hooks * sock_hooks;               \
83       V3_SOCK client_sock = 0;                                          \
84       if ((sock_hooks) && (sock_hooks)->accept) {               \
85         client_sock = (sock_hooks)->accept(sock, ip_ptr, port_ptr);             \
86       }                                                         \
87       client_sock;                                                      \
88     })
89
90
91 #define V3_Select_Socket(rset,wset,eset,tv) ({                  \
92       extern struct v3_socket_hooks * sock_hooks;               \
93       int ret = -1;                                             \
94       if ((sock_hooks) && (sock_hooks)->select) {               \
95         ret = (sock_hooks)->select(rset, wset, eset, tv);       \
96       }                                                         \
97       ret;                                                      \
98     })
99
100
101
102 #define V3_Connect_To_IP(sock, ip, port) ({                     \
103       extern struct v3_socket_hooks * sock_hooks;               \
104       int ret = -1;                                             \
105       if ((sock_hooks) && (sock_hooks)->connect_to_ip) {        \
106         ret = (sock_hooks)->connect_to_ip(sock, ip, port);      \
107       }                                                         \
108       ret;                                                      \
109     })
110
111
112 #define V3_Connect_To_Host(sock, hostname, port) ({                     \
113       extern struct v3_socket_hooks * sock_hooks;                       \
114       int ret = -1;                                                     \
115       if ((sock_hooks) && (sock_hooks)->connect_to_host) {              \
116         ret = (sock_hooks)->connect_to_host(sock, hostname, port);      \
117       }                                                                 \
118       ret;                                                              \
119     })
120
121
122 #define V3_Send(sock, buf, len) ({                              \
123       extern struct v3_socket_hooks * sock_hooks;               \
124       int ret = -1;                                             \
125       if ((sock_hooks) && (sock_hooks)->send) {                 \
126         ret = (sock_hooks)->send(sock, buf, len);               \
127       }                                                         \
128       ret;                                                      \
129     })
130
131 #define V3_Recv(sock, buf, len) ({                              \
132       extern struct v3_socket_hooks * sock_hooks;               \
133       int ret = -1;                                             \
134       if ((sock_hooks) && (sock_hooks)->recv) {                 \
135         ret = (sock_hooks)->recv(sock, buf, len);               \
136       }                                                         \
137       ret;                                                      \
138     })
139
140 #define V3_SendTo_Host(sock, hostname, port, buf, len) ({               \
141       extern struct v3_socket_hooks * sock_hooks;                       \
142       int ret = -1;                                                     \
143       if ((sock_hooks) && (sock_hooks)->sendto_host) {                  \
144         ret = (sock_hooks)->sendto_host(sock, hostname, port, buf, len); \
145       }                                                                 \
146       ret;                                                              \
147     })
148
149
150 #define V3_SendTo_IP(sock, ip, port, buf, len) ({                       \
151       extern struct v3_socket_hooks * sock_hooks;                       \
152       int ret = -1;                                                     \
153       if ((sock_hooks) && (sock_hooks)->sendto_ip) {                    \
154         ret = (sock_hooks)->sendto_ip(sock, ip, port, buf, len);        \
155       }                                                                 \
156       ret;                                                              \
157     })
158
159
160 #define V3_RecvFrom_Host(sock, hostname, port, buf, len) ({             \
161       extern struct v3_socket_hooks * sock_hooks;                       \
162       int ret = -1;                                                     \
163       if ((sock_hooks) && (sock_hooks)->recvfrom_host) {                \
164         ret = (sock_hooks)->recvfrom_host(sock, hostname, port, buf, len); \
165       }                                                                 \
166       ret;                                                              \
167     })
168
169
170 #define V3_RecvFrom_IP(sock, ip, port, buf, len) ({                     \
171       extern struct v3_socket_hooks * sock_hooks;                       \
172       int ret = -1;                                                     \
173       if ((sock_hooks) && (sock_hooks)->recvfrom_ip) {                  \
174         ret = (sock_hooks)->recvfrom_ip(sock, ip, port, buf, len);      \
175       }                                                                 \
176       ret;                                                              \
177     })
178
179
180 #endif
181
182
183 struct v3_timeval {
184   long    tv_sec;         /* seconds */
185   long    tv_usec;        /* and microseconds */
186 };
187
188 struct v3_sock_entry {
189   V3_SOCK sock;
190   unsigned int is_set;
191   struct v3_sock_entry * next;
192 };
193
194 struct v3_sock_set {
195   unsigned int num_socks;
196   struct v3_sock_entry * socks;
197 };
198
199
200 void v3_init_sock_set(struct v3_sock_set * sock_set);
201
202 void v3_set_sock(struct v3_sock_set * sock_set, V3_SOCK sock); // adds socket to the sockset
203 void v3_clr_sock(struct v3_sock_set * sock_set, V3_SOCK sock); // deletes socket from sockset
204 int v3_isset_sock(struct v3_sock_set * sock_set, V3_SOCK sock);  // checks is_set vairable 
205 void v3_zero_sockset(struct v3_sock_set * sock_set);    // clears all is_set variables.
206
207
208
209 #define v3_foreach_sock(/* (struct v3_sock_set *) */ sock_set, /* (struct v3_sock_entry *) */ iter) \
210        for (iter = sock_set->socks; iter != NULL; iter = iter->next)
211
212
213
214 struct v3_socket_hooks {
215   // Socket creation routines
216   V3_SOCK (*tcp_socket)(const int bufsize, const int nodelay, const int nonblocking);
217   V3_SOCK (*udp_socket)(const int bufsize, const int nonblocking);
218
219   // Socket Destruction
220   void (*close)(V3_SOCK sock);
221
222   // Network Server Calls
223   int (*bind_socket)(const V3_SOCK sock, const int port);
224
225   int (*listen)(const V3_SOCK sock, int backlog);
226   
227   V3_SOCK (*accept)(const V3_SOCK sock, unsigned int * remote_ip, unsigned int * port);
228   // This going to suck
229   int (*select)(struct v3_sock_set * rset, \
230                 struct v3_sock_set * wset, \
231                 struct v3_sock_set * eset, \
232                 struct v3_timeval tv);
233
234   // Connect calls
235   int (*connect_to_ip)(const V3_SOCK sock, const int hostip, const int port);
236   int (*connect_to_host)(const V3_SOCK sock, const char * hostname, const int port);
237
238   // TCP Data Transfer
239   int (*send)(const V3_SOCK sock, const char * buf, const int len);
240   int (*recv)(const V3_SOCK sock, char * buf, const int len);
241   
242   // UDP Data Transfer
243   int (*sendto_host)(const V3_SOCK sock, const char * hostname, const int port, 
244                     const char * buf, const int len);
245   int (*sendto_ip)(const V3_SOCK sock, const int ip_addr, const int port, 
246                   const char * buf, const int len);
247   
248   int (*recvfrom_host)(const V3_SOCK sock, const char * hostname, const int port, 
249                          char * buf, const int len);
250   int (*recvfrom_ip)(const V3_SOCK sock, const int ip_addr, const int port, 
251                          char * buf, const int len);
252 };
253
254
255 extern void V3_Init_Sockets(struct v3_socket_hooks * hooks);
256
257
258 #endif