blob: 31cd211ab4eed7f18af30ce9ef10f408ccd4c9b9 [file] [log] [blame]
Cody Schuffelen134ff032019-11-22 00:25:32 -08001#pragma once
2/*
3 * Copyright (C) 2017 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <memory>
19
20#include "common/libs/fs/shared_fd.h"
21#include "host/commands/ivserver/vsocsharedmem.h"
22
23namespace ivserver {
24
25// QemuClient manages individual QEmu connections using protocol specified
26// in documentation file distributed as part of QEmu 2.8 package under:
27// docs/specs/ivshmem-spec.txt
28// Alternatively, please point your browser to the following URL:
29// https://github.com/qemu/qemu/blob/stable-2.8/docs/specs/ivshmem-spec.txt
30class QemuClient final {
31 public:
32 static std::unique_ptr<QemuClient> New(const VSoCSharedMemory &shmem,
33 const cvd::SharedFD &connection);
34
35 cvd::SharedFD client_socket() const { return client_socket_; }
36
37 private:
38 enum QemuConstants : int64_t {
39 kIvshMemProtocolVersion = 0,
40 // Marker for the shared memory file
41 kSharedMem = -1,
42 // HostID is in fact a Peer ID and can take multiple values, depending on
43 // how many subsystems we would like Guest to talk to.
44 kHostID = 0,
45 // GuestID is a unique form of Peer ID (see above), that identifies newly
46 // created quest in IvSharedMem world.
47 kGuestID = 1
48 };
49
50 static_assert(QemuConstants::kHostID != QemuConstants::kGuestID,
51 "Guest and host should have different IDs");
52
53 cvd::SharedFD client_socket_;
54
55 // Initialize new instance of QemuClient.
56 QemuClient(cvd::SharedFD qemu_listener_socket);
57
58 // Once the QemuClient object is constructed, invoking the following
59 // method will perform the actual handshake with a QEMU instance.
60 bool PerformHandshake(const VSoCSharedMemory &shmem_fd);
61
62 // Send socket data to Qemu.
63 bool SendSocketInfo(QemuConstants message, const cvd::SharedFD &socket);
64
65 QemuClient(const QemuClient &) = delete;
66 QemuClient &operator=(const QemuClient &) = delete;
67};
68
69} // namespace ivserver