blob: 970662df016de7570d86beb79ee2a15513e9c675 [file] [log] [blame]
Ryan Hainingb1899152018-01-29 15:50:37 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
Ryan Hainingb1899152018-01-29 15:50:37 -080017
18#include "common/vsoc/shm/base.h"
19#include "common/vsoc/shm/circqueue.h"
20#include "common/vsoc/shm/lock.h"
21#include "common/vsoc/shm/version.h"
22
23// Memory layout for wifi packet exchange region.
24namespace vsoc {
25namespace layout {
26namespace socket_forward {
27
28struct QueuePair {
29 // Traffic originating from host that proceeds towards guest.
30 CircularPacketQueue<16, 8192> host_to_guest;
31 // Traffic originating from guest that proceeds towards host.
32 CircularPacketQueue<16, 8192> guest_to_host;
33
34 enum QueueState : std::uint32_t {
35 INACTIVE = 0,
36 HOST_CONNECTED = 1,
37 BOTH_CONNECTED = 2,
38 HOST_CLOSED = 3,
39 GUEST_CLOSED = 4,
40 // If both are closed then the queue goes back to INACTIVE
41 // BOTH_CLOSED = 0,
42 };
43 QueueState queue_state_;
44 std::uint32_t port_;
45
46 SpinLock queue_state_lock_;
Greg Hartmanf16d58f2018-02-12 17:27:16 -080047
48 bool Recover() {
49 bool recovered = false;
50 bool rval = host_to_guest.Recover();
51 recovered = recovered || rval;
52 rval = guest_to_host.Recover();
53 recovered = recovered || rval;
54 rval = queue_state_lock_.Recover();
55 recovered = recovered || rval;
56 // TODO: Put queue_state_ and port_ recovery here, probably after grabbing
57 // the queue_state_lock_.
58 return recovered;
59 }
Ryan Hainingb1899152018-01-29 15:50:37 -080060};
61
62struct SocketForwardLayout : public RegionLayout {
Greg Hartmanf16d58f2018-02-12 17:27:16 -080063 bool Recover() {
64 bool recovered = false;
65 for (auto& i : queues_) {
66 bool rval = i.Recover();
67 recovered = recovered || rval;
68 }
69 //TODO: consider handling the sequence number here
70 return recovered;
71 }
72
Ryan Hainingb1899152018-01-29 15:50:37 -080073 QueuePair queues_[version_info::socket_forward::kNumQueues];
74 std::atomic_uint32_t seq_num;
75 static const char* region_name;
76};
77
78ASSERT_SHM_COMPATIBLE(SocketForwardLayout, socket_forward);
79
80} // namespace socket_forward
81} // namespace layout
82} // namespace vsoc