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>
30 typedef void * v3_sock_t;
32 v3_sock_t v3_create_udp_socket(struct v3_vm_info * vm);
33 v3_sock_t v3_create_tcp_socket(struct v3_vm_info * vm);
35 void v3_socket_close(v3_sock_t sock);
37 int v3_socket_bind(v3_sock_t sock, uint16_t port);
38 int v3_socket_listen(const v3_sock_t sock, int backlog);
39 v3_sock_t v3_socket_accept(const v3_sock_t sock, uint32_t * remote_ip, uint32_t * port);
41 int v3_connect_to_ip(const v3_sock_t sock, const uint32_t hostip, const uint16_t port);
42 int v3_connect_to_host(const v3_sock_t sock, const char * hostname, const uint16_t port);
45 int v3_socket_send(const v3_sock_t sock, const uint8_t * buf, const uint32_t len);
46 int v3_socket_recv(const v3_sock_t sock, uint8_t * buf, const uint32_t len);
49 int v3_socket_send_to_host(const v3_sock_t sock, const char * hostname, const uint16_t port,
50 const uint8_t * buf, const uint32_t len);
51 int v3_socket_send_to_ip(const v3_sock_t sock, const uint32_t ip, const uint16_t port,
52 const uint8_t * buf, const uint32_t len);
53 int v3_socket_recv_from_host(const v3_sock_t sock, const char * hostname, const uint16_t port,
54 uint8_t * buf, const uint32_t len);
55 int v3_socket_recv_from_ip(const v3_sock_t sock, const uint32_t ip, const uint16_t port,
56 uint8_t * buf, const uint32_t len);
59 #define V3_Select_Socket(rset,wset,eset,tv) ({ \
60 extern struct v3_socket_hooks * sock_hooks; \
62 if ((sock_hooks) && (sock_hooks)->select) { \
63 ret = (sock_hooks)->select(rset, wset, eset, tv); \
74 #define V3_SOCK_SET(n, p) ((p)->fd_bits[(n) / 8] |= (1 << ((n) & 7)))
75 #define V3_SOCK_CLR(n, p) ((p)->fd_bits[(n) / 8] &= ~(1 << ((n) & 7)))
76 #define V3_SOCK_ISSET(n,p) ((p)->fd_bits[(n) / 8] & (1 << ((n) & 7)))
77 #define V3_SOCK_ZERO(p) memset((void *)(p), 0, sizeof(*(p)))
80 uint32_t v3_inet_addr(const char * ip_str);
81 char * v3_inet_ntoa(uint32_t addr);
82 uint16_t v3_htons(uint16_t s);
83 uint16_t v3_ntohs(uint16_t s);
84 uint32_t v3_htonl(uint32_t s);
85 uint32_t v3_ntohl(uint32_t s);
94 long tv_sec; /* seconds */
95 long tv_usec; /* and microseconds */
99 #define V3_SOCK_SETSIZE 1000
101 typedef struct v3_sock_set {
102 /* This format needs to match the standard posix FD_SET format, so it can be cast */
103 unsigned char fd_bits [(V3_SOCK_SETSIZE + 7) / 8];
108 struct v3_socket_hooks {
109 /* Socket creation routines */
110 void *(*tcp_socket)(const int bufsize, const int nodelay, const int nonblocking, void * priv_data);
111 void *(*udp_socket)(const int bufsize, const int nonblocking, void * priv_data);
113 /* Socket Destruction */
114 void (*close)(void * sock);
116 /* Network Server Calls */
117 int (*bind)(const void * sock, const int port);
119 int (*listen)(const void * sock, int backlog);
121 void *(*accept)(const void * sock, unsigned int * remote_ip, unsigned int * port);
122 /* This going to suck */
123 int (*select)(struct v3_sock_set * rset, \
124 struct v3_sock_set * wset, \
125 struct v3_sock_set * eset, \
126 struct v3_timeval tv);
129 int (*connect_to_ip)(const void * sock, const int hostip, const int port);
130 int (*connect_to_host)(const void * sock, const char * hostname, const int port);
132 /* TCP Data Transfer */
133 int (*send)(const void * sock, const char * buf, const int len);
134 int (*recv)(const void * sock, char * buf, const int len);
136 /* UDP Data Transfer */
137 int (*sendto_host)(const void * sock, const char * hostname, const int port,
138 const char * buf, const int len);
139 int (*sendto_ip)(const void * sock, const int ip_addr, const int port,
140 const char * buf, const int len);
142 int (*recvfrom_host)(const void * sock, const char * hostname, const int port,
143 char * buf, const int len);
144 int (*recvfrom_ip)(const void * sock, const int ip_addr, const int port,
145 char * buf, const int len);
149 extern void V3_Init_Sockets(struct v3_socket_hooks * hooks);