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>
25 #ifndef CONFIG_DEBUG_IDE
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 * HD_SECTOR_SIZE;
101 int length = sector_count * HD_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 * HD_SECTOR_SIZE;
160 int length = sector_count * HD_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 / HD_SECTOR_SIZE;
209 static struct v3_hd_ops hd_ops = {
212 .get_capacity = hd_get_capacity,
219 static int hd_free(struct vm_device * dev) {
223 static struct v3_device_ops dev_ops = {
231 static int socket_init(struct hd_state * hd) {
234 PrintDebug("Intializing Net HD\n");
236 hd->socket = V3_Create_TCP_Socket();
238 PrintDebug("HD socket: %d\n", hd->socket);
239 PrintDebug("Connecting to: %s:%d\n", v3_inet_ntoa(hd->ip_addr), hd->port);
241 V3_Connect_To_IP(hd->socket, v3_ntohl(hd->ip_addr), hd->port);
243 PrintDebug("Connected to NBD server\n");
245 //snprintf(header, 64, "V3_NBD_1 %s\n", cd->disk_name);
246 strcpy(header, "V3_NBD_1 ");
247 strncat(header, hd->disk_name, 32);
248 strncat(header, "\n", 1);
251 if (send_all(hd->socket, header, strlen(header)) == -1) {
252 PrintError("Error connecting to Network Block Device: %s\n", hd->disk_name);
258 char nbd_cmd[4] = {0,0,0,0};
260 nbd_cmd[0] = NBD_CAPACITY_CMD;
262 if (send_all(hd->socket, nbd_cmd, 4) == -1) {
263 PrintError("Error sending capacity command\n");
267 if (recv_all(hd->socket, (char *)&(hd->capacity), 8) == -1) {
268 PrintError("Error Receiving Capacity\n");
272 PrintDebug("Capacity: %p\n", (void *)(hd->capacity));
281 static int hd_init(struct guest_info * vm, void * cfg_data) {
282 struct hd_state * hd = (struct hd_state *)V3_Malloc(sizeof(struct hd_state));
283 struct net_hd_cfg * cfg = (struct net_hd_cfg *)cfg_data;
285 PrintDebug("Registering Net HD at %s:%d disk=%s\n", cfg->ip_str, cfg->port, cfg->disk_tag);
287 strncpy(hd->disk_name, cfg->disk_tag, sizeof(hd->disk_name));
288 hd->ip_addr = v3_inet_addr(cfg->ip_str);
289 hd->port = cfg->port;
291 hd->ide = v3_find_dev(vm, cfg->ide);
294 PrintError("Could not find backend %s\n", cfg->ide);
299 hd->drive = cfg->drive;
301 struct vm_device * dev = v3_allocate_device("NET-HD", &dev_ops, hd);
303 if (v3_attach_device(vm, dev) == -1) {
304 PrintError("Could not attach device %s\n", "NET-HD");
308 if (socket_init(hd) == -1) {
309 PrintError("could not initialize network connection\n");
314 PrintDebug("Registering HD\n");
316 if (v3_ide_register_harddisk(hd->ide, hd->bus, hd->drive, "V3-NET-HD", &hd_ops, dev) == -1) {
320 PrintDebug("intialization done\n");
326 device_register("NET-HD", hd_init)