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.
38 #ifndef __DEV_VIRTIO_FS9P_HH__
39 #define __DEV_VIRTIO_FS9P_HH__
45 #include "base/pollevent.hh"
46 #include "dev/virtio/base.hh"
48 struct VirtIO9PBaseParams;
50 typedef uint8_t P9MsgType;
51 typedef uint16_t P9Tag;
54 /** Length including header */
62 /** Convert p9 byte order (LE) to host byte order */
63 template <typename T> inline T
64 p9toh(T v) { return letoh(v); }
66 /** Convert host byte order to p9 byte order (LE) */
67 template <typename T> inline T
68 htop9(T v) { return htole(v); }
70 template <> inline P9MsgHeader
74 v.type = p9toh(v.type);
79 template <> inline P9MsgHeader
83 v.type = htop9(v.type);
89 * This class implements a VirtIO transport layer for the 9p network
92 * The 9p VirtIO transport uses the following queues:
93 * -# 9p requests and replies
95 * Each 9p request and response is sent in its own descriptor
96 * chain. The guest initiates a transaction by packing a T message
97 * (see the 9p spec) into the first part of a descriptor chain. After
98 * the T message, the guest reserves space for the reply (R message)
99 * by including one or more writable descriptors. The server replies
100 * by writing an R message into the writable descriptors and putting
101 * the chain in the used ring (VirtQueue::produceDescriptor()).
103 * @see https://github.com/rustyrussell/virtio-spec
104 * @see https://github.com/ericvh/9p-rfc
105 * @see https://code.google.com/p/diod/wiki/protocol
107 class VirtIO9PBase : public VirtIODeviceBase
110 typedef VirtIO9PBaseParams Params;
111 VirtIO9PBase(Params *params);
112 virtual ~VirtIO9PBase();
114 void readConfig(PacketPtr pkt, Addr cfgOffset);
118 * VirtIO 9p configuration structure
120 * @note The fields in this structure depend on the features
121 * exposed to the guest.
128 /** Currently active configuration (host byte order) */
129 std::unique_ptr<Config> config;
131 /** VirtIO device ID */
132 static const DeviceId ID_9P = 0x09;
137 /** Device provides a name of the resource in its configuration */
138 static const FeatureBits F_MOUNT_TAG = 0x01;
143 * Virtqueue for 9p requests
145 class FSQueue : public VirtQueue
148 FSQueue(PortProxy &proxy, ByteOrder bo,
149 uint16_t size, VirtIO9PBase &_parent)
150 : VirtQueue(proxy, bo, size), parent(_parent) {}
151 virtual ~FSQueue() {}
153 void onNotifyDescriptor(VirtDescriptor *desc);
155 std::string name() const { return parent.name() + ".queue"; }
158 VirtIO9PBase &parent;
165 * Handle incoming 9p RPC message.
167 * @param header 9p message header.
168 * @param data Pointer to data in message.
169 * @param size Size of data (excluding header)
171 virtual void recvTMsg(const P9MsgHeader &header, const uint8_t *data, size_t size) = 0;
173 * Send a 9p RPC message reply.
175 * @param header 9p message header.
176 * @param data Pointer to data in message.
177 * @param size Size of data (excluding header)
179 void sendRMsg(const P9MsgHeader &header, const uint8_t *data, size_t size);
182 * Dump a 9p RPC message on the debug output
184 * @param header 9p message header.
185 * @param data Pointer to data in message.
186 * @param size Size of data (excluding header)
188 void dumpMsg(const P9MsgHeader &header, const uint8_t *data, size_t size);
192 * Map between 9p transaction tags and descriptors where they
195 * When handling asynchronous requests, we need to ensure that
196 * replies are posted in the same descriptor as queries. The 9p
197 * RPC protocol uses the tag field in the header to match requests
198 * and replies, which we use here to find the relevant descriptor.
200 std::map<P9Tag, VirtDescriptor *> pendingTransactions;
203 struct VirtIO9PProxyParams;
206 * VirtIO 9p proxy base class.
208 * This base class provides basic functionality shared by different 9p
209 * proxy implementations.
211 class VirtIO9PProxy : public VirtIO9PBase
214 typedef VirtIO9PProxyParams Params;
215 VirtIO9PProxy(Params *params);
216 virtual ~VirtIO9PProxy();
218 void serialize(CheckpointOut &cp) const override;
219 void unserialize(CheckpointIn &cp) override;
222 void recvTMsg(const P9MsgHeader &header, const uint8_t *data,
223 size_t size) override;
225 /** Notification of pending data from server */
226 void serverDataReady();
229 * Read data from the server behind the proxy.
231 * @note This method may return read fewer than len bytes.
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.
237 virtual ssize_t read(uint8_t *data, size_t len) = 0;
239 * Write data to the server behind the proxy.
241 * @note This method may return write fewer than len bytes.
243 * @param data Pointer to data to write.
244 * @param len Maximum length to write.
245 * @return Number of bytes written, -errno on failure.
247 virtual ssize_t write(const uint8_t *data, size_t len) = 0;
250 * Convenience function that reads exactly len bytes.
252 * This method calls read until exactly len number of bytes has
253 * been read. A read() call is retried if the underlying syscall
256 * @param data Memory location to store results in.
257 * @param len Number of bytes to read.
259 void readAll(uint8_t *data, size_t len);
261 * Convenience function that writes exactly len bytes.
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.
267 * @param data Data to write.
268 * @param len Number of bytes to write.
270 void writeAll(const uint8_t *data, size_t len);
273 * Bool to track if the device has been used or not.
275 * We need to keep track of if the device has been used as we are
276 * unable to checkpoint the device in the event that the device
277 * has been mounted in the guest system. This is due to the fact
278 * that we do not, and cannot, track the complete state across
284 struct VirtIO9PDiodParams;
287 * VirtIO 9p proxy that communicates with the diod 9p server using
290 class VirtIO9PDiod : public VirtIO9PProxy
293 typedef VirtIO9PDiodParams Params;
294 VirtIO9PDiod(Params *params);
295 virtual ~VirtIO9PDiod();
301 * Start diod and setup the communication pipes.
305 ssize_t read(uint8_t *data, size_t len);
306 ssize_t write(const uint8_t *data, size_t len);
307 /** Kill the diod child process at the end of the simulation */
308 void terminateDiod();
311 class DiodDataEvent : public PollEvent
314 DiodDataEvent(VirtIO9PDiod &_parent, int fd, int event)
315 : PollEvent(fd, event), parent(_parent) {}
317 virtual ~DiodDataEvent() {};
319 void process(int revent);
322 VirtIO9PDiod &parent;
325 /** fd for data pipe going to diod (write end) */
327 /** fd for data pipe coming from diod (read end) */
330 std::unique_ptr<DiodDataEvent> dataEvent;
332 /** PID of diod process */
336 struct VirtIO9PSocketParams;
339 * VirtIO 9p proxy that communicates with a 9p server over tcp
342 class VirtIO9PSocket : public VirtIO9PProxy
345 typedef VirtIO9PSocketParams Params;
346 VirtIO9PSocket(Params *params);
347 virtual ~VirtIO9PSocket();
353 * Try to resolve the server name and connect to the 9p server.
355 void connectSocket();
357 /** 9p server disconnect notification */
358 void socketDisconnect();
360 ssize_t read(uint8_t *data, size_t len);
361 ssize_t write(const uint8_t *data, size_t len);
364 class SocketDataEvent : public PollEvent
367 SocketDataEvent(VirtIO9PSocket &_parent, int fd, int event)
368 : PollEvent(fd, event), parent(_parent) {}
370 virtual ~SocketDataEvent() {};
372 void process(int revent);
375 VirtIO9PSocket &parent;
378 /** Socket connected to the 9p server */
381 std::unique_ptr<SocketDataEvent> dataEvent;
384 #endif // __DEV_VIRTIO_FS9P_HH__