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.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
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.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 #ifndef __VMM_SOCKET_H__
22 #define __VMM_SOCKET_H__
24 #include <palacios/vmm.h>
29 #define V3_Create_UDP_Socket() ({ \
30 extern struct v3_socket_hooks * sock_hooks; \
32 if ((sock_hooks) && (sock_hooks)->udp_socket) { \
33 sock = (sock_hooks)->udp_socket(0,0); \
40 #define V3_Create_TCP_Socket() ({ \
41 extern struct v3_socket_hooks * sock_hooks; \
43 if ((sock_hooks) && (sock_hooks)->tcp_socket) { \
44 sock = (sock_hooks)->tcp_socket(0,1,0); \
50 #define V3_Close_Socket(sock) \
52 extern struct v3_socket_hooks * sock_hooks; \
53 if ((sock_hooks) && (sock_hooks)->close) { \
54 (sock_hooks)->close(sock); \
60 #define V3_Bind_Socket(sock, port) ({ \
61 extern struct v3_socket_hooks * sock_hooks; \
63 if ((sock_hooks) && (sock_hooks)->bind_socket) { \
64 ret = (sock_hooks)->bind_socket(sock, port); \
70 #define V3_Listen_Socket(sock, backlog) ({ \
71 extern struct v3_socket_hooks * sock_hooks; \
73 if ((sock_hooks) && (sock_hooks)->listen) { \
74 ret = (sock_hooks)->listen(sock, backlog); \
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); \
90 #define V3_Select_Socket(rset,wset,eset,tv) ({ \
91 extern struct v3_socket_hooks * sock_hooks; \
93 if ((sock_hooks) && (sock_hooks)->select) { \
94 ret = (sock_hooks)->select(rset, wset, eset, tv); \
101 #define V3_Connect_To_IP(sock, ip, port) ({ \
102 extern struct v3_socket_hooks * sock_hooks; \
104 if ((sock_hooks) && (sock_hooks)->connect_to_ip) { \
105 ret = (sock_hooks)->connect_to_ip(sock, ip, port); \
111 #define V3_Connect_To_Host(sock, hostname, port) ({ \
112 extern struct v3_socket_hooks * sock_hooks; \
114 if ((sock_hooks) && (sock_hooks)->connect_to_host) { \
115 ret = (sock_hooks)->connect_to_host(sock, hostname, port); \
121 #define V3_Send(sock, buf, len) ({ \
122 extern struct v3_socket_hooks * sock_hooks; \
124 if ((sock_hooks) && (sock_hooks)->send) { \
125 ret = (sock_hooks)->send(sock, buf, len); \
130 #define V3_Recv(sock, buf, len) ({ \
131 extern struct v3_socket_hooks * sock_hooks; \
133 if ((sock_hooks) && (sock_hooks)->recv) { \
134 ret = (sock_hooks)->recv(sock, buf, len); \
139 #define V3_SendTo_Host(sock, hostname, port, buf, len) ({ \
140 extern struct v3_socket_hooks * sock_hooks; \
142 if ((sock_hooks) && (sock_hooks)->sendto_host) { \
143 ret = (sock_hooks)->sendto_host(sock, hostname, port, buf, len); \
149 #define V3_SendTo_IP(sock, ip, port, buf, len) ({ \
150 extern struct v3_socket_hooks * sock_hooks; \
152 if ((sock_hooks) && (sock_hooks)->sendto_ip) { \
153 ret = (sock_hooks)->sendto_ip(sock, ip, port, buf, len); \
159 #define V3_RecvFrom_Host(sock, hostname, port, buf, len) ({ \
160 extern struct v3_socket_hooks * sock_hooks; \
162 if ((sock_hooks) && (sock_hooks)->recvfrom_host) { \
163 ret = (sock_hooks)->recvfrom_host(sock, hostname, port, buf, len); \
169 #define V3_RecvFrom_IP(sock, ip, port, buf, len) ({ \
170 extern struct v3_socket_hooks * sock_hooks; \
172 if ((sock_hooks) && (sock_hooks)->recvfrom_ip) { \
173 ret = (sock_hooks)->recvfrom_ip(sock, ip, port, buf, len); \
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)))
186 uint32_t v3_inet_addr(const char * ip_str);
187 char * v3_inet_ntoa(uint32_t addr);
188 uint16_t v3_htons(uint16_t s);
189 uint16_t v3_ntohs(uint16_t s);
190 uint32_t v3_htonl(uint32_t s);
191 uint32_t v3_ntohl(uint32_t s);
199 long tv_sec; /* seconds */
200 long tv_usec; /* and microseconds */
204 #define V3_SOCK_SETSIZE 1000
206 typedef struct v3_sock_set {
207 /* This format needs to match the standard posix FD_SET format, so it can be cast */
208 unsigned char fd_bits [(V3_SOCK_SETSIZE + 7) / 8];
213 struct v3_socket_hooks {
214 /* Socket creation routines */
215 int (*tcp_socket)(const int bufsize, const int nodelay, const int nonblocking);
216 int (*udp_socket)(const int bufsize, const int nonblocking);
218 /* Socket Destruction */
219 void (*close)(int sock);
221 /* Network Server Calls */
222 int (*bind_socket)(const int sock, const int port);
224 int (*listen)(const int sock, int backlog);
226 int (*accept)(const int sock, unsigned int * remote_ip, unsigned int * port);
227 /* This going to suck */
228 int (*select)(struct v3_sock_set * rset, \
229 struct v3_sock_set * wset, \
230 struct v3_sock_set * eset, \
231 struct v3_timeval tv);
234 int (*connect_to_ip)(const int sock, const int hostip, const int port);
235 int (*connect_to_host)(const int sock, const char * hostname, const int port);
237 /* TCP Data Transfer */
238 int (*send)(const int sock, const char * buf, const int len);
239 int (*recv)(const int sock, char * buf, const int len);
241 /* UDP Data Transfer */
242 int (*sendto_host)(const int sock, const char * hostname, const int port,
243 const char * buf, const int len);
244 int (*sendto_ip)(const int sock, const int ip_addr, const int port,
245 const char * buf, const int len);
247 int (*recvfrom_host)(const int sock, const char * hostname, const int port,
248 char * buf, const int len);
249 int (*recvfrom_ip)(const int sock, const int ip_addr, const int port,
250 char * buf, const int len);
254 extern void V3_Init_Sockets(struct v3_socket_hooks * hooks);