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_hd.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
40 uint64_t capacity; // in bytes
49 struct vm_device * ide;
56 static int send_all(int socket, char * buf, int length) {
59 PrintDebug("Sending %d bytes\n", length - bytes_sent);
60 while (bytes_sent < length) {
61 int tmp_bytes = V3_Send(socket, buf + bytes_sent, length - bytes_sent);
62 PrintDebug("Sent %d bytes\n", tmp_bytes);
65 PrintError("Connection Closed unexpectedly\n");
69 bytes_sent += tmp_bytes;
76 static int recv_all(int socket, char * buf, int length) {
79 PrintDebug("Reading %d bytes\n", length - bytes_read);
80 while (bytes_read < length) {
81 int tmp_bytes = V3_Recv(socket, buf + bytes_read, length - bytes_read);
82 PrintDebug("Received %d bytes\n", tmp_bytes);
85 PrintError("Connection Closed unexpectedly\n");
89 bytes_read += tmp_bytes;
96 // HDs always read 512 byte blocks... ?
97 static int hd_read(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) {
98 struct vm_device * hd_dev = (struct vm_device *)private_data;
99 struct hd_state * hd = (struct hd_state *)(hd_dev->private_data);
100 int offset = lba * IDE_SECTOR_SIZE;
101 int length = sector_count * IDE_SECTOR_SIZE;
103 uint32_t ret_len = 0;
104 char nbd_cmd[4] = {0,0,0,0};
106 nbd_cmd[0] = NBD_READ_CMD;
108 if (send_all(hd->socket, nbd_cmd, 4) == -1) {
109 PrintError("Error sending read command\n");
113 if (send_all(hd->socket, (char *)&offset, 8) == -1) {
114 PrintError("Error sending read offset\n");
118 if (send_all(hd->socket, (char *)&length, 4) == -1) {
119 PrintError("Error sending read length\n");
123 if (recv_all(hd->socket, (char *)&status, 1) == -1) {
124 PrintError("Error receiving status\n");
128 if (status != NBD_STATUS_OK) {
129 PrintError("NBD Error....\n");
133 PrintDebug("Reading Data Ret Length\n");
135 if (recv_all(hd->socket, (char *)&ret_len, 4) == -1) {
136 PrintError("Error receiving Return read length\n");
140 if (ret_len != length) {
141 PrintError("Read length mismatch (req=%d) (result=%d)\n", length, ret_len);
145 PrintDebug("Reading Data (%d bytes)\n", ret_len);
147 if (recv_all(hd->socket, (char *)buf, ret_len) == -1) {
148 PrintError("Read Data Error\n");
156 static int hd_write(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) {
157 struct vm_device * hd_dev = (struct vm_device *)private_data;
158 struct hd_state * hd = (struct hd_state *)(hd_dev->private_data);
159 int offset = lba * IDE_SECTOR_SIZE;
160 int length = sector_count * IDE_SECTOR_SIZE;
162 char nbd_cmd[4] = {0,0,0,0};
164 nbd_cmd[0] = NBD_WRITE_CMD;
166 if (send_all(hd->socket, nbd_cmd, 4) == -1) {
167 PrintError("Error sending write command\n");
171 if (send_all(hd->socket, (char *)&offset, 8) == -1) {
172 PrintError("Error sending write offset\n");
176 if (send_all(hd->socket, (char *)&length, 4) == -1) {
177 PrintError("Error sending write length\n");
181 PrintDebug("Writing Data (%d bytes)\n", length);
183 if (send_all(hd->socket, (char *)buf, length) == -1) {
184 PrintError("Write Data Error\n");
188 if (recv_all(hd->socket, (char *)&status, 1) == -1) {
189 PrintError("Error receiving status\n");
193 if (status != NBD_STATUS_OK) {
194 PrintError("NBD Error....\n");
202 static uint64_t hd_get_capacity(void * private_data) {
203 struct vm_device * hd_dev = (struct vm_device *)private_data;
204 struct hd_state * hd = (struct hd_state *)(hd_dev->private_data);
206 return hd->capacity / IDE_SECTOR_SIZE;
209 static struct v3_ide_hd_ops hd_ops = {
212 .get_capacity = hd_get_capacity,
216 static int hd_init(struct vm_device * dev) {
217 struct hd_state * hd = (struct hd_state *)(dev->private_data);
220 PrintDebug("Intializing Net HD\n");
222 hd->socket = V3_Create_TCP_Socket();
224 PrintDebug("HD socket: %d\n", hd->socket);
225 PrintDebug("Connecting to: %s:%d\n", v3_inet_ntoa(hd->ip_addr), hd->port);
227 V3_Connect_To_IP(hd->socket, v3_ntohl(hd->ip_addr), hd->port);
229 PrintDebug("Connected to NBD server\n");
231 //snprintf(header, 64, "V3_NBD_1 %s\n", cd->disk_name);
232 strcpy(header, "V3_NBD_1 ");
233 strncat(header, hd->disk_name, 32);
234 strncat(header, "\n", 1);
237 if (send_all(hd->socket, header, strlen(header)) == -1) {
238 PrintError("Error connecting to Network Block Device: %s\n", hd->disk_name);
244 char nbd_cmd[4] = {0,0,0,0};
246 nbd_cmd[0] = NBD_CAPACITY_CMD;
248 if (send_all(hd->socket, nbd_cmd, 4) == -1) {
249 PrintError("Error sending capacity command\n");
253 if (recv_all(hd->socket, (char *)&(hd->capacity), 8) == -1) {
254 PrintError("Error Receiving Capacity\n");
258 PrintDebug("Capacity: %p\n", (void *)(hd->capacity));
261 PrintDebug("Registering HD\n");
263 if (v3_ide_register_harddisk(hd->ide, hd->bus, hd->drive, "V3-NET-HD", &hd_ops, dev) == -1) {
267 PrintDebug("intialization done\n");
273 static int hd_deinit(struct vm_device * dev) {
277 static struct vm_device_ops dev_ops = {
285 struct vm_device * v3_create_net_hd(struct vm_device * ide,
286 uint_t bus, uint_t drive,
287 const char * ip_str, uint16_t port,
288 const char * disk_tag) {
289 struct hd_state * hd = (struct hd_state *)V3_Malloc(sizeof(struct hd_state));
291 PrintDebug("Registering Net HD at %s:%d disk=%s\n", ip_str, port, disk_tag);
293 strncpy(hd->disk_name, disk_tag, sizeof(hd->disk_name));
294 hd->ip_addr = v3_inet_addr(ip_str);
301 struct vm_device * hd_dev = v3_create_device("NET-HD", &dev_ops, hd);