Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 |
| 17 | |
| 18 | namespace android { |
| 19 | |
| 20 | #pragma clang diagnostic push |
| 21 | #pragma clang diagnostic error "-Wpadded" |
| 22 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 23 | constexpr uint8_t RPC_CONNECTION_OPTION_INCOMING = 0x1; // default is outgoing |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 24 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 25 | constexpr uint32_t RPC_WIRE_ADDRESS_OPTION_CREATED = 1 << 0; // distinguish from '0' address |
| 26 | constexpr uint32_t RPC_WIRE_ADDRESS_OPTION_FOR_SERVER = 1 << 1; |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 27 | |
| 28 | struct RpcWireAddress { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 29 | uint32_t options; |
| 30 | uint32_t address; |
| 31 | |
| 32 | static inline RpcWireAddress fromRaw(uint64_t raw) { |
| 33 | return *reinterpret_cast<RpcWireAddress*>(&raw); |
| 34 | } |
| 35 | static inline uint64_t toRaw(RpcWireAddress addr) { |
| 36 | return *reinterpret_cast<uint64_t*>(&addr); |
| 37 | } |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 38 | }; |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 39 | static_assert(sizeof(RpcWireAddress) == sizeof(uint64_t)); |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 40 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 41 | /** |
| 42 | * This is sent to an RpcServer in order to request a new connection is created, |
| 43 | * either as part of a new session or an existing session |
| 44 | */ |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 45 | struct RpcConnectionHeader { |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 46 | uint32_t version; // maximum supported by caller |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 47 | uint8_t options; |
Frederick Mayle | 69a0c99 | 2022-05-26 20:38:39 +0000 | [diff] [blame] | 48 | uint8_t fileDescriptorTransportMode; |
| 49 | uint8_t reservered[8]; |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 50 | // Follows is sessionIdSize bytes. |
| 51 | // if size is 0, this is requesting a new session. |
| 52 | uint16_t sessionIdSize; |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 53 | }; |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 54 | static_assert(sizeof(RpcConnectionHeader) == 16); |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * In response to an RpcConnectionHeader which corresponds to a new session, |
| 58 | * this returns information to the server. |
| 59 | */ |
| 60 | struct RpcNewSessionResponse { |
| 61 | uint32_t version; // maximum supported by callee <= maximum supported by caller |
| 62 | uint8_t reserved[4]; |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 63 | }; |
Steven Moreland | 14cd3fe | 2021-09-02 16:18:14 -0700 | [diff] [blame] | 64 | static_assert(sizeof(RpcNewSessionResponse) == 8); |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 65 | |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 66 | #define RPC_CONNECTION_INIT_OKAY "cci" |
| 67 | |
| 68 | /** |
| 69 | * Whenever a client connection is setup, this is sent as the initial |
| 70 | * transaction. The main use of this is in order to control the timing for when |
Steven Moreland | 1b30429 | 2021-07-15 22:59:34 +0000 | [diff] [blame] | 71 | * an incoming connection is setup. |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 72 | */ |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 73 | struct RpcOutgoingConnectionInit { |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 74 | char msg[4]; |
| 75 | uint8_t reserved[4]; |
| 76 | }; |
Steven Moreland | 14cd3fe | 2021-09-02 16:18:14 -0700 | [diff] [blame] | 77 | static_assert(sizeof(RpcOutgoingConnectionInit) == 8); |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 78 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 79 | enum : uint32_t { |
| 80 | /** |
| 81 | * follows is RpcWireTransaction, if flags != oneway, reply w/ RPC_COMMAND_REPLY expected |
| 82 | */ |
| 83 | RPC_COMMAND_TRANSACT = 0, |
| 84 | /** |
| 85 | * follows is RpcWireReply |
| 86 | */ |
| 87 | RPC_COMMAND_REPLY, |
| 88 | /** |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 89 | * follows is RpcDecStrong |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 90 | * |
| 91 | * note - this in the protocol directly instead of as a 'special |
| 92 | * transaction' in order to keep it as lightweight as possible (we don't |
| 93 | * want to create a 'Parcel' object for every decref) |
| 94 | */ |
| 95 | RPC_COMMAND_DEC_STRONG, |
| 96 | }; |
| 97 | |
| 98 | /** |
| 99 | * These commands are used when the address in an RpcWireTransaction is zero'd |
| 100 | * out (no address). This allows the transact/reply flow to be used for |
| 101 | * additional server commands, without making the protocol for |
| 102 | * transactions/replies more complicated. |
| 103 | */ |
| 104 | enum : uint32_t { |
| 105 | RPC_SPECIAL_TRANSACT_GET_ROOT = 0, |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 106 | RPC_SPECIAL_TRANSACT_GET_MAX_THREADS = 1, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 107 | RPC_SPECIAL_TRANSACT_GET_SESSION_ID = 2, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 110 | // serialization is like: |
| 111 | // |RpcWireHeader|struct desginated by 'command'| (over and over again) |
Frederick Mayle | ffe9ac2 | 2022-06-30 02:07:36 +0000 | [diff] [blame] | 112 | // |
| 113 | // When file descriptors are included in out-of-band data (e.g. in unix domain |
| 114 | // sockets), they are always paired with the RpcWireHeader bytes of the |
| 115 | // transaction or reply the file descriptors belong to. |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 116 | |
| 117 | struct RpcWireHeader { |
| 118 | uint32_t command; // RPC_COMMAND_* |
| 119 | uint32_t bodySize; |
| 120 | |
| 121 | uint32_t reserved[2]; |
| 122 | }; |
Steven Moreland | 14cd3fe | 2021-09-02 16:18:14 -0700 | [diff] [blame] | 123 | static_assert(sizeof(RpcWireHeader) == 16); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 124 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 125 | struct RpcDecStrong { |
| 126 | RpcWireAddress address; |
| 127 | uint32_t amount; |
| 128 | uint32_t reserved; |
| 129 | }; |
| 130 | static_assert(sizeof(RpcDecStrong) == 16); |
| 131 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 132 | struct RpcWireTransaction { |
| 133 | RpcWireAddress address; |
| 134 | uint32_t code; |
| 135 | uint32_t flags; |
| 136 | |
| 137 | uint64_t asyncNumber; |
| 138 | |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 139 | // The size of the Parcel data directly following RpcWireTransaction. |
| 140 | uint32_t parcelDataSize; |
| 141 | |
| 142 | uint32_t reserved[3]; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 143 | |
Devin Moore | 139913a | 2021-08-13 19:59:16 +0000 | [diff] [blame] | 144 | uint8_t data[]; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 145 | }; |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 146 | static_assert(sizeof(RpcWireTransaction) == 40); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 147 | |
| 148 | struct RpcWireReply { |
| 149 | int32_t status; // transact return |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 150 | |
| 151 | // -- Fields below only transmitted starting at protocol version 1 -- |
| 152 | |
| 153 | // The size of the Parcel data directly following RpcWireReply. |
| 154 | uint32_t parcelDataSize; |
| 155 | |
| 156 | uint32_t reserved[3]; |
| 157 | |
| 158 | // Byte size of RpcWireReply in the wire protocol. |
| 159 | static size_t wireSize(uint32_t protocolVersion) { |
| 160 | if (protocolVersion == 0) { |
| 161 | return sizeof(int32_t); |
| 162 | } |
| 163 | return sizeof(RpcWireReply); |
| 164 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 165 | }; |
Frederick Mayle | dc07cf8 | 2022-05-26 20:30:12 +0000 | [diff] [blame] | 166 | static_assert(sizeof(RpcWireReply) == 20); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 167 | |
| 168 | #pragma clang diagnostic pop |
| 169 | |
| 170 | } // namespace android |