Debian Codegraph @

src/core/lib/iomgr/tcp_server_utils_posix.h

raw
1 /*
2 *
3 * Copyright 2017 gRPC authors.
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
19 #ifndef GRPC_CORE_LIB_IOMGR_TCP_SERVER_UTILS_POSIX_H
20 #define GRPC_CORE_LIB_IOMGR_TCP_SERVER_UTILS_POSIX_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include "src/core/lib/iomgr/ev_posix.h"
25 #include "src/core/lib/iomgr/resolve_address.h"
26 #include "src/core/lib/iomgr/socket_utils_posix.h"
27 #include "src/core/lib/iomgr/tcp_server.h"
28 #include "src/core/lib/resource_quota/memory_quota.h"
29
30 /* one listening port */
31 typedef struct grpc_tcp_listener {
32 int fd;
33 grpc_fd* emfd;
34 grpc_tcp_server* server;
35 grpc_resolved_address addr;
36 int port;
37 unsigned port_index;
38 unsigned fd_index;
39 grpc_closure read_closure;
40 grpc_closure destroyed_closure;
41 struct grpc_tcp_listener* next;
42 /* sibling is a linked list of all listeners for a given port. add_port and
43 clone_port place all new listeners in the same sibling list. A member of
44 the 'sibling' list is also a member of the 'next' list. The head of each
45 sibling list has is_sibling==0, and subsequent members of sibling lists
46 have is_sibling==1. is_sibling allows separate sibling lists to be
47 identified while iterating through 'next'. */
48 struct grpc_tcp_listener* sibling;
49 int is_sibling;
50 } grpc_tcp_listener;
51
52 /* the overall server */
53 struct grpc_tcp_server {
54 gpr_refcount refs;
55 /* Called whenever accept() succeeds on a server port. */
56 grpc_tcp_server_cb on_accept_cb = nullptr;
57 void* on_accept_cb_arg = nullptr;
58
59 gpr_mu mu;
60
61 /* active port count: how many ports are actually still listening */
62 size_t active_ports = 0;
63 /* destroyed port count: how many ports are completely destroyed */
64 size_t destroyed_ports = 0;
65
66 /* is this server shutting down? */
67 bool shutdown = false;
68 /* have listeners been shutdown? */
69 bool shutdown_listeners = false;
70 /* use SO_REUSEPORT */
71 bool so_reuseport = false;
72 /* expand wildcard addresses to a list of all local addresses */
73 bool expand_wildcard_addrs = false;
74
75 /* linked list of server ports */
76 grpc_tcp_listener* head = nullptr;
77 grpc_tcp_listener* tail = nullptr;
78 unsigned nports = 0;
79
80 /* List of closures passed to shutdown_starting_add(). */
81 grpc_closure_list shutdown_starting{nullptr, nullptr};
82
83 /* shutdown callback */
84 grpc_closure* shutdown_complete = nullptr;
85
86 /* all pollsets interested in new connections. The object pointed at is not
87 * owned by this struct */
88 const std::vector<grpc_pollset*>* pollsets = nullptr;
89
90 /* next pollset to assign a channel to */
91 gpr_atm next_pollset_to_assign = 0;
92
93 /* Contains config extracted from channel args for this server */
94 grpc_core::PosixTcpOptions options;
95
96 /* a handler for external connections, owned */
97 grpc_core::TcpServerFdHandler* fd_handler = nullptr;
98
99 /* used to create slice allocators for endpoints, owned */
100 grpc_core::MemoryQuotaRefPtr memory_quota;
101 };
102
103 /* If successful, add a listener to \a s for \a addr, set \a dsmode for the
104 socket, and return the \a listener. */
105 grpc_error_handle grpc_tcp_server_add_addr(grpc_tcp_server* s,
106 const grpc_resolved_address* addr,
107 unsigned port_index,
108 unsigned fd_index,
109 grpc_dualstack_mode* dsmode,
110 grpc_tcp_listener** listener);
111
112 /* Get all addresses assigned to network interfaces on the machine and create a
113 listener for each. requested_port is the port to use for every listener, or 0
114 to select one random port that will be used for every listener. Set *out_port
115 to the port selected. Return absl::OkStatus() only if all listeners were
116 added. */
117 grpc_error_handle grpc_tcp_server_add_all_local_addrs(grpc_tcp_server* s,
118 unsigned port_index,
119 int requested_port,
120 int* out_port);
121
122 /* Prepare a recently-created socket for listening. */
123 grpc_error_handle grpc_tcp_server_prepare_socket(
124 grpc_tcp_server*, int fd, const grpc_resolved_address* addr,
125 bool so_reuseport, int* port);
126 /* Ruturn true if the platform supports ifaddrs */
127 bool grpc_tcp_server_have_ifaddrs(void);
128
129 #endif /* GRPC_CORE_LIB_IOMGR_TCP_SERVER_UTILS_POSIX_H */