2 * Copyright (c) 2014-2017 ARM Limited
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.
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.
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.
37 * Authors: Andreas Sandberg
40 #ifndef __DEV_VIRTIO_FS9P_HH__
41 #define __DEV_VIRTIO_FS9P_HH__
47 #include "base/pollevent.hh"
48 #include "dev/virtio/base.hh"
50 struct VirtIO9PBaseParams;
52 typedef uint8_t P9MsgType;
53 typedef uint16_t P9Tag;
56 /** Length including header */
64 /** Convert p9 byte order (LE) to host byte order */
65 template <typename T> inline T
66 p9toh(T v) { return letoh(v); }
68 /** Convert host byte order to p9 byte order (LE) */
69 template <typename T> inline T
70 htop9(T v) { return htole(v); }
72 template <> inline P9MsgHeader
76 v.type = p9toh(v.type);
81 template <> inline P9MsgHeader
85 v.type = htop9(v.type);
91 * This class implements a VirtIO transport layer for the 9p network
94 * The 9p VirtIO transport uses the following queues:
95 * -# 9p requests and replies
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()).
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
109 class VirtIO9PBase : public VirtIODeviceBase
112 typedef VirtIO9PBaseParams Params;
113 VirtIO9PBase(Params *params);
114 virtual ~VirtIO9PBase();
116 void readConfig(PacketPtr pkt, Addr cfgOffset);
120 * VirtIO 9p configuration structure
122 * @note The fields in this structure depend on the features
123 * exposed to the guest.
130 /** Currently active configuration (host byte order) */
131 std::unique_ptr<Config> config;
133 /** VirtIO device ID */
134 static const DeviceId ID_9P = 0x09;
139 /** Device provides a name of the resource in its configuration */
140 static const FeatureBits F_MOUNT_TAG = 0x01;
145 * Virtqueue for 9p requests
147 class FSQueue : public VirtQueue
150 FSQueue(PortProxy &proxy, uint16_t size, VirtIO9PBase &_parent)
151 : VirtQueue(proxy, size), parent(_parent) {}
152 virtual ~FSQueue() {}
154 void onNotifyDescriptor(VirtDescriptor *desc);
156 std::string name() const { return parent.name() + ".queue"; }
159 VirtIO9PBase &parent;
166 * Handle incoming 9p RPC message.
168 * @param header 9p message header.
169 * @param data Pointer to data in message.
170 * @param size Size of data (excluding header)
172 virtual void recvTMsg(const P9MsgHeader &header, const uint8_t *data, size_t size) = 0;
174 * Send a 9p RPC message reply.
176 * @param header 9p message header.
177 * @param data Pointer to data in message.
178 * @param size Size of data (excluding header)
180 void sendRMsg(const P9MsgHeader &header, const uint8_t *data, size_t size);
183 * Dump a 9p RPC message on the debug output
185 * @param header 9p message header.
186 * @param data Pointer to data in message.
187 * @param size Size of data (excluding header)
189 void dumpMsg(const P9MsgHeader &header, const uint8_t *data, size_t size);
193 * Map between 9p transaction tags and descriptors where they
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.
201 std::map<P9Tag, VirtDescriptor *> pendingTransactions;
204 struct VirtIO9PProxyParams;
207 * VirtIO 9p proxy base class.
209 * This base class provides basic functionality shared by different 9p
210 * proxy implementations.
212 class VirtIO9PProxy : public VirtIO9PBase
215 typedef VirtIO9PProxyParams Params;
216 VirtIO9PProxy(Params *params);
217 virtual ~VirtIO9PProxy();
219 void serialize(CheckpointOut &cp) const override;
220 void unserialize(CheckpointIn &cp) override;
223 void recvTMsg(const P9MsgHeader &header, const uint8_t *data,
224 size_t size) override;
226 /** Notification of pending data from server */
227 void serverDataReady();
230 * Read data from the server behind the proxy.
232 * @note This method may return read fewer than len bytes.
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.
238 virtual ssize_t read(uint8_t *data, size_t len) = 0;
240 * Write data to the server behind the proxy.
242 * @note This method may return write fewer than len bytes.
244 * @param data Pointer to data to write.
245 * @param len Maximum length to write.
246 * @return Number of bytes written, -errno on failure.
248 virtual ssize_t write(const uint8_t *data, size_t len) = 0;
251 * Convenience function that reads exactly len bytes.
253 * This method calls read until exactly len number of bytes has
254 * been read. A read() call is retried if the underlying syscall
257 * @param data Memory location to store results in.
258 * @param len Number of bytes to read.
260 void readAll(uint8_t *data, size_t len);
262 * Convenience function that writes exactly len bytes.
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.
268 * @param data Data to write.
269 * @param len Number of bytes to write.
271 void writeAll(const uint8_t *data, size_t len);
274 * Bool to track if the device has been used or not.
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
285 struct VirtIO9PDiodParams;
288 * VirtIO 9p proxy that communicates with the diod 9p server using
291 class VirtIO9PDiod : public VirtIO9PProxy
294 typedef VirtIO9PDiodParams Params;
295 VirtIO9PDiod(Params *params);
296 virtual ~VirtIO9PDiod();
302 * Start diod and setup the communication pipes.
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();
312 class DiodDataEvent : public PollEvent
315 DiodDataEvent(VirtIO9PDiod &_parent, int fd, int event)
316 : PollEvent(fd, event), parent(_parent) {}
318 virtual ~DiodDataEvent() {};
320 void process(int revent);
323 VirtIO9PDiod &parent;
326 /** fd for data pipe going to diod (write end) */
328 /** fd for data pipe coming from diod (read end) */
331 std::unique_ptr<DiodDataEvent> dataEvent;
333 /** PID of diod process */
337 struct VirtIO9PSocketParams;
340 * VirtIO 9p proxy that communicates with a 9p server over tcp
343 class VirtIO9PSocket : public VirtIO9PProxy
346 typedef VirtIO9PSocketParams Params;
347 VirtIO9PSocket(Params *params);
348 virtual ~VirtIO9PSocket();
354 * Try to resolve the server name and connect to the 9p server.
356 void connectSocket();
358 /** 9p server disconnect notification */
359 void socketDisconnect();
361 ssize_t read(uint8_t *data, size_t len);
362 ssize_t write(const uint8_t *data, size_t len);
365 class SocketDataEvent : public PollEvent
368 SocketDataEvent(VirtIO9PSocket &_parent, int fd, int event)
369 : PollEvent(fd, event), parent(_parent) {}
371 virtual ~SocketDataEvent() {};
373 void process(int revent);
376 VirtIO9PSocket &parent;
379 /** Socket connected to the 9p server */
382 std::unique_ptr<SocketDataEvent> dataEvent;
385 #endif // __DEV_VIRTIO_FS9P_HH__