blob: 59b8312ce32333154ab1b23a0cab5faffb623f12 [file] [log] [blame]
Willy Tarreau982b6e32009-01-25 13:49:53 +01001/*
2 * Pipe management
3 *
4 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <unistd.h>
Willy Tarreau9ed560e2011-10-24 17:09:22 +020014#include <fcntl.h>
Willy Tarreau982b6e32009-01-25 13:49:53 +010015
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020016#include <haproxy/api.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020017#include <haproxy/global.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020018#include <haproxy/thread.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020019#include <haproxy/pool.h>
Willy Tarreau982b6e32009-01-25 13:49:53 +010020
Willy Tarreau551271d2020-06-04 08:32:23 +020021#include <haproxy/pipe-t.h>
Willy Tarreau982b6e32009-01-25 13:49:53 +010022
Willy Tarreau8ceae722018-11-26 11:58:30 +010023DECLARE_STATIC_POOL(pool_head_pipe, "pipe", sizeof(struct pipe));
24
Willy Tarreau982b6e32009-01-25 13:49:53 +010025struct pipe *pipes_live = NULL; /* pipes which are still ready to use */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010026
Willy Tarreau86abe442018-11-25 20:12:18 +010027__decl_spinlock(pipes_lock); /* lock used to protect pipes list */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010028
Willy Tarreau160287b2020-01-29 11:05:50 +010029static THREAD_LOCAL int local_pipes_free = 0; /* #cache objects */
30static THREAD_LOCAL struct pipe *local_pipes = NULL;
31
Willy Tarreau982b6e32009-01-25 13:49:53 +010032int pipes_used = 0; /* # of pipes in use (2 fds each) */
33int pipes_free = 0; /* # of pipes unused */
34
Willy Tarreau982b6e32009-01-25 13:49:53 +010035/* return a pre-allocated empty pipe. Try to allocate one if there isn't any
36 * left. NULL is returned if a pipe could not be allocated.
37 */
38struct pipe *get_pipe()
39{
Emeric Brund8b3b652017-11-07 11:19:48 +010040 struct pipe *ret = NULL;
Willy Tarreau982b6e32009-01-25 13:49:53 +010041 int pipefd[2];
42
Willy Tarreau160287b2020-01-29 11:05:50 +010043 ret = local_pipes;
44 if (likely(ret)) {
45 local_pipes = ret->next;
46 local_pipes_free--;
47 HA_ATOMIC_SUB(&pipes_free, 1);
48 HA_ATOMIC_ADD(&pipes_used, 1);
49 goto out;
50 }
51
Willy Tarreau982b6e32009-01-25 13:49:53 +010052 if (likely(pipes_live)) {
Willy Tarreaua945cfd2020-01-29 10:41:34 +010053 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +010054 ret = pipes_live;
Willy Tarreau876b4112020-01-30 09:15:37 +010055 if (likely(ret))
Willy Tarreaua945cfd2020-01-29 10:41:34 +010056 pipes_live = ret->next;
Willy Tarreaua945cfd2020-01-29 10:41:34 +010057 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau876b4112020-01-30 09:15:37 +010058 if (ret) {
59 HA_ATOMIC_SUB(&pipes_free, 1);
60 HA_ATOMIC_ADD(&pipes_used, 1);
Willy Tarreaua945cfd2020-01-29 10:41:34 +010061 goto out;
Willy Tarreau876b4112020-01-30 09:15:37 +010062 }
Willy Tarreau982b6e32009-01-25 13:49:53 +010063 }
64
Willy Tarreau876b4112020-01-30 09:15:37 +010065 HA_ATOMIC_ADD(&pipes_used, 1);
66 if (pipes_used + pipes_free >= global.maxpipes)
67 goto fail;
Willy Tarreau982b6e32009-01-25 13:49:53 +010068
Willy Tarreaubafbe012017-11-24 17:34:44 +010069 ret = pool_alloc(pool_head_pipe);
Willy Tarreau982b6e32009-01-25 13:49:53 +010070 if (!ret)
Willy Tarreau876b4112020-01-30 09:15:37 +010071 goto fail;
Willy Tarreau982b6e32009-01-25 13:49:53 +010072
Willy Tarreau876b4112020-01-30 09:15:37 +010073 if (pipe(pipefd) < 0)
74 goto fail;
75
Willy Tarreaubd9a0a72011-10-23 21:14:29 +020076#ifdef F_SETPIPE_SZ
77 if (global.tune.pipesize)
78 fcntl(pipefd[0], F_SETPIPE_SZ, global.tune.pipesize);
79#endif
Willy Tarreau982b6e32009-01-25 13:49:53 +010080 ret->data = 0;
81 ret->prod = pipefd[1];
82 ret->cons = pipefd[0];
83 ret->next = NULL;
Emeric Brund8b3b652017-11-07 11:19:48 +010084 out:
Willy Tarreau982b6e32009-01-25 13:49:53 +010085 return ret;
Willy Tarreau876b4112020-01-30 09:15:37 +010086 fail:
87 pool_free(pool_head_pipe, ret);
88 HA_ATOMIC_SUB(&pipes_used, 1);
89 return NULL;
90
Willy Tarreau982b6e32009-01-25 13:49:53 +010091}
92
Emeric Brund8b3b652017-11-07 11:19:48 +010093/* destroy a pipe, possibly because an error was encountered on it. Its FDs
94 * will be closed and it will not be reinjected into the live pool.
95 */
96void kill_pipe(struct pipe *p)
97{
Willy Tarreaua945cfd2020-01-29 10:41:34 +010098 close(p->prod);
99 close(p->cons);
100 pool_free(pool_head_pipe, p);
101 HA_ATOMIC_SUB(&pipes_used, 1);
Emeric Brund8b3b652017-11-07 11:19:48 +0100102}
103
Willy Tarreau982b6e32009-01-25 13:49:53 +0100104/* put back a unused pipe into the live pool. If it still has data in it, it is
105 * closed and not reinjected into the live pool. The caller is not allowed to
106 * use it once released.
107 */
108void put_pipe(struct pipe *p)
109{
Willy Tarreaua945cfd2020-01-29 10:41:34 +0100110 if (unlikely(p->data)) {
111 kill_pipe(p);
112 return;
Willy Tarreau982b6e32009-01-25 13:49:53 +0100113 }
Willy Tarreaua945cfd2020-01-29 10:41:34 +0100114
Willy Tarreau876b4112020-01-30 09:15:37 +0100115 if (likely(local_pipes_free * global.nbthread < global.maxpipes - pipes_used)) {
Willy Tarreau160287b2020-01-29 11:05:50 +0100116 p->next = local_pipes;
117 local_pipes = p;
118 local_pipes_free++;
Willy Tarreau876b4112020-01-30 09:15:37 +0100119 goto out;
Willy Tarreau160287b2020-01-29 11:05:50 +0100120 }
121
Willy Tarreaua945cfd2020-01-29 10:41:34 +0100122 HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau982b6e32009-01-25 13:49:53 +0100123 p->next = pipes_live;
124 pipes_live = p;
Emeric Brund8b3b652017-11-07 11:19:48 +0100125 HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock);
Willy Tarreau876b4112020-01-30 09:15:37 +0100126 out:
127 HA_ATOMIC_ADD(&pipes_free, 1);
128 HA_ATOMIC_SUB(&pipes_used, 1);
Willy Tarreau982b6e32009-01-25 13:49:53 +0100129}
130
Willy Tarreau982b6e32009-01-25 13:49:53 +0100131/*
132 * Local variables:
133 * c-indent-level: 8
134 * c-basic-offset: 8
135 * End:
136 */