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