Debian Codegraph @

c/src/proactor/epoll_raw_connection.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 /* This is currently epoll implementation specific - and will need changing for the other proactors */
23
24 #include "epoll-internal.h"
25 #include "proactor-internal.h"
26 #include "raw_connection-internal.h"
27
28 #include <proton/proactor.h>
29 #include <proton/listener.h>
30 #include <proton/netaddr.h>
31 #include <proton/raw_connection.h>
32
33 #include <alloca.h>
34 #include <assert.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #include <sys/epoll.h>
40
41 /* epoll specific raw connection struct */
42 struct praw_connection_t {
43 task_t task;
44 struct pn_raw_connection_t raw_connection;
45 psocket_t psocket;
46 struct pn_netaddr_t local, remote; /* Actual addresses */
47 pmutex rearm_mutex; /* protects pconnection_rearm from out of order arming*/
48 pn_event_batch_t batch;
49 struct addrinfo *addrinfo; /* Resolved address list */
50 struct addrinfo *ai; /* Current connect address */
51 bool connected;
52 bool disconnected;
53 };
54
55 static void psocket_error(praw_connection_t *rc, int err, const char* msg) {
56 pn_condition_t *cond = rc->raw_connection.condition;
57 if (!pn_condition_is_set(cond)) { /* Preserve older error information */
58 strerrorbuf what;
59 pstrerror(err, what);
60 char addr[PN_MAX_ADDR];
61 pn_netaddr_str(&rc->remote, addr, sizeof(addr));
62 pn_condition_format(cond, PNI_IO_CONDITION, "%s - %s %s", what, msg, addr);
63 }
64 }
65
66 static void psocket_gai_error(praw_connection_t *rc, int gai_err, const char* what, const char *addr) {
67 pn_condition_format(rc->raw_connection.condition, PNI_IO_CONDITION, "%s - %s %s",
68 gai_strerror(gai_err), what, addr);
69 }
70
71 static void praw_connection_connected_lh(praw_connection_t *prc) {
72 // Need to check socket for connection error
73 prc->connected = true;
74 if (prc->addrinfo) {
75 freeaddrinfo(prc->addrinfo);
76 prc->addrinfo = NULL;
77 }
78 prc->ai = NULL;
79 socklen_t len = sizeof(prc->remote.ss);
80 (void)getpeername(prc->psocket.epoll_io.fd, (struct sockaddr*)&prc->remote.ss, &len);
81
82 pni_raw_connected(&prc->raw_connection);
83 }
84
85 /* multi-address connections may call pconnection_start multiple times with diffferent FDs */
86 static void praw_connection_start(praw_connection_t *prc, int fd) {
87 int efd = prc->task.proactor->epollfd;
88
89 /* Get the local socket name now, get the peer name in pconnection_connected */
90 socklen_t len = sizeof(prc->local.ss);
91 (void)getsockname(fd, (struct sockaddr*)&prc->local.ss, &len);
92
93 epoll_extended_t *ee = &prc->psocket.epoll_io;
94 if (ee->polling) { /* This is not the first attempt, stop polling and close the old FD */
95 int fd = ee->fd; /* Save fd, it will be set to -1 by stop_polling */
96 stop_polling(ee, efd);
97 pclosefd(prc->task.proactor, fd);
98 }
99 ee->fd = fd;
100 ee->wanted = EPOLLIN | EPOLLOUT;
101 start_polling(ee, efd); // TODO: check for error
102 }
103
104 /* Called on initial connect, and if connection fails to try another address */
105 static void praw_connection_maybe_connect_lh(praw_connection_t *prc) {
106 while (prc->ai) { /* Have an address */
107 struct addrinfo *ai = prc->ai;
108 prc->ai = prc->ai->ai_next; /* Move to next address in case this fails */
109 int fd = socket(ai->ai_family, SOCK_STREAM, 0);
110 if (fd >= 0) {
111 configure_socket(fd);
112 if (!connect(fd, ai->ai_addr, ai->ai_addrlen) || errno == EINPROGRESS) {
113
114 /* Until we finish connecting save away the address we're trying to connect to */
115 memcpy((struct sockaddr *) &prc->remote.ss, ai->ai_addr, ai->ai_addrlen);
116
117 praw_connection_start(prc, fd);
118 return; /* Async connection started */
119 } else {
120 close(fd);
121 }
122 }
123 /* connect failed immediately, go round the loop to try the next addr */
124 }
125 int err;
126 socklen_t errlen = sizeof(err);
127 getsockopt(prc->psocket.epoll_io.fd, SOL_SOCKET, SO_ERROR, (void *)&err, &errlen);
128 psocket_error(prc, err, "on connect");
129
130 freeaddrinfo(prc->addrinfo);
131 prc->addrinfo = NULL;
132 prc->disconnected = true;
133 }
134
135 //
136 // Raw socket API
137 //
138 static pn_event_t * pni_raw_batch_next(pn_event_batch_t *batch);
139
140 static void praw_connection_init(praw_connection_t *prc, pn_proactor_t *p, pn_raw_connection_t *rc) {
141 task_init(&prc->task, RAW_CONNECTION, p);
142 psocket_init(&prc->psocket, RAW_CONNECTION_IO);
143
144 prc->connected = false;
145 prc->disconnected = false;
146 prc->batch.next_event = pni_raw_batch_next;
147
148 pmutex_init(&prc->rearm_mutex);
149 }
150
151 static void praw_connection_cleanup(praw_connection_t *prc) {
152 int fd = prc->psocket.epoll_io.fd;
153 stop_polling(&prc->psocket.epoll_io, prc->task.proactor->epollfd);
154 if (fd != -1)
155 pclosefd(prc->task.proactor, fd);
156
157 lock(&prc->task.mutex);
158 bool can_free = proactor_remove(&prc->task);
159 unlock(&prc->task.mutex);
160 if (can_free) {
161 task_finalize(&prc->task);
162 free(prc);
163 }
164 // else proactor_disconnect logic owns prc and its final free
165 }
166
167 pn_raw_connection_t *pn_raw_connection(void) {
168 praw_connection_t *conn = (praw_connection_t*) calloc(1, sizeof(praw_connection_t));
169 if (!conn) return NULL;
170
171 pni_raw_initialize(&conn->raw_connection);
172
173 return &conn->raw_connection;
174 }
175
176 void pn_proactor_raw_connect(pn_proactor_t *p, pn_raw_connection_t *rc, const char *addr) {
177 assert(rc);
178 praw_connection_t *prc = containerof(rc, praw_connection_t, raw_connection);
179 praw_connection_init(prc, p, rc);
180 // TODO: check case of proactor shutting down
181
182 lock(&prc->task.mutex);
183 proactor_add(&prc->task);
184
185 bool notify = false;
186
187 const char *host;
188 const char *port;
189 size_t addrlen = strlen(addr);
190 char *addr_buf = (char*) alloca(addrlen+1);
191 pni_parse_addr(addr, addr_buf, addrlen+1, &host, &port);
192
193 int gai_error = pgetaddrinfo(host, port, 0, &prc->addrinfo);
194 if (!gai_error) {
195 prc->ai = prc->addrinfo;
196 praw_connection_maybe_connect_lh(prc); /* Start connection attempts */
197 if (prc->disconnected) notify = schedule(&prc->task);
198 } else {
199 psocket_gai_error(prc, gai_error, "connect to ", addr);
200 prc->disconnected = true;
201 notify = schedule(&prc->task);
202 lock(&p->task.mutex);
203 notify |= schedule_if_inactive(p);
204 unlock(&p->task.mutex);
205 }
206
207 /* We need to issue INACTIVE on immediate failure */
208 unlock(&prc->task.mutex);
209 if (notify) notify_poller(p);
210 }
211
212 void pn_listener_raw_accept(pn_listener_t *l, pn_raw_connection_t *rc) {
213 assert(rc);
214 pn_proactor_t *p = pn_listener_proactor(l);
215 praw_connection_t *prc = containerof(rc, praw_connection_t, raw_connection);
216 praw_connection_init(prc, p, rc);
217 // TODO: fuller sanity check on input args
218
219 int err = 0;
220 int fd = -1;
221 bool notify = false;
222 lock(&l->task.mutex);
223 if (l->task.closing)
224 err = EBADF;
225 else {
226 accepted_t *a = listener_accepted_next(l);
227 if (a) {
228 fd = a->accepted_fd;
229 a->accepted_fd = -1;
230 }
231 else err = EWOULDBLOCK;
232 }
233
234 proactor_add(&prc->task);
235
236 lock(&prc->task.mutex);
237 if (fd >= 0) {
238 configure_socket(fd);
239 praw_connection_start(prc, fd);
240 praw_connection_connected_lh(prc);
241 } else {
242 psocket_error(prc, err, "pn_listener_accept");
243 }
244
245 if (!l->task.working && listener_has_event(l)) {
246 notify = schedule(&l->task);
247 }
248 unlock(&prc->task.mutex);
249 unlock(&l->task.mutex);
250 if (notify) notify_poller(p);
251 }
252
253 const pn_netaddr_t *pn_raw_connection_local_addr(pn_raw_connection_t *rc) {
254 praw_connection_t *prc = containerof(rc, praw_connection_t, raw_connection);
255 if (!prc) return NULL;
256 return &prc->local;
257 }
258
259 const pn_netaddr_t *pn_raw_connection_remote_addr(pn_raw_connection_t *rc) {
260 praw_connection_t *prc = containerof(rc, praw_connection_t, raw_connection);
261 if (!prc) return NULL;
262 return &prc->remote;
263 }
264
265 void pn_raw_connection_wake(pn_raw_connection_t *rc) {
266 bool notify = false;
267 praw_connection_t *prc = containerof(rc, praw_connection_t, raw_connection);
268 pn_proactor_t *p = prc->task.proactor;
269 lock(&prc->task.mutex);
270 if (!prc->task.closing) {
271 notify = pni_task_wake(&prc->task);
272 }
273 unlock(&prc->task.mutex);
274 if (notify) notify_poller(p);
275 }
276
277 static inline void set_closed(pn_raw_connection_t *rc)
278 {
279 praw_connection_t *prc = containerof(rc, praw_connection_t, raw_connection);
280 lock(&prc->task.mutex);
281 prc->task.closing = true;
282 unlock(&prc->task.mutex);
283 }
284
285 void pn_raw_connection_close(pn_raw_connection_t *rc) {
286 set_closed(rc);
287 pni_raw_close(rc);
288 }
289
290 void pn_raw_connection_read_close(pn_raw_connection_t *rc) {
291 if (pn_raw_connection_is_write_closed(rc)) {
292 set_closed(rc);
293 }
294 pni_raw_read_close(rc);
295 }
296
297 void pn_raw_connection_write_close(pn_raw_connection_t *rc) {
298 if (pn_raw_connection_is_read_closed(rc)) {
299 set_closed(rc);
300 }
301 pni_raw_write_close(rc);
302 }
303
304 static pn_event_t *pni_raw_batch_next(pn_event_batch_t *batch) {
305 praw_connection_t *rc = containerof(batch, praw_connection_t, batch);
306 pn_raw_connection_t *raw = &rc->raw_connection;
307
308 // Check wake status every event processed
309 bool waking = false;
310 lock(&rc->task.mutex);
311 if (pni_task_wake_pending(&rc->task)) {
312 waking = true;
313 pni_task_wake_done(&rc->task);
314 }
315 unlock(&rc->task.mutex);
316 if (waking) pni_raw_wake(raw);
317
318 return pni_raw_event_next(raw);
319 }
320
321 task_t *pni_psocket_raw_task(psocket_t* ps) {
322 return &containerof(ps, praw_connection_t, psocket)->task;
323 }
324
325 psocket_t *pni_task_raw_psocket(task_t *t) {
326 return &containerof(t, praw_connection_t, task)->psocket;
327 }
328
329 praw_connection_t *pni_batch_raw_connection(pn_event_batch_t *batch) {
330 return (batch->next_event == pni_raw_batch_next) ?
331 containerof(batch, praw_connection_t, batch) : NULL;
332 }
333
334 task_t *pni_raw_connection_task(praw_connection_t *rc) {
335 return &rc->task;
336 }
337
338 static long snd(int fd, const void* b, size_t s) {
339 return send(fd, b, s, MSG_NOSIGNAL | MSG_DONTWAIT);
340 }
341
342 static long rcv(int fd, void* b, size_t s) {
343 return recv(fd, b, s, MSG_DONTWAIT);
344 }
345
346 static int shutr(int fd) {
347 return shutdown(fd, SHUT_RD);
348 }
349
350 static int shutw(int fd) {
351 return shutdown(fd, SHUT_WR);
352 }
353
354 static void set_error(pn_raw_connection_t *conn, const char *msg, int err) {
355 psocket_error(containerof(conn, praw_connection_t, raw_connection), err, msg);
356 }
357
358 pn_event_batch_t *pni_raw_connection_process(task_t *t, uint32_t io_events, bool sched_ready) {
359 praw_connection_t *rc = containerof(t, praw_connection_t, task);
360 lock(&rc->task.mutex);
361 int events = io_events;
362 int fd = rc->psocket.epoll_io.fd;
363 if (!rc->connected) {
364 if (events & (EPOLLHUP | EPOLLERR)) {
365 praw_connection_maybe_connect_lh(rc);
366 }
367 if (rc->disconnected) {
368 pni_raw_connect_failed(&rc->raw_connection);
369 unlock(&rc->task.mutex);
370 return &rc->batch;
371 }
372 if (events & (EPOLLHUP | EPOLLERR)) {
373 unlock(&rc->task.mutex);
374 return NULL;
375 }
376 praw_connection_connected_lh(rc);
377 }
378 unlock(&rc->task.mutex);
379
380 bool wake = false;
381 lock(&t->mutex);
382 t->working = true;
383 if (sched_ready) {
384 schedule_done(t);
385 if (pni_task_wake_pending(&rc->task)) {
386 wake = true;
387 pni_task_wake_done(&rc->task);
388 }
389 }
390 unlock(&t->mutex);
391
392 if (wake) pni_raw_wake(&rc->raw_connection);
393 if (events & EPOLLIN) pni_raw_read(&rc->raw_connection, fd, rcv, set_error);
394 if (events & EPOLLOUT) pni_raw_write(&rc->raw_connection, fd, snd, set_error);
395 return &rc->batch;
396 }
397
398 void pni_raw_connection_done(praw_connection_t *rc) {
399 bool notify = false;
400 bool ready = false;
401 lock(&rc->task.mutex);
402 pn_proactor_t *p = rc->task.proactor;
403 tslot_t *ts = rc->task.runner;
404 rc->task.working = false;
405 notify = pni_task_wake_pending(&rc->task) && schedule(&rc->task);
406 // The task may be in the ready state even if we've got no raw connection
407 // wakes outstanding because we dealt with it already in pni_raw_batch_next()
408 ready = rc->task.ready;
409 unlock(&rc->task.mutex);
410
411 pn_raw_connection_t *raw = &rc->raw_connection;
412 int fd = rc->psocket.epoll_io.fd;
413 pni_raw_process_shutdown(raw, fd, shutr, shutw);
414 int wanted =
415 (pni_raw_can_read(raw) ? EPOLLIN : 0) |
416 (pni_raw_can_write(raw) ? EPOLLOUT : 0);
417 if (wanted) {
418 rc->psocket.epoll_io.wanted = wanted;
419 rearm_polling(&rc->psocket.epoll_io, p->epollfd); // TODO: check for error
420 } else {
421 bool finished_disconnect = raw->state==conn_fini && !ready && !raw->disconnectpending;
422 if (finished_disconnect) {
423 // If we're closed and we've sent the disconnect then close
424 pni_raw_finalize(raw);
425 praw_connection_cleanup(rc);
426 }
427 }
428
429 lock(&p->sched_mutex);
430 tslot_t *resume_thread;
431 notify |= unassign_thread(p, ts, UNUSED, &resume_thread);
432 unlock(&p->sched_mutex);
433 if (notify) notify_poller(p);
434 if (resume_thread) pni_resume(p, resume_thread);
435 }