NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
io_message.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #ifndef APPLICATIONS_STORAGE_STORAGE_COMMON_IO_MESSAGE_HPP_
27 #define APPLICATIONS_STORAGE_STORAGE_COMMON_IO_MESSAGE_HPP_
28 
29 #include <cstddef>
30 #include <cstdint>
31 #include <string>
32 
33 #include <doca_error.h>
34 #include <doca_types.h>
35 
38 
39 namespace storage {
40 
41 /*
42  * Types of io message
43  */
44 enum class io_message_type : uint32_t {
45  result = 0,
46  read,
47  write,
48 };
49 
50 /*
51  * Absolute size of an io_message
52  */
53 size_t constexpr size_of_io_message = 64;
54 
55 /*
56  * Utility to allow for easy access and manipulation of each io_message field without having to process the full message
57  */
59 public:
60  /*
61  * Get the message type
62  *
63  * @buf [in]: Pointer to the message buffer
64  * @return: Type of the message
65  */
66  static inline io_message_type get_type(char const *buf)
67  {
68  io_message_type ret{};
69  static_cast<void>(storage::from_buffer(buf, reinterpret_cast<uint32_t &>(ret)));
70  return ret;
71  }
72 
73  /*
74  * Set the message type
75  *
76  * @type [in]: Type of the message
77  * @buf [in/out]: Pointer to the message buffer
78  */
79  static inline void set_type(io_message_type type, char *buf)
80  {
81  storage::to_buffer(buf, static_cast<uint32_t>(type));
82  }
83 
84  /*
85  * Get the message user data
86  *
87  * @buf [in]: Pointer to the message buffer
88  * @return: Message user data
89  */
90  static inline doca_data get_user_data(char const *buf)
91  {
92  doca_data ret{};
93  static_cast<void>(storage::from_buffer(buf + offsetof(layout, user_data), ret.u64));
94  return ret;
95  }
96 
97  /*
98  * Set the message user data
99  *
100  * @user_data [in]: User data value
101  * @buf [in/out]: Pointer to the message buffer
102  */
103  static inline void set_user_data(doca_data user_data, char *buf)
104  {
105  storage::to_buffer(buf + offsetof(layout, user_data), user_data.u64);
106  }
107 
108  /*
109  * Get the message correlation id
110  *
111  * @buf [in]: Pointer to the message buffer
112  * @return: Message correlation id
113  */
114  static inline uint32_t get_correlation_id(char const *buf)
115  {
116  uint32_t ret{};
117  static_cast<void>(storage::from_buffer(buf + offsetof(layout, correlation_id), ret));
118  return ret;
119  }
120 
121  /*
122  * Set the message correlation id
123  *
124  * @correlation_id [in]: Correlation id value
125  * @buf [in/out]: Pointer to the message buffer
126  */
127  static inline void set_correlation_id(uint32_t correlation_id, char *buf)
128  {
129  storage::to_buffer(buf + offsetof(layout, correlation_id), correlation_id);
130  }
131 
132  /*
133  * Get the message result
134  *
135  * @buf [in]: Pointer to the message buffer
136  * @return: Returned status code. Typically: DOCA_SUCCESS on success and DOCA_ERROR otherwise
137  */
138  static inline doca_error_t get_result(char const *buf)
139  {
140  doca_error_t ret{};
141  static_cast<void>(
142  storage::from_buffer(buf + offsetof(layout, result), reinterpret_cast<uint32_t &>(ret)));
143  return ret;
144  }
145 
146  /*
147  * Set the message result
148  *
149  * @result [in]: Result value
150  * @buf [in/out]: Pointer to the message buffer
151  */
152  static inline void set_result(doca_error_t result, char *buf)
153  {
154  storage::to_buffer(buf + offsetof(layout, result), static_cast<uint32_t>(result));
155  }
156 
157  /*
158  * Get the message io address
159  *
160  * @buf [in]: Pointer to the message buffer
161  * @return: Message io address
162  */
163  static inline uint64_t get_io_address(char const *buf)
164  {
165  uint64_t ret{};
166  static_cast<void>(storage::from_buffer(buf + offsetof(layout, io_address), ret));
167  return ret;
168  }
169 
170  /*
171  * Set the message io address
172  *
173  * @io_address [in]: Address value
174  * @buf [in/out]: Pointer to the message buffer
175  */
176  static inline void set_io_address(uint64_t io_address, char *buf)
177  {
178  storage::to_buffer(buf + offsetof(layout, io_address), io_address);
179  }
180 
181  /*
182  * Get the message io size
183  *
184  * @buf [in]: Pointer to the message buffer
185  * @return: Message io size
186  */
187  static inline uint32_t get_io_size(char const *buf)
188  {
189  uint32_t ret{};
190  static_cast<void>(storage::from_buffer(buf + offsetof(layout, io_size), ret));
191  return ret;
192  }
193 
194  /*
195  * Set the message io size
196  *
197  * @io_size [in]: Size value
198  * @buf [in/out]: Pointer to the message buffer
199  */
200  static inline void set_io_size(uint32_t io_size, char *buf)
201  {
202  storage::to_buffer(buf + offsetof(layout, io_size), io_size);
203  }
204 
205  /*
206  * Get the message io remote offset
207  *
208  * @buf [in]: Pointer to the message buffer
209  * @return: Message io output offset
210  */
211  static inline uint32_t get_remote_offset(char const *buf)
212  {
213  uint32_t ret{};
214  static_cast<void>(storage::from_buffer(buf + offsetof(layout, remote_offset), ret));
215  return ret;
216  }
217 
218  /*
219  * Set the message io remote offset
220  *
221  * @remote_offset [in]: Remote offset value
222  * @buf [in/out]: Pointer to the message buffer
223  */
224  static inline void set_remote_offset(uint32_t remote_offset, char *buf)
225  {
226  storage::to_buffer(buf + offsetof(layout, remote_offset), remote_offset);
227  }
228 
229 private:
230  /*
231  * Physical model of the message to automate the calculation of value offsets
232  */
233  struct alignas(storage::cache_line_size) layout {
234  uint32_t type; /* use 4 bytes for the layout to keep data members aligned */
235  uint32_t correlation_id;
236  doca_data user_data;
238  uint32_t io_size;
239  uint64_t io_address;
240  uint32_t remote_offset;
241  };
242 
243  static_assert(sizeof(layout) == storage::size_of_io_message,
244  "io_message_buffer size is supposed to be a single cache line");
245 };
246 
247 /*
248  * Convert an io_message to a string a user can read
249  *
250  * @io_message [in]: Buffer holding the io message
251  * @return: User readable string
252  */
253 std::string io_message_to_string(char const *io_message);
254 
255 } /* namespace storage */
256 
257 #endif /* APPLICATIONS_STORAGE_STORAGE_COMMON_IO_MESSAGE_HPP_ */
#define offsetof(t, d)
int32_t result
static uint32_t get_correlation_id(char const *buf)
Definition: io_message.hpp:114
static uint32_t get_io_size(char const *buf)
Definition: io_message.hpp:187
static void set_user_data(doca_data user_data, char *buf)
Definition: io_message.hpp:103
static doca_data get_user_data(char const *buf)
Definition: io_message.hpp:90
static void set_correlation_id(uint32_t correlation_id, char *buf)
Definition: io_message.hpp:127
static void set_type(io_message_type type, char *buf)
Definition: io_message.hpp:79
static uint32_t get_remote_offset(char const *buf)
Definition: io_message.hpp:211
static void set_io_address(uint64_t io_address, char *buf)
Definition: io_message.hpp:176
static io_message_type get_type(char const *buf)
Definition: io_message.hpp:66
static uint64_t get_io_address(char const *buf)
Definition: io_message.hpp:163
static void set_io_size(uint32_t io_size, char *buf)
Definition: io_message.hpp:200
static void set_result(doca_error_t result, char *buf)
Definition: io_message.hpp:152
static doca_error_t get_result(char const *buf)
Definition: io_message.hpp:138
static void set_remote_offset(uint32_t remote_offset, char *buf)
Definition: io_message.hpp:224
enum doca_error doca_error_t
DOCA API return codes.
constexpr size_t size_of_io_message
Definition: io_message.hpp:53
char const * from_buffer(char const *buffer, T &value)
std::string io_message_to_string(char const *buf)
Definition: io_message.cpp:50
char * to_buffer(char *buffer, uint8_t value)
uint8_t type
Definition: packets.h:0
Convenience type for representing opaque data.
Definition: doca_types.h:56
uint64_t u64
Definition: doca_types.h:58