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".
20 #include <palacios/vmm.h>
21 #include <devices/net_cd.h>
22 #include <devices/ide.h>
23 #include <palacios/vmm_socket.h>
27 #define PrintDebug(fmt, args...)
31 #define NBD_READ_CMD 0x1
32 #define NBD_WRITE_CMD 0x2
33 #define NBD_CAPACITY_CMD 0x3
35 #define NBD_STATUS_OK 0x00
36 #define NBD_STATUS_ERR 0xff
39 uint64_t capacity; // in bytes
48 struct vm_device * ide;
55 static int send_all(int socket, char * buf, int length) {
58 PrintDebug("Sending %d bytes\n", length - bytes_sent);
59 while (bytes_sent < length) {
60 int tmp_bytes = V3_Send(socket, buf + bytes_sent, length - bytes_sent);
61 PrintDebug("Sent %d bytes\n", tmp_bytes);
64 PrintError("Connection Closed unexpectedly\n");
68 bytes_sent += tmp_bytes;
75 static int recv_all(int socket, char * buf, int length) {
78 PrintDebug("Reading %d bytes\n", length - bytes_read);
79 while (bytes_read < length) {
80 int tmp_bytes = V3_Recv(socket, buf + bytes_read, length - bytes_read);
81 PrintDebug("Received %d bytes\n", tmp_bytes);
84 PrintError("Connection Closed unexpectedly\n");
88 bytes_read += tmp_bytes;
94 // CDs always read 2048 byte blocks... ?
95 static int cd_read(uint8_t * buf, int block_count, uint64_t lba, void * private_data) {
96 struct vm_device * cd_dev = (struct vm_device *)private_data;
97 struct cd_state * cd = (struct cd_state *)(cd_dev->private_data);
98 uint64_t offset = lba * ATAPI_BLOCK_SIZE;
99 int length = block_count * ATAPI_BLOCK_SIZE;
101 uint32_t ret_len = 0;
102 char nbd_cmd[4] = {0,0,0,0};
104 nbd_cmd[0] = NBD_READ_CMD;
106 if (send_all(cd->socket, nbd_cmd, 4) == -1) {
107 PrintError("Error sending capacity command\n");
111 if (send_all(cd->socket, (char *)&offset, 8) == -1) {
112 PrintError("Error sending read offset\n");
116 if (send_all(cd->socket, (char *)&length, 4) == -1) {
117 PrintError("Error sending read length\n");
121 if (recv_all(cd->socket, (char *)&status, 1) == -1) {
122 PrintError("Error receiving status\n");
126 if (status != NBD_STATUS_OK) {
127 PrintError("NBD Error....\n");
131 PrintDebug("Reading Data Ret Length\n");
133 if (recv_all(cd->socket, (char *)&ret_len, 4) == -1) {
134 PrintError("Error receiving Return read length\n");
138 if (ret_len != length) {
139 PrintError("Read length mismatch (req=%d) (result=%d)\n", length, ret_len);
143 PrintDebug("Reading Data (%d bytes)\n", ret_len);
145 if (recv_all(cd->socket, (char *)buf, ret_len) == -1) {
146 PrintError("Read Data Error\n");
154 static uint32_t cd_get_capacity(void * private_data) {
155 struct vm_device * cd_dev = (struct vm_device *)private_data;
156 struct cd_state * cd = (struct cd_state *)(cd_dev->private_data);
158 return cd->capacity / ATAPI_BLOCK_SIZE;
161 static struct v3_ide_cd_ops cd_ops = {
163 .get_capacity = cd_get_capacity,
167 static int cd_init(struct vm_device * dev) {
168 struct cd_state * cd = (struct cd_state *)(dev->private_data);
171 PrintDebug("Intializing Net CD\n");
173 cd->socket = V3_Create_TCP_Socket();
175 PrintDebug("CD socket: %d\n", cd->socket);
176 PrintDebug("Connecting to: %s:%d\n", v3_inet_ntoa(cd->ip_addr), cd->port);
178 V3_Connect_To_IP(cd->socket, v3_ntohl(cd->ip_addr), cd->port);
181 PrintDebug("Connected to NBD server\n");
183 //snprintf(header, 64, "V3_NBD_1 %s\n", cd->disk_name);
184 strcpy(header, "V3_NBD_1 ");
185 strncat(header, cd->disk_name, 32);
186 strncat(header, "\n", 1);
189 if (send_all(cd->socket, header, strlen(header)) == -1) {
190 PrintError("Error connecting to Network Block Device: %s\n", cd->disk_name);
196 char nbd_cmd[4] = {0,0,0,0};
198 nbd_cmd[0] = NBD_CAPACITY_CMD;
200 if (send_all(cd->socket, nbd_cmd, 4) == -1) {
201 PrintError("Error sending capacity command\n");
205 if (recv_all(cd->socket, (char *)&(cd->capacity), 8) == -1) {
206 PrintError("Error Receiving Capacity\n");
210 PrintDebug("Capacity: %p\n", (void *)(cd->capacity));
214 PrintDebug("Registering CD\n");
216 if (v3_ide_register_cdrom(cd->ide, cd->bus, cd->drive, "NET-CD", &cd_ops, dev) == -1) {
220 PrintDebug("intialization done\n");
226 static int cd_deinit(struct vm_device * dev) {
230 static struct vm_device_ops dev_ops = {
238 struct vm_device * v3_create_net_cd(struct vm_device * ide,
239 uint_t bus, uint_t drive,
240 const char * ip_str, uint16_t port,
241 const char * disk_tag) {
242 struct cd_state * cd = (struct cd_state *)V3_Malloc(sizeof(struct cd_state));
244 PrintDebug("Registering Net CD at %s:%d disk=%s\n", ip_str, port, disk_tag);
246 strncpy(cd->disk_name, disk_tag, sizeof(cd->disk_name));
247 cd->ip_addr = v3_inet_addr(ip_str);
254 struct vm_device * cd_dev = v3_create_device("NET-CD", &dev_ops, cd);