Debian Codegraph @

crt/aws-c-io/include/aws/io/channel.h

raw symbols SCIP
1 #ifndef AWS_IO_CHANNEL_H
2 #define AWS_IO_CHANNEL_H
3 /**
4 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: Apache-2.0.
6 */
7
8 #include <aws/io/io.h>
9
10 #include <aws/common/statistics.h>
11 #include <aws/common/task_scheduler.h>
12
13 AWS_PUSH_SANE_WARNING_LEVEL
14
15 enum aws_channel_direction {
16 AWS_CHANNEL_DIR_READ,
17 AWS_CHANNEL_DIR_WRITE,
18 };
19
20 struct aws_channel;
21 struct aws_channel_slot;
22 struct aws_channel_handler;
23 struct aws_event_loop;
24 struct aws_event_loop_local_object;
25
26 typedef void(aws_channel_on_setup_completed_fn)(struct aws_channel *channel, int error_code, void *user_data);
27
28 /* Callback called when a channel is completely shutdown. error_code refers to the reason the channel was closed. */
29 typedef void(aws_channel_on_shutdown_completed_fn)(struct aws_channel *channel, int error_code, void *user_data);
30
31 struct aws_channel_slot {
32 struct aws_allocator *alloc;
33 struct aws_channel *channel;
34 struct aws_channel_slot *adj_left;
35 struct aws_channel_slot *adj_right;
36 struct aws_channel_handler *handler;
37 size_t window_size;
38 size_t upstream_message_overhead;
39 size_t current_window_update_batch_size;
40 };
41
42 struct aws_channel_task;
43 typedef void(aws_channel_task_fn)(struct aws_channel_task *channel_task, void *arg, enum aws_task_status status);
44
45 struct aws_channel_task {
46 struct aws_task wrapper_task;
47 aws_channel_task_fn *task_fn;
48 void *arg;
49 const char *type_tag;
50 struct aws_linked_list_node node;
51 };
52
53 struct aws_channel_handler_vtable {
54 /**
55 * Called by the channel when a message is available for processing in the read direction. It is your
56 * responsibility to call aws_mem_release(message->allocator, message); on message when you are finished with it.
57 * You must only call `aws_mem_release(message->allocator, message);` if the `process_read_message`
58 * returns AWS_OP_SUCCESS. In case of an error, you must not clean up the message and should just raise the error.
59 *
60 * Also keep in mind that your slot's internal window has been decremented. You'll want to call
61 * aws_channel_slot_increment_read_window() at some point in the future if you want to keep receiving data.
62 */
63 int (*process_read_message)(
64 struct aws_channel_handler *handler,
65 struct aws_channel_slot *slot,
66 struct aws_io_message *message);
67 /**
68 * Called by the channel when a message is available for processing in the write direction. It is your
69 * responsibility to call aws_mem_release(message->allocator, message); on message when you are finished with it.
70 * You must only call `aws_mem_release(message->allocator, message);` if the `process_read_message`
71 * returns AWS_OP_SUCCESS. In case of an error, you must not clean up the message and should just raise the error.
72 */
73 int (*process_write_message)(
74 struct aws_channel_handler *handler,
75 struct aws_channel_slot *slot,
76 struct aws_io_message *message);
77 /**
78 * Called by the channel when a downstream handler has issued a window increment. You'll want to update your
79 * internal state and likely propagate a window increment message of your own by calling
80 * 'aws_channel_slot_increment_read_window()'
81 */
82 int (*increment_read_window)(struct aws_channel_handler *handler, struct aws_channel_slot *slot, size_t size);
83
84 /**
85 * The channel calls shutdown on all handlers twice, once to shut down reading, and once to shut down writing.
86 * Shutdown always begins with the left-most handler, and proceeds to the right with dir set to
87 * AWS_CHANNEL_DIR_READ. Then shutdown is called on handlers from right to left with dir set to
88 * AWS_CHANNEL_DIR_WRITE.
89 *
90 * The shutdown process does not need to complete immediately and may rely on scheduled tasks.
91 * The handler must call aws_channel_slot_on_handler_shutdown_complete() when it is finished,
92 * which propagates shutdown to the next handler. If 'free_scarce_resources_immediately' is true,
93 * then resources vulnerable to denial-of-service attacks (such as sockets and file handles)
94 * must be closed immediately before the shutdown() call returns.
95 */
96 int (*shutdown)(
97 struct aws_channel_handler *handler,
98 struct aws_channel_slot *slot,
99 enum aws_channel_direction dir,
100 int error_code,
101 bool free_scarce_resources_immediately);
102
103 /**
104 * Called by the channel when the handler is added to a slot, to get the initial window size.
105 */
106 size_t (*initial_window_size)(struct aws_channel_handler *handler);
107
108 /** Called by the channel anytime a handler is added or removed, provides a hint for downstream
109 * handlers to avoid message fragmentation due to message overhead. */
110 size_t (*message_overhead)(struct aws_channel_handler *handler);
111
112 /**
113 * Clean up any resources and deallocate yourself. The shutdown process will already be completed before this
114 * function is called.
115 */
116 void (*destroy)(struct aws_channel_handler *handler);
117
118 /**
119 * Directs the channel handler to reset all of the internal statistics it tracks about itself.
120 */
121 void (*reset_statistics)(struct aws_channel_handler *handler);
122
123 /**
124 * Adds a pointer to the handler's internal statistics (if they exist) to a list of statistics structures
125 * associated with the channel's handler chain.
126 */
127 void (*gather_statistics)(struct aws_channel_handler *handler, struct aws_array_list *stats_list);
128
129 /*
130 * If this handler represents a source of data (like the socket_handler), then this will trigger a read
131 * from the data source.
132 */
133 void (*trigger_read)(struct aws_channel_handler *handler);
134 };
135
136 struct aws_channel_handler {
137 struct aws_channel_handler_vtable *vtable;
138 struct aws_allocator *alloc;
139 struct aws_channel_slot *slot;
140 void *impl;
141 };
142
143 /**
144 * Args for creating a new channel.
145 * event_loop to use for IO and tasks. on_setup_completed will be invoked when
146 * the setup process is finished It will be executed in the event loop's thread.
147 * on_shutdown_completed will be executed upon channel shutdown.
148 *
149 * enable_read_back_pressure toggles whether or not back pressure will be applied in the channel.
150 * Leave this option off unless you're using something like reactive-streams, since it is a slight throughput
151 * penalty.
152 *
153 * Unless otherwise
154 * specified all functions for channels and channel slots must be executed within that channel's event-loop's thread.
155 **/
156 struct aws_channel_options {
157 struct aws_event_loop *event_loop;
158 aws_channel_on_setup_completed_fn *on_setup_completed;
159 aws_channel_on_shutdown_completed_fn *on_shutdown_completed;
160 void *setup_user_data;
161 void *shutdown_user_data;
162 bool enable_read_back_pressure;
163 };
164
165 AWS_EXTERN_C_BEGIN
166
167 extern AWS_IO_API size_t g_aws_channel_max_fragment_size;
168
169 /**
170 * Initializes channel_task for use.
171 */
172 AWS_IO_API
173 void aws_channel_task_init(
174 struct aws_channel_task *channel_task,
175 aws_channel_task_fn *task_fn,
176 void *arg,
177 const char *type_tag);
178
179 /**
180 * Allocates new channel, Unless otherwise specified all functions for channels and channel slots must be executed
181 * within that channel's event-loop's thread. channel_options are copied.
182 */
183 AWS_IO_API
184 struct aws_channel *aws_channel_new(struct aws_allocator *allocator, const struct aws_channel_options *creation_args);
185
186 /**
187 * Mark the channel, along with all slots and handlers, for destruction.
188 * Must be called after shutdown has completed.
189 * Can be called from any thread assuming 'aws_channel_shutdown()' has completed.
190 * Note that memory will not be freed until all users which acquired holds on the channel via
191 * aws_channel_acquire_hold(), release them via aws_channel_release_hold().
192 */
193 AWS_IO_API
194 void aws_channel_destroy(struct aws_channel *channel);
195
196 /**
197 * Initiates shutdown of the channel. Shutdown will begin with the left-most slot. Each handler will invoke
198 * 'aws_channel_slot_on_handler_shutdown_complete' once they've finished their shutdown process for the read direction.
199 * Once the right-most slot has shutdown in the read direction, the process will start shutting down starting on the
200 * right-most slot. Once the left-most slot has shutdown in the write direction, 'callbacks->shutdown_completed' will be
201 * invoked in the event loop's thread.
202 *
203 * This function can be called from any thread.
204 */
205 AWS_IO_API
206 int aws_channel_shutdown(struct aws_channel *channel, int error_code);
207
208 /**
209 * Prevent a channel's memory from being freed.
210 * Any number of users may acquire a hold to prevent a channel and its handlers from being unexpectedly freed.
211 * Any user which acquires a hold must release it via aws_channel_release_hold().
212 * Memory will be freed once all holds are released and aws_channel_destroy() has been called.
213 */
214 AWS_IO_API
215 void aws_channel_acquire_hold(struct aws_channel *channel);
216
217 /**
218 * Release a hold on the channel's memory, allowing it to be freed.
219 * This may be called before or after aws_channel_destroy().
220 */
221 AWS_IO_API
222 void aws_channel_release_hold(struct aws_channel *channel);
223
224 /**
225 * Allocates and initializes a new slot for use with the channel. If this is the first slot in the channel, it will
226 * automatically be added to the channel as the first slot. For all subsequent calls on a given channel, the slot will
227 * need to be added to the channel via. the aws_channel_slot_insert_right(), aws_channel_slot_insert_end(), and
228 * aws_channel_slot_insert_left() APIs.
229 */
230 AWS_IO_API
231 struct aws_channel_slot *aws_channel_slot_new(struct aws_channel *channel);
232
233 /**
234 * Fetches the event loop the channel is a part of.
235 */
236 AWS_IO_API
237 struct aws_event_loop *aws_channel_get_event_loop(struct aws_channel *channel);
238
239 /**
240 * Fetches the current timestamp from the event-loop's clock, in nanoseconds.
241 */
242 AWS_IO_API
243 int aws_channel_current_clock_time(struct aws_channel *channel, uint64_t *time_nanos);
244
245 /**
246 * Retrieves an object by key from the event loop's local storage.
247 */
248 AWS_IO_API
249 int aws_channel_fetch_local_object(
250 struct aws_channel *channel,
251 const void *key,
252 struct aws_event_loop_local_object *obj);
253
254 /**
255 * Stores an object by key in the event loop's local storage.
256 */
257 AWS_IO_API
258 int aws_channel_put_local_object(
259 struct aws_channel *channel,
260 const void *key,
261 const struct aws_event_loop_local_object *obj);
262
263 /**
264 * Removes an object by key from the event loop's local storage.
265 */
266 AWS_IO_API
267 int aws_channel_remove_local_object(
268 struct aws_channel *channel,
269 const void *key,
270 struct aws_event_loop_local_object *removed_obj);
271
272 /**
273 * Acquires a message from the event loop's message pool. size_hint is merely a hint, it may be smaller than you
274 * requested and you are responsible for checking the bounds of it. If the returned message is not large enough, you
275 * must send multiple messages. This cannot fail, it never returns NULL.
276 */
277 AWS_IO_API
278 struct aws_io_message *aws_channel_acquire_message_from_pool(
279 struct aws_channel *channel,
280 enum aws_io_message_type message_type,
281 size_t size_hint);
282
283 /**
284 * Schedules a task to run on the event loop as soon as possible.
285 * This is the ideal way to move a task into the correct thread. It's also handy for context switches.
286 * This function is safe to call from any thread.
287 *
288 * If called from the channel's event loop, the task will get directly added to the run-now list.
289 * If called from outside the channel's event loop, the task will go into a cross-thread task queue.
290 *
291 * If tasks must be serialized relative to some source synchronization, you may not want to use this API
292 * because tasks submitted from the event loop thread can "jump ahead" of tasks submitted from external threads
293 * due to this optimization. If this is a problem, you can either refactor your submission logic or use
294 * the aws_channel_schedule_task_now_serialized variant which does not perform this optimization.
295 *
296 * The task should not be cleaned up or modified until its function is executed.
297 */
298 AWS_IO_API
299 void aws_channel_schedule_task_now(struct aws_channel *channel, struct aws_channel_task *task);
300
301 /**
302 * Schedules a task to run on the event loop as soon as possible.
303 *
304 * This variant always uses the cross thread queue rather than conditionally skipping it when already in
305 * the destination event loop. While not "optimal", this allows us to serialize task execution no matter where
306 * the task was submitted from: if you are submitting tasks from a critical section, the serialized order that you
307 * submit is guaranteed to be the order that they execute on the event loop.
308 *
309 * The task should not be cleaned up or modified until its function is executed.
310 */
311 AWS_IO_API
312 void aws_channel_schedule_task_now_serialized(struct aws_channel *channel, struct aws_channel_task *task);
313
314 /**
315 * Schedules a task to run on the event loop at the specified time.
316 * This is the ideal way to move a task into the correct thread. It's also handy for context switches.
317 * Use aws_channel_current_clock_time() to get the current time in nanoseconds.
318 * This function is safe to call from any thread.
319 *
320 * The task should not be cleaned up or modified until its function is executed.
321 */
322 AWS_IO_API
323 void aws_channel_schedule_task_future(
324 struct aws_channel *channel,
325 struct aws_channel_task *task,
326 uint64_t run_at_nanos);
327
328 /**
329 * Instrument a channel with a statistics handler. While instrumented with a statistics handler, the channel
330 * will periodically report per-channel-handler-specific statistics about handler performance and state.
331 *
332 * Assigning a statistics handler to a channel is a transfer of ownership -- the channel will clean up
333 * the handler appropriately. Statistics handlers may be changed dynamically (for example, the upgrade
334 * from a vanilla http channel to a websocket channel), but this function may only be called from the
335 * event loop thread that the channel is a part of.
336 *
337 * The first possible hook to set a statistics handler is the channel's creation callback.
338 */
339 AWS_IO_API
340 int aws_channel_set_statistics_handler(struct aws_channel *channel, struct aws_crt_statistics_handler *handler);
341
342 /**
343 * Returns true if the caller is on the event loop's thread. If false, you likely need to use
344 * aws_channel_schedule_task(). This function is safe to call from any thread.
345 */
346 AWS_IO_API
347 bool aws_channel_thread_is_callers_thread(struct aws_channel *channel);
348
349 /**
350 * Sets the handler for a slot, the slot will also call get_current_window_size() and propagate a window update
351 * upstream.
352 */
353 AWS_IO_API
354 int aws_channel_slot_set_handler(struct aws_channel_slot *slot, struct aws_channel_handler *handler);
355
356 /**
357 * Removes slot from the channel and deallocates the slot and its handler.
358 */
359 AWS_IO_API
360 int aws_channel_slot_remove(struct aws_channel_slot *slot);
361
362 /**
363 * Replaces remove with new_slot. Deallocates remove and its handler.
364 */
365 AWS_IO_API
366 int aws_channel_slot_replace(struct aws_channel_slot *remove, struct aws_channel_slot *new_slot);
367
368 /**
369 * inserts 'to_add' to the position immediately to the right of slot. Note that the first call to
370 * aws_channel_slot_new() adds it to the channel implicitly.
371 */
372 AWS_IO_API
373 int aws_channel_slot_insert_right(struct aws_channel_slot *slot, struct aws_channel_slot *to_add);
374
375 /**
376 * Inserts to 'to_add' the end of the channel. Note that the first call to
377 * aws_channel_slot_new() adds it to the channel implicitly.
378 */
379 AWS_IO_API
380 int aws_channel_slot_insert_end(struct aws_channel *channel, struct aws_channel_slot *to_add);
381
382 /**
383 * inserts 'to_add' to the position immediately to the left of slot. Note that the first call to
384 * aws_channel_slot_new() adds it to the channel implicitly.
385 */
386 AWS_IO_API
387 int aws_channel_slot_insert_left(struct aws_channel_slot *slot, struct aws_channel_slot *to_add);
388
389 /**
390 * Sends a message to the adjacent slot in the channel based on dir. Also does window size checking.
391 *
392 * NOTE: if this function returns an error code, it is the caller's responsibility to release message
393 * back to the pool. If this function returns AWS_OP_SUCCESS, the recipient of the message has taken
394 * ownership of the message. So, for example, don't release a message to the pool and then return an error.
395 * If you encounter an error condition in this case, shutdown the channel with the appropriate error code.
396 */
397 AWS_IO_API
398 int aws_channel_slot_send_message(
399 struct aws_channel_slot *slot,
400 struct aws_io_message *message,
401 enum aws_channel_direction dir);
402
403 /**
404 * Convenience function that invokes aws_channel_acquire_message_from_pool(),
405 * asking for the largest reasonable DATA message that can be sent in the write direction,
406 * with upstream overhead accounted for. This cannot fail, it never returns NULL.
407 */
408 AWS_IO_API
409 struct aws_io_message *aws_channel_slot_acquire_max_message_for_write(struct aws_channel_slot *slot);
410
411 /**
412 * Issues a window update notification upstream (to the left.)
413 */
414 AWS_IO_API
415 int aws_channel_slot_increment_read_window(struct aws_channel_slot *slot, size_t window);
416
417 /**
418 * Called by handlers once they have finished their shutdown in the 'dir' direction. Propagates the shutdown process
419 * to the next handler in the channel.
420 */
421 AWS_IO_API
422 int aws_channel_slot_on_handler_shutdown_complete(
423 struct aws_channel_slot *slot,
424 enum aws_channel_direction dir,
425 int err_code,
426 bool free_scarce_resources_immediately);
427
428 /**
429 * Initiates shutdown on slot. callbacks->on_shutdown_completed will be called
430 * once the shutdown process is completed.
431 */
432 AWS_IO_API
433 int aws_channel_slot_shutdown(
434 struct aws_channel_slot *slot,
435 enum aws_channel_direction dir,
436 int err_code,
437 bool free_scarce_resources_immediately);
438
439 /**
440 * Fetches the downstream read window. This gives you the information necessary to honor the read window. If you call
441 * send_message() and it exceeds this window, the message will be rejected.
442 */
443 AWS_IO_API
444 size_t aws_channel_slot_downstream_read_window(struct aws_channel_slot *slot);
445
446 /** Fetches the current overhead of upstream handlers. This provides a hint to avoid fragmentation if you care. */
447 AWS_IO_API
448 size_t aws_channel_slot_upstream_message_overhead(struct aws_channel_slot *slot);
449
450 /**
451 * Calls destroy on handler's vtable
452 */
453 AWS_IO_API
454 void aws_channel_handler_destroy(struct aws_channel_handler *handler);
455
456 /**
457 * Calls process_read_message on handler's vtable
458 */
459 AWS_IO_API
460 int aws_channel_handler_process_read_message(
461 struct aws_channel_handler *handler,
462 struct aws_channel_slot *slot,
463 struct aws_io_message *message);
464
465 /**
466 * Calls process_write_message on handler's vtable.
467 */
468 AWS_IO_API
469 int aws_channel_handler_process_write_message(
470 struct aws_channel_handler *handler,
471 struct aws_channel_slot *slot,
472 struct aws_io_message *message);
473
474 /**
475 * Calls on_window_update on handler's vtable.
476 */
477 AWS_IO_API
478 int aws_channel_handler_increment_read_window(
479 struct aws_channel_handler *handler,
480 struct aws_channel_slot *slot,
481 size_t size);
482
483 /**
484 * calls shutdown_direction on handler's vtable.
485 */
486 AWS_IO_API
487 int aws_channel_handler_shutdown(
488 struct aws_channel_handler *handler,
489 struct aws_channel_slot *slot,
490 enum aws_channel_direction dir,
491 int error_code,
492 bool free_scarce_resources_immediately);
493
494 /**
495 * Calls initial_window_size on handler's vtable.
496 */
497 AWS_IO_API
498 size_t aws_channel_handler_initial_window_size(struct aws_channel_handler *handler);
499
500 AWS_IO_API
501 struct aws_channel_slot *aws_channel_get_first_slot(struct aws_channel *channel);
502
503 /**
504 * A way for external processes to force a read by the data-source channel handler. Necessary in certain cases, like
505 * when a server channel finishes setting up its initial handlers, a read may have already been triggered on the
506 * socket (the client's CLIENT_HELLO tls payload, for example) and absent further data/notifications, this data
507 * would never get processed.
508 */
509 AWS_IO_API
510 int aws_channel_trigger_read(struct aws_channel *channel);
511
512 AWS_EXTERN_C_END
513 AWS_POP_SANE_WARNING_LEVEL
514
515 #endif /* AWS_IO_CHANNEL_H */