1/* 2 * Copyright (c) 2014-2017 ARM Limited 3 * All rights reserved 4 * 5 * The license below extends only to copyright in the software and shall 6 * not be construed as granting a license to any other intellectual 7 * property including but not limited to intellectual property relating 8 * to a hardware implementation of the functionality of the software 9 * licensed hereunder. You may use the software subject to the license 10 * terms below provided that you ensure that this notice is replicated 11 * unmodified and in its entirety in all distributions of the software, 12 * modified or unmodified, in source code or in binary form. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions are 16 * met: redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer; 18 * redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution; 21 * neither the name of the copyright holders nor the names of its 22 * contributors may be used to endorse or promote products derived from 23 * this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * 37 * Authors: Andreas Sandberg 38 */ 39 40#ifndef __DEV_VIRTIO_FS9P_HH__ 41#define __DEV_VIRTIO_FS9P_HH__ 42 43#include <map> 44#include <memory> 45#include <string> 46 47#include "base/pollevent.hh" 48#include "dev/virtio/base.hh" 49 50struct VirtIO9PBaseParams; 51 52typedef uint8_t P9MsgType; 53typedef uint16_t P9Tag; 54 55struct P9MsgHeader { 56 /** Length including header */ 57 uint32_t len; 58 /** Message type */ 59 P9MsgType type; 60 /** Message tag */ 61 P9Tag tag; 62} M5_ATTR_PACKED; 63 64/** Convert p9 byte order (LE) to host byte order */ 65template <typename T> inline T 66p9toh(T v) { return letoh(v); } 67 68/** Convert host byte order to p9 byte order (LE) */ 69template <typename T> inline T 70htop9(T v) { return htole(v); } 71 72template <> inline P9MsgHeader 73p9toh(P9MsgHeader v) 74{ 75 v.len = p9toh(v.len); 76 v.type = p9toh(v.type); 77 v.tag = p9toh(v.tag); 78 return v; 79} 80 81template <> inline P9MsgHeader 82htop9(P9MsgHeader v) 83{ 84 v.len = htop9(v.len); 85 v.type = htop9(v.type); 86 v.tag = htop9(v.tag); 87 return v; 88} 89 90/** 91 * This class implements a VirtIO transport layer for the 9p network 92 * file system. 93 * 94 * The 9p VirtIO transport uses the following queues: 95 * -# 9p requests and replies 96 * 97 * Each 9p request and response is sent in its own descriptor 98 * chain. The guest initiates a transaction by packing a T message 99 * (see the 9p spec) into the first part of a descriptor chain. After 100 * the T message, the guest reserves space for the reply (R message) 101 * by including one or more writable descriptors. The server replies 102 * by writing an R message into the writable descriptors and putting 103 * the chain in the used ring (VirtQueue::produceDescriptor()). 104 * 105 * @see https://github.com/rustyrussell/virtio-spec 106 * @see https://github.com/ericvh/9p-rfc 107 * @see https://code.google.com/p/diod/wiki/protocol 108 */ 109class VirtIO9PBase : public VirtIODeviceBase 110{ 111 public: 112 typedef VirtIO9PBaseParams Params; 113 VirtIO9PBase(Params *params); 114 virtual ~VirtIO9PBase(); 115 116 void readConfig(PacketPtr pkt, Addr cfgOffset); 117 118 protected: 119 /** 120 * VirtIO 9p configuration structure 121 * 122 * @note The fields in this structure depend on the features 123 * exposed to the guest. 124 */ 125 struct Config { 126 uint16_t len; 127 char tag[]; 128 } M5_ATTR_PACKED; 129 130 /** Currently active configuration (host byte order) */ 131 std::unique_ptr<Config> config; 132 133 /** VirtIO device ID */ 134 static const DeviceId ID_9P = 0x09; 135 136 /** @{ 137 * @name Feature bits 138 */ 139 /** Device provides a name of the resource in its configuration */ 140 static const FeatureBits F_MOUNT_TAG = 0x01; 141 /** @} */ 142 143 protected: 144 /** 145 * Virtqueue for 9p requests 146 */ 147 class FSQueue : public VirtQueue 148 { 149 public: 150 FSQueue(PortProxy &proxy, uint16_t size, VirtIO9PBase &_parent) 151 : VirtQueue(proxy, size), parent(_parent) {} 152 virtual ~FSQueue() {} 153 154 void onNotifyDescriptor(VirtDescriptor *desc); 155 156 std::string name() const { return parent.name() + ".queue"; } 157 158 protected: 159 VirtIO9PBase &parent; 160 }; 161 162 FSQueue queue; 163 164 protected: 165 /** 166 * Handle incoming 9p RPC message. 167 * 168 * @param header 9p message header. 169 * @param data Pointer to data in message. 170 * @param size Size of data (excluding header) 171 */ 172 virtual void recvTMsg(const P9MsgHeader &header, const uint8_t *data, size_t size) = 0; 173 /** 174 * Send a 9p RPC message reply. 175 * 176 * @param header 9p message header. 177 * @param data Pointer to data in message. 178 * @param size Size of data (excluding header) 179 */ 180 void sendRMsg(const P9MsgHeader &header, const uint8_t *data, size_t size); 181 182 /** 183 * Dump a 9p RPC message on the debug output 184 * 185 * @param header 9p message header. 186 * @param data Pointer to data in message. 187 * @param size Size of data (excluding header) 188 */ 189 void dumpMsg(const P9MsgHeader &header, const uint8_t *data, size_t size); 190 191 private: 192 /** 193 * Map between 9p transaction tags and descriptors where they 194 * appeared. 195 * 196 * When handling asynchronous requests, we need to ensure that 197 * replies are posted in the same descriptor as queries. The 9p 198 * RPC protocol uses the tag field in the header to match requests 199 * and replies, which we use here to find the relevant descriptor. 200 */ 201 std::map<P9Tag, VirtDescriptor *> pendingTransactions; 202}; 203 204struct VirtIO9PProxyParams; 205 206/** 207 * VirtIO 9p proxy base class. 208 * 209 * This base class provides basic functionality shared by different 9p 210 * proxy implementations. 211 */ 212class VirtIO9PProxy : public VirtIO9PBase 213{ 214 public: 215 typedef VirtIO9PProxyParams Params; 216 VirtIO9PProxy(Params *params); 217 virtual ~VirtIO9PProxy(); 218 219 void serialize(CheckpointOut &cp) const override; 220 void unserialize(CheckpointIn &cp) override; 221 222 protected: 223 void recvTMsg(const P9MsgHeader &header, const uint8_t *data, 224 size_t size) override; 225 226 /** Notification of pending data from server */ 227 void serverDataReady(); 228 229 /** 230 * Read data from the server behind the proxy. 231 * 232 * @note This method may return read fewer than len bytes. 233 * 234 * @param data Memory location to store results in. 235 * @param len Maximum length to read. 236 * @return Number of bytes read, -errno on failure. 237 */ 238 virtual ssize_t read(uint8_t *data, size_t len) = 0; 239 /** 240 * Write data to the server behind the proxy. 241 * 242 * @note This method may return write fewer than len bytes. 243 * 244 * @param data Pointer to data to write. 245 * @param len Maximum length to write. 246 * @return Number of bytes written, -errno on failure. 247 */ 248 virtual ssize_t write(const uint8_t *data, size_t len) = 0; 249 250 /** 251 * Convenience function that reads exactly len bytes. 252 * 253 * This method calls read until exactly len number of bytes has 254 * been read. A read() call is retried if the underlying syscall 255 * was interrupted. 256 * 257 * @param data Memory location to store results in. 258 * @param len Number of bytes to read. 259 */ 260 void readAll(uint8_t *data, size_t len); 261 /** 262 * Convenience function that writes exactly len bytes. 263 * 264 * This method calls write until exactly len number of bytes has 265 * been written. A write() call is retried if the underlying 266 * syscall was interrupted. 267 * 268 * @param data Data to write. 269 * @param len Number of bytes to write. 270 */ 271 void writeAll(const uint8_t *data, size_t len); 272 273 /** 274 * Bool to track if the device has been used or not. 275 * 276 * We need to keep track of if the device has been used as we are 277 * unable to checkpoint the device in the event that the device 278 * has been mounted in the guest system. This is due to the fact 279 * that we do not, and cannot, track the complete state across 280 * host and guest. 281 */ 282 bool deviceUsed; 283}; 284 285struct VirtIO9PDiodParams; 286 287/** 288 * VirtIO 9p proxy that communicates with the diod 9p server using 289 * pipes. 290 */ 291class VirtIO9PDiod : public VirtIO9PProxy 292{ 293 public: 294 typedef VirtIO9PDiodParams Params; 295 VirtIO9PDiod(Params *params); 296 virtual ~VirtIO9PDiod(); 297 298 void startup(); 299 300 protected: 301 /** 302 * Start diod and setup the communication pipes. 303 */ 304 void startDiod(); 305 306 ssize_t read(uint8_t *data, size_t len); 307 ssize_t write(const uint8_t *data, size_t len); 308 /** Kill the diod child process at the end of the simulation */ 309 void terminateDiod(); 310 311 private: 312 class DiodDataEvent : public PollEvent 313 { 314 public: 315 DiodDataEvent(VirtIO9PDiod &_parent, int fd, int event) 316 : PollEvent(fd, event), parent(_parent) {} 317 318 virtual ~DiodDataEvent() {}; 319 320 void process(int revent); 321 322 private: 323 VirtIO9PDiod &parent; 324 }; 325 326 /** fd for data pipe going to diod (write end) */ 327 int fd_to_diod; 328 /** fd for data pipe coming from diod (read end) */ 329 int fd_from_diod; 330 331 std::unique_ptr<DiodDataEvent> dataEvent; 332 333 /** PID of diod process */ 334 int diod_pid; 335}; 336 337struct VirtIO9PSocketParams; 338 339/** 340 * VirtIO 9p proxy that communicates with a 9p server over tcp 341 * sockets. 342 */ 343class VirtIO9PSocket : public VirtIO9PProxy 344{ 345 public: 346 typedef VirtIO9PSocketParams Params; 347 VirtIO9PSocket(Params *params); 348 virtual ~VirtIO9PSocket(); 349 350 void startup(); 351 352 protected: 353 /** 354 * Try to resolve the server name and connect to the 9p server. 355 */ 356 void connectSocket(); 357 358 /** 9p server disconnect notification */ 359 void socketDisconnect(); 360 361 ssize_t read(uint8_t *data, size_t len); 362 ssize_t write(const uint8_t *data, size_t len); 363 364 private: 365 class SocketDataEvent : public PollEvent 366 { 367 public: 368 SocketDataEvent(VirtIO9PSocket &_parent, int fd, int event) 369 : PollEvent(fd, event), parent(_parent) {} 370 371 virtual ~SocketDataEvent() {}; 372 373 void process(int revent); 374 375 private: 376 VirtIO9PSocket &parent; 377 }; 378 379 /** Socket connected to the 9p server */ 380 int fdSocket; 381 382 std::unique_ptr<SocketDataEvent> dataEvent; 383}; 384 385#endif // __DEV_VIRTIO_FS9P_HH__ 386