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 <palacios/vmm_dev_mgr.h>
22 #include <interfaces/vmm_socket.h>
24 #ifndef V3_CONFIG_DEBUG_IDE
26 #define PrintDebug(fmt, args...)
30 #define NBD_READ_CMD 0x1
31 #define NBD_WRITE_CMD 0x2
32 #define NBD_CAPACITY_CMD 0x3
34 #define NBD_STATUS_OK 0x00
35 #define NBD_STATUS_ERR 0xff
39 uint64_t capacity; // in bytes
46 struct v3_vm_info * vm;
53 static int send_all(v3_sock_t socket, char * buf, int length) {
56 PrintDebug("Sending %d bytes\n", length - bytes_sent);
57 while (bytes_sent < length) {
58 int tmp_bytes = v3_socket_send(socket, buf + bytes_sent, length - bytes_sent);
59 PrintDebug("Sent %d bytes\n", tmp_bytes);
62 PrintError("Connection Closed unexpectedly\n");
66 bytes_sent += tmp_bytes;
73 static int recv_all(v3_sock_t socket, char * buf, int length) {
76 PrintDebug("Reading %d bytes\n", length - bytes_read);
77 while (bytes_read < length) {
78 int tmp_bytes = v3_socket_recv(socket, buf + bytes_read, length - bytes_read);
79 PrintDebug("Received %d bytes\n", tmp_bytes);
82 PrintError("Connection Closed unexpectedly\n");
86 bytes_read += tmp_bytes;
93 // HDs always read 512 byte blocks... ?
94 static int read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
95 struct disk_state * disk = (struct disk_state *)private_data ;
98 char nbd_cmd[4] = {0,0,0,0};
99 uint64_t offset = lba;
100 uint64_t length = num_bytes;
102 nbd_cmd[0] = NBD_READ_CMD;
104 if (send_all(disk->socket, nbd_cmd, 4) == -1) {
105 PrintError("Error sending read command\n");
109 if (send_all(disk->socket, (char *)&offset, 8) == -1) {
110 PrintError("Error sending read offset\n");
114 if (send_all(disk->socket, (char *)&length, 4) == -1) {
115 PrintError("Error sending read length\n");
119 if (recv_all(disk->socket, (char *)&status, 1) == -1) {
120 PrintError("Error receiving status\n");
124 if (status != NBD_STATUS_OK) {
125 PrintError("NBD Error....\n");
129 PrintDebug("Reading Data Ret Length\n");
131 if (recv_all(disk->socket, (char *)&ret_len, 4) == -1) {
132 PrintError("Error receiving Return read length\n");
136 if (ret_len != length) {
137 PrintError("Read length mismatch (req=%llu) (result=%u)\n", length, ret_len);
141 PrintDebug("Reading Data (%d bytes)\n", ret_len);
143 if (recv_all(disk->socket, (char *)buf, ret_len) == -1) {
144 PrintError("Read Data Error\n");
152 static int write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) {
153 struct disk_state * disk = (struct disk_state *)private_data ;
154 uint64_t offset = lba;
155 int length = num_bytes;
157 char nbd_cmd[4] = {0,0,0,0};
159 nbd_cmd[0] = NBD_WRITE_CMD;
161 if (send_all(disk->socket, nbd_cmd, 4) == -1) {
162 PrintError("Error sending write command\n");
166 if (send_all(disk->socket, (char *)&offset, 8) == -1) {
167 PrintError("Error sending write offset\n");
171 if (send_all(disk->socket, (char *)&length, 4) == -1) {
172 PrintError("Error sending write length\n");
176 PrintDebug("Writing Data (%d bytes)\n", length);
178 if (send_all(disk->socket, (char *)buf, length) == -1) {
179 PrintError("Write Data Error\n");
183 if (recv_all(disk->socket, (char *)&status, 1) == -1) {
184 PrintError("Error receiving status\n");
188 if (status != NBD_STATUS_OK) {
189 PrintError("NBD Error....\n");
197 static uint64_t get_capacity(void * private_data) {
198 struct disk_state * disk = (struct disk_state *)private_data;
200 return disk->capacity;
203 static struct v3_dev_blk_ops blk_ops = {
206 .get_capacity = get_capacity,
213 static int disk_free(struct disk_state * disk) {
215 v3_socket_close(disk->socket);
221 static struct v3_device_ops dev_ops = {
222 .free = (int (*)(void *))disk_free,
226 static int socket_init(struct disk_state * disk) {
229 PrintDebug("Intializing Net Disk\n");
231 disk->socket = v3_create_tcp_socket(disk->vm);
233 PrintDebug("DISK socket: %p\n", disk->socket);
234 PrintDebug("Connecting to: %s:%d\n", v3_inet_ntoa(disk->ip_addr), disk->port);
236 v3_connect_to_ip(disk->socket, v3_ntohl(disk->ip_addr), disk->port);
238 PrintDebug("Connected to NBD server\n");
240 //snprintf(header, 64, "V3_NBD_1 %s\n", cd->disk_name);
241 strcpy(header, "V3_NBD_1 ");
242 strncat(header, disk->disk_name, 32);
243 strncat(header, "\n", 1);
246 if (send_all(disk->socket, header, strlen(header)) == -1) {
247 PrintError("Error connecting to Network Block Device: %s\n", disk->disk_name);
251 // store local copy of capacity
253 char nbd_cmd[4] = {0,0,0,0};
255 nbd_cmd[0] = NBD_CAPACITY_CMD;
257 if (send_all(disk->socket, nbd_cmd, 4) == -1) {
258 PrintError("Error sending capacity command\n");
262 if (recv_all(disk->socket, (char *)&(disk->capacity), 8) == -1) {
263 PrintError("Error Receiving Capacity\n");
267 PrintDebug("Capacity: %p\n", (void *)(addr_t)disk->capacity);
276 static int disk_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
277 struct disk_state * disk = (struct disk_state *)V3_Malloc(sizeof(struct disk_state));
279 char * ip_str = v3_cfg_val(cfg, "IP");
280 char * port_str = v3_cfg_val(cfg, "port");
281 char * disk_tag = v3_cfg_val(cfg, "tag");
282 char * dev_id = v3_cfg_val(cfg, "ID");
284 v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
286 PrintDebug("Registering Net disk at %s:%s disk=%s\n", ip_str, port_str, disk_tag);
288 strncpy(disk->disk_name, disk_tag, sizeof(disk->disk_name));
289 disk->ip_addr = v3_inet_addr(ip_str);
290 disk->port = atoi(port_str);
293 struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, disk);
296 PrintError("Could not attach device %s\n", dev_id);
301 if (socket_init(disk) == -1) {
302 PrintError("could not initialize network connection\n");
303 v3_remove_device(dev);
307 PrintDebug("Registering Disk\n");
309 if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"),
310 &blk_ops, frontend_cfg, disk) == -1) {
311 PrintError("Could not connect %s to frontend\n", dev_id);
312 v3_remove_device(dev);
316 PrintDebug("intialization done\n");
322 device_register("NETDISK", disk_init)