fs9p.hh revision 11168
1/* 2 * Copyright (c) 2014 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, size_t size); 224 225 /** Notification of pending data from server */ 226 void serverDataReady(); 227 228 /** 229 * Read data from the server behind the proxy. 230 * 231 * @note This method may return read fewer than len bytes. 232 * 233 * @param data Memory location to store results in. 234 * @param len Maximum length to read. 235 * @return Number of bytes read, -errno on failure. 236 */ 237 virtual ssize_t read(uint8_t *data, size_t len) = 0; 238 /** 239 * Write data to the server behind the proxy. 240 * 241 * @note This method may return write fewer than len bytes. 242 * 243 * @param data Pointer to data to write. 244 * @param len Maximum length to write. 245 * @return Number of bytes written, -errno on failure. 246 */ 247 virtual ssize_t write(const uint8_t *data, size_t len) = 0; 248 249 /** 250 * Convenience function that reads exactly len bytes. 251 * 252 * This method calls read until exactly len number of bytes has 253 * been read. A read() call is retried if the underlying syscall 254 * was interrupted. 255 * 256 * @param data Memory location to store results in. 257 * @param len Number of bytes to read. 258 */ 259 void readAll(uint8_t *data, size_t len); 260 /** 261 * Convenience function that writes exactly len bytes. 262 * 263 * This method calls write until exactly len number of bytes has 264 * been written. A write() call is retried if the underlying 265 * syscall was interrupted. 266 * 267 * @param data Data to write. 268 * @param len Number of bytes to write. 269 */ 270 void writeAll(const uint8_t *data, size_t len); 271}; 272 273struct VirtIO9PDiodParams; 274 275/** 276 * VirtIO 9p proxy that communicates with the diod 9p server using 277 * pipes. 278 */ 279class VirtIO9PDiod : public VirtIO9PProxy 280{ 281 public: 282 typedef VirtIO9PDiodParams Params; 283 VirtIO9PDiod(Params *params); 284 virtual ~VirtIO9PDiod(); 285 286 void startup(); 287 288 protected: 289 /** 290 * Start diod and setup the communication pipes. 291 */ 292 void startDiod(); 293 294 ssize_t read(uint8_t *data, size_t len); 295 ssize_t write(const uint8_t *data, size_t len); 296 297 private: 298 class DiodDataEvent : public PollEvent 299 { 300 public: 301 DiodDataEvent(VirtIO9PDiod &_parent, int fd, int event) 302 : PollEvent(fd, event), parent(_parent) {} 303 304 virtual ~DiodDataEvent() {}; 305 306 void process(int revent); 307 308 private: 309 VirtIO9PDiod &parent; 310 }; 311 312 /** fd for data pipe going to diod (write end) */ 313 int fd_to_diod; 314 /** fd for data pipe coming from diod (read end) */ 315 int fd_from_diod; 316 317 std::unique_ptr<DiodDataEvent> dataEvent; 318 319 /** PID of diod process */ 320 int diod_pid; 321}; 322 323struct VirtIO9PSocketParams; 324 325/** 326 * VirtIO 9p proxy that communicates with a 9p server over tcp 327 * sockets. 328 */ 329class VirtIO9PSocket : public VirtIO9PProxy 330{ 331 public: 332 typedef VirtIO9PSocketParams Params; 333 VirtIO9PSocket(Params *params); 334 virtual ~VirtIO9PSocket(); 335 336 void startup(); 337 338 protected: 339 /** 340 * Try to resolve the server name and connect to the 9p server. 341 */ 342 void connectSocket(); 343 344 /** 9p server disconnect notification */ 345 void socketDisconnect(); 346 347 ssize_t read(uint8_t *data, size_t len); 348 ssize_t write(const uint8_t *data, size_t len); 349 350 private: 351 class SocketDataEvent : public PollEvent 352 { 353 public: 354 SocketDataEvent(VirtIO9PSocket &_parent, int fd, int event) 355 : PollEvent(fd, event), parent(_parent) {} 356 357 virtual ~SocketDataEvent() {}; 358 359 void process(int revent); 360 361 private: 362 VirtIO9PSocket &parent; 363 }; 364 365 /** Socket connected to the 9p server */ 366 int fdSocket; 367 368 std::unique_ptr<SocketDataEvent> dataEvent; 369}; 370 371#endif // __DEV_VIRTIO_FS9P_HH__ 372