#ifdef __V3VEE__
-#define V3_Create_UDP_Socket() ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int sock = 0; \
- if ((sock_hooks) && (sock_hooks)->udp_socket) { \
- sock = (sock_hooks)->udp_socket(0,0); \
- } \
- sock; \
- })
-
+typedef void * v3_sock_t;
-#define V3_Create_TCP_Socket() ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int sock = 0; \
- if ((sock_hooks) && (sock_hooks)->tcp_socket) { \
- sock = (sock_hooks)->tcp_socket(0,1,0); \
- } \
- sock; \
- })
+v3_sock_t v3_create_udp_socket(struct v3_vm_info * vm);
+v3_sock_t v3_create_tcp_socket(struct v3_vm_info * vm);
+void v3_socket_close(v3_sock_t sock);
-#define V3_Close_Socket(sock) \
- do { \
- extern struct v3_socket_hooks * sock_hooks; \
- if ((sock_hooks) && (sock_hooks)->close) { \
- (sock_hooks)->close(sock); \
- } \
- } while (0);
+int v3_socket_bind(v3_sock_t sock, uint16_t port);
+int v3_socket_listen(const v3_sock_t sock, int backlog);
+v3_sock_t v3_socket_accept(const v3_sock_t sock, uint32_t * remote_ip, uint32_t * port);
+int v3_connect_to_ip(const v3_sock_t sock, const uint32_t hostip, const uint16_t port);
+int v3_connect_to_host(const v3_sock_t sock, const char * hostname, const uint16_t port);
-#define V3_Bind_Socket(sock, port) ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int ret = -1; \
- if ((sock_hooks) && (sock_hooks)->bind_socket) { \
- ret = (sock_hooks)->bind_socket(sock, port); \
- } \
- ret; \
- })
-
-
-#define V3_Listen_Socket(sock, backlog) ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int ret = -1; \
- if ((sock_hooks) && (sock_hooks)->listen) { \
- ret = (sock_hooks)->listen(sock, backlog); \
- } \
- ret; \
- })
+int v3_socket_send(const v3_sock_t sock, const uint8_t * buf, const uint32_t len);
+int v3_socket_recv(const v3_sock_t sock, uint8_t * buf, const uint32_t len);
-#define V3_Accept_Socket(sock, ip_ptr, port_ptr) ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int client_sock = 0; \
- if ((sock_hooks) && (sock_hooks)->accept) { \
- client_sock = (sock_hooks)->accept(sock, ip_ptr, port_ptr); \
- } \
- client_sock; \
- })
+int v3_socket_send_to_host(const v3_sock_t sock, const char * hostname, const uint16_t port,
+ const uint8_t * buf, const uint32_t len);
+int v3_socket_send_to_ip(const v3_sock_t sock, const uint32_t ip, const uint16_t port,
+ const uint8_t * buf, const uint32_t len);
+int v3_socket_recv_from_host(const v3_sock_t sock, const char * hostname, const uint16_t port,
+ uint8_t * buf, const uint32_t len);
+int v3_socket_recv_from_ip(const v3_sock_t sock, const uint32_t ip, const uint16_t port,
+ uint8_t * buf, const uint32_t len);
#define V3_Select_Socket(rset,wset,eset,tv) ({ \
-#define V3_Connect_To_IP(sock, ip, port) ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int ret = -1; \
- if ((sock_hooks) && (sock_hooks)->connect_to_ip) { \
- ret = (sock_hooks)->connect_to_ip(sock, ip, port); \
- } \
- ret; \
- })
-
-
-#define V3_Connect_To_Host(sock, hostname, port) ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int ret = -1; \
- if ((sock_hooks) && (sock_hooks)->connect_to_host) { \
- ret = (sock_hooks)->connect_to_host(sock, hostname, port); \
- } \
- ret; \
- })
-
-
-#define V3_Send(sock, buf, len) ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int ret = -1; \
- if ((sock_hooks) && (sock_hooks)->send) { \
- ret = (sock_hooks)->send(sock, buf, len); \
- } \
- ret; \
- })
-
-#define V3_Recv(sock, buf, len) ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int ret = -1; \
- if ((sock_hooks) && (sock_hooks)->recv) { \
- ret = (sock_hooks)->recv(sock, buf, len); \
- } \
- ret; \
- })
-
-#define V3_SendTo_Host(sock, hostname, port, buf, len) ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int ret = -1; \
- if ((sock_hooks) && (sock_hooks)->sendto_host) { \
- ret = (sock_hooks)->sendto_host(sock, hostname, port, buf, len); \
- } \
- ret; \
- })
-
-
-#define V3_SendTo_IP(sock, ip, port, buf, len) ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int ret = -1; \
- if ((sock_hooks) && (sock_hooks)->sendto_ip) { \
- ret = (sock_hooks)->sendto_ip(sock, ip, port, buf, len); \
- } \
- ret; \
- })
-
-#define V3_RecvFrom_Host(sock, hostname, port, buf, len) ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int ret = -1; \
- if ((sock_hooks) && (sock_hooks)->recvfrom_host) { \
- ret = (sock_hooks)->recvfrom_host(sock, hostname, port, buf, len); \
- } \
- ret; \
- })
-#define V3_RecvFrom_IP(sock, ip, port, buf, len) ({ \
- extern struct v3_socket_hooks * sock_hooks; \
- int ret = -1; \
- if ((sock_hooks) && (sock_hooks)->recvfrom_ip) { \
- ret = (sock_hooks)->recvfrom_ip(sock, ip, port, buf, len); \
- } \
- ret; \
- })
-
-
-#define V3_SOCK_SET(n, p) ((p)->fd_bits[(n)/8] |= (1 << ((n) & 7)))
-#define V3_SOCK_CLR(n, p) ((p)->fd_bits[(n)/8] &= ~(1 << ((n) & 7)))
-#define V3_SOCK_ISSET(n,p) ((p)->fd_bits[(n)/8] & (1 << ((n) & 7)))
-#define V3_SOCK_ZERO(p) memset((void*)(p), 0, sizeof(*(p)))
+#define V3_SOCK_SET(n, p) ((p)->fd_bits[(n) / 8] |= (1 << ((n) & 7)))
+#define V3_SOCK_CLR(n, p) ((p)->fd_bits[(n) / 8] &= ~(1 << ((n) & 7)))
+#define V3_SOCK_ISSET(n,p) ((p)->fd_bits[(n) / 8] & (1 << ((n) & 7)))
+#define V3_SOCK_ZERO(p) memset((void *)(p), 0, sizeof(*(p)))
uint32_t v3_inet_addr(const char * ip_str);
+
#endif
struct v3_socket_hooks {
/* Socket creation routines */
- int (*tcp_socket)(const int bufsize, const int nodelay, const int nonblocking);
- int (*udp_socket)(const int bufsize, const int nonblocking);
+ void *(*tcp_socket)(const int bufsize, const int nodelay, const int nonblocking, void * priv_data);
+ void *(*udp_socket)(const int bufsize, const int nonblocking, void * priv_data);
/* Socket Destruction */
- void (*close)(int sock);
+ void (*close)(void * sock);
/* Network Server Calls */
- int (*bind_socket)(const int sock, const int port);
+ int (*bind)(const void * sock, const int port);
- int (*listen)(const int sock, int backlog);
+ int (*listen)(const void * sock, int backlog);
- int (*accept)(const int sock, unsigned int * remote_ip, unsigned int * port);
+ void *(*accept)(const void * sock, unsigned int * remote_ip, unsigned int * port);
/* This going to suck */
int (*select)(struct v3_sock_set * rset, \
struct v3_sock_set * wset, \
struct v3_timeval tv);
/* Connect calls */
- int (*connect_to_ip)(const int sock, const int hostip, const int port);
- int (*connect_to_host)(const int sock, const char * hostname, const int port);
+ int (*connect_to_ip)(const void * sock, const int hostip, const int port);
+ int (*connect_to_host)(const void * sock, const char * hostname, const int port);
/* TCP Data Transfer */
- int (*send)(const int sock, const char * buf, const int len);
- int (*recv)(const int sock, char * buf, const int len);
+ int (*send)(const void * sock, const char * buf, const int len);
+ int (*recv)(const void * sock, char * buf, const int len);
/* UDP Data Transfer */
- int (*sendto_host)(const int sock, const char * hostname, const int port,
+ int (*sendto_host)(const void * sock, const char * hostname, const int port,
const char * buf, const int len);
- int (*sendto_ip)(const int sock, const int ip_addr, const int port,
+ int (*sendto_ip)(const void * sock, const int ip_addr, const int port,
const char * buf, const int len);
- int (*recvfrom_host)(const int sock, const char * hostname, const int port,
+ int (*recvfrom_host)(const void * sock, const char * hostname, const int port,
char * buf, const int len);
- int (*recvfrom_ip)(const int sock, const int ip_addr, const int port,
+ int (*recvfrom_ip)(const void * sock, const int ip_addr, const int port,
char * buf, const int len);
};
struct disk_state {
uint64_t capacity; // in bytes
- int socket;
+ v3_sock_t socket;
uint32_t ip_addr;
uint16_t port;
+ struct v3_vm_info * vm;
+
char disk_name[32];
};
-static int send_all(int socket, char * buf, int length) {
+static int send_all(v3_sock_t socket, char * buf, int length) {
int bytes_sent = 0;
PrintDebug("Sending %d bytes\n", length - bytes_sent);
while (bytes_sent < length) {
- int tmp_bytes = V3_Send(socket, buf + bytes_sent, length - bytes_sent);
+ int tmp_bytes = v3_socket_send(socket, buf + bytes_sent, length - bytes_sent);
PrintDebug("Sent %d bytes\n", tmp_bytes);
if (tmp_bytes == 0) {
}
-static int recv_all(int socket, char * buf, int length) {
+static int recv_all(v3_sock_t socket, char * buf, int length) {
int bytes_read = 0;
PrintDebug("Reading %d bytes\n", length - bytes_read);
while (bytes_read < length) {
- int tmp_bytes = V3_Recv(socket, buf + bytes_read, length - bytes_read);
+ int tmp_bytes = v3_socket_recv(socket, buf + bytes_read, length - bytes_read);
PrintDebug("Received %d bytes\n", tmp_bytes);
if (tmp_bytes == 0) {
PrintDebug("Intializing Net Disk\n");
- disk->socket = V3_Create_TCP_Socket();
+ disk->socket = v3_create_tcp_socket(disk->vm);
PrintDebug("DISK socket: %d\n", disk->socket);
PrintDebug("Connecting to: %s:%d\n", v3_inet_ntoa(disk->ip_addr), disk->port);
- V3_Connect_To_IP(disk->socket, v3_ntohl(disk->ip_addr), disk->port);
+ v3_connect_to_ip(disk->socket, v3_ntohl(disk->ip_addr), disk->port);
PrintDebug("Connected to NBD server\n");
strncpy(disk->disk_name, disk_tag, sizeof(disk->disk_name));
disk->ip_addr = v3_inet_addr(ip_str);
disk->port = atoi(port_str);
-
+ disk->vm = vm;
+
struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, disk);
if (v3_attach_device(vm, dev) == -1) {
#include <palacios/vmm.h>
#include <palacios/vmm_debug.h>
#include <palacios/vmm_types.h>
+#include <palacios/vm_guest.h>
-
-struct v3_socket_hooks * sock_hooks = 0;
-
-void V3_Init_Sockets(struct v3_socket_hooks * hooks) {
- sock_hooks = hooks;
- PrintDebug("V3 sockets inited\n");
-
- return;
-}
-
+static struct v3_socket_hooks * sock_hooks = 0;
uint32_t v3_inet_addr(const char * ip_str) {
rp = str;
ap = (uint8_t *)&addr;
- for(n = 0; n < 4; n++) {
+ for (n = 0; n < 4; n++) {
i = 0;
do {
uint32_t v3_ntohl(uint32_t n) {
return v3_htonl(n);
}
+
+
+v3_sock_t v3_create_udp_socket(struct v3_vm_info * vm) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->udp_socket);
+
+ return sock_hooks->udp_socket(0, 0, vm->host_priv_data);
+}
+
+v3_sock_t v3_create_tcp_socket(struct v3_vm_info * vm) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->tcp_socket);
+
+ return sock_hooks->tcp_socket(0, 1, 0, vm->host_priv_data);
+}
+
+void v3_socket_close(v3_sock_t sock) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->close);
+
+ sock_hooks->close(sock);
+}
+
+int v3_socket_bind(const v3_sock_t sock, uint16_t port) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->bind);
+
+ return sock_hooks->bind(sock, port);
+}
+
+int v3_socket_listen(const v3_sock_t sock, int backlog) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->listen);
+
+ return sock_hooks->listen(sock, backlog);
+}
+
+v3_sock_t v3_socket_accept(const v3_sock_t sock, uint32_t * remote_ip, uint32_t * port) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->accept);
+
+ return sock_hooks->accept(sock, remote_ip, port);
+}
+
+int v3_connect_to_ip(const v3_sock_t sock, const uint32_t hostip, const uint16_t port) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->connect_to_ip);
+
+ return sock_hooks->connect_to_ip(sock, hostip, port);
+}
+
+int v3_connect_to_host(const v3_sock_t sock, const char * hostname, const uint16_t port) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->connect_to_host);
+
+ return sock_hooks->connect_to_host(sock, hostname, port);
+}
+
+int v3_socket_send(const v3_sock_t sock, const uint8_t * buf, const uint32_t len) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->send);
+
+ return sock_hooks->send(sock, buf, len);
+}
+
+int v3_socket_recv(const v3_sock_t sock, uint8_t * buf, const uint32_t len) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->recv);
+
+ return sock_hooks->recv(sock, buf, len);
+}
+
+int v3_socket_send_to_host(const v3_sock_t sock, const char * hostname, const uint16_t port,
+ const uint8_t * buf, const uint32_t len) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->sendto_host);
+
+ return sock_hooks->sendto_host(sock, hostname, port, buf, len);
+}
+
+int v3_socket_send_to_ip(const v3_sock_t sock, const uint32_t ip, const uint16_t port,
+ const uint8_t * buf, const uint32_t len) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->sendto_ip);
+
+ return sock_hooks->sendto_ip(sock, ip, port, buf, len);
+}
+
+int v3_socket_recv_from_host(const v3_sock_t sock, const char * hostname, const uint16_t port,
+ uint8_t * buf, const uint32_t len) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->recvfrom_host);
+
+ return sock_hooks->recvfrom_host(sock, hostname, port, buf, len);
+}
+
+int v3_socket_recv_from_ip(const v3_sock_t sock, const uint32_t ip, const uint16_t port,
+ uint8_t * buf, const uint32_t len) {
+ V3_ASSERT(sock_hooks);
+ V3_ASSERT(sock_hooks->recvfrom_ip);
+
+ return sock_hooks->recvfrom_ip(sock, ip, port, buf, len);
+}
+
+
+
+
+void V3_Init_Sockets(struct v3_socket_hooks * hooks) {
+ sock_hooks = hooks;
+ PrintDebug("V3 sockets inited\n");
+
+ return;
+}