Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 9ed560e | 2011-10-24 17:09:22 +0200 | [diff] [blame] | 14 | #include <fcntl.h> |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 15 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 16 | #include <haproxy/api.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 17 | #include <haproxy/global.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 18 | #include <haproxy/pipe-t.h> |
Willy Tarreau | d0ef439 | 2020-06-02 09:38:52 +0200 | [diff] [blame] | 19 | #include <haproxy/pool.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 20 | #include <haproxy/thread.h> |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 21 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 22 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 23 | DECLARE_STATIC_POOL(pool_head_pipe, "pipe", sizeof(struct pipe)); |
| 24 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 25 | struct pipe *pipes_live = NULL; /* pipes which are still ready to use */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 26 | |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 27 | __decl_spinlock(pipes_lock); /* lock used to protect pipes list */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 28 | |
Willy Tarreau | 160287b | 2020-01-29 11:05:50 +0100 | [diff] [blame] | 29 | static THREAD_LOCAL int local_pipes_free = 0; /* #cache objects */ |
| 30 | static THREAD_LOCAL struct pipe *local_pipes = NULL; |
| 31 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 32 | int pipes_used = 0; /* # of pipes in use (2 fds each) */ |
| 33 | int pipes_free = 0; /* # of pipes unused */ |
| 34 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 35 | /* 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 | */ |
| 38 | struct pipe *get_pipe() |
| 39 | { |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 40 | struct pipe *ret = NULL; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 41 | int pipefd[2]; |
| 42 | |
Willy Tarreau | 160287b | 2020-01-29 11:05:50 +0100 | [diff] [blame] | 43 | ret = local_pipes; |
| 44 | if (likely(ret)) { |
| 45 | local_pipes = ret->next; |
| 46 | local_pipes_free--; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 47 | HA_ATOMIC_DEC(&pipes_free); |
| 48 | HA_ATOMIC_INC(&pipes_used); |
Willy Tarreau | 160287b | 2020-01-29 11:05:50 +0100 | [diff] [blame] | 49 | goto out; |
| 50 | } |
| 51 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 52 | if (likely(pipes_live)) { |
Willy Tarreau | a945cfd | 2020-01-29 10:41:34 +0100 | [diff] [blame] | 53 | HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 54 | ret = pipes_live; |
Willy Tarreau | 876b411 | 2020-01-30 09:15:37 +0100 | [diff] [blame] | 55 | if (likely(ret)) |
Willy Tarreau | a945cfd | 2020-01-29 10:41:34 +0100 | [diff] [blame] | 56 | pipes_live = ret->next; |
Willy Tarreau | a945cfd | 2020-01-29 10:41:34 +0100 | [diff] [blame] | 57 | HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 876b411 | 2020-01-30 09:15:37 +0100 | [diff] [blame] | 58 | if (ret) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 59 | HA_ATOMIC_DEC(&pipes_free); |
| 60 | HA_ATOMIC_INC(&pipes_used); |
Willy Tarreau | a945cfd | 2020-01-29 10:41:34 +0100 | [diff] [blame] | 61 | goto out; |
Willy Tarreau | 876b411 | 2020-01-30 09:15:37 +0100 | [diff] [blame] | 62 | } |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 65 | HA_ATOMIC_INC(&pipes_used); |
Willy Tarreau | 876b411 | 2020-01-30 09:15:37 +0100 | [diff] [blame] | 66 | if (pipes_used + pipes_free >= global.maxpipes) |
| 67 | goto fail; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 68 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 69 | ret = pool_alloc(pool_head_pipe); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 70 | if (!ret) |
Willy Tarreau | 876b411 | 2020-01-30 09:15:37 +0100 | [diff] [blame] | 71 | goto fail; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 72 | |
Willy Tarreau | 876b411 | 2020-01-30 09:15:37 +0100 | [diff] [blame] | 73 | if (pipe(pipefd) < 0) |
| 74 | goto fail; |
| 75 | |
Willy Tarreau | bd9a0a7 | 2011-10-23 21:14:29 +0200 | [diff] [blame] | 76 | #ifdef F_SETPIPE_SZ |
| 77 | if (global.tune.pipesize) |
| 78 | fcntl(pipefd[0], F_SETPIPE_SZ, global.tune.pipesize); |
| 79 | #endif |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 80 | ret->data = 0; |
| 81 | ret->prod = pipefd[1]; |
| 82 | ret->cons = pipefd[0]; |
| 83 | ret->next = NULL; |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 84 | out: |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 85 | return ret; |
Willy Tarreau | 876b411 | 2020-01-30 09:15:37 +0100 | [diff] [blame] | 86 | fail: |
| 87 | pool_free(pool_head_pipe, ret); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 88 | HA_ATOMIC_DEC(&pipes_used); |
Willy Tarreau | 876b411 | 2020-01-30 09:15:37 +0100 | [diff] [blame] | 89 | return NULL; |
| 90 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 93 | /* 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 | */ |
| 96 | void kill_pipe(struct pipe *p) |
| 97 | { |
Willy Tarreau | a945cfd | 2020-01-29 10:41:34 +0100 | [diff] [blame] | 98 | close(p->prod); |
| 99 | close(p->cons); |
| 100 | pool_free(pool_head_pipe, p); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 101 | HA_ATOMIC_DEC(&pipes_used); |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 104 | /* 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 | */ |
| 108 | void put_pipe(struct pipe *p) |
| 109 | { |
Willy Tarreau | a945cfd | 2020-01-29 10:41:34 +0100 | [diff] [blame] | 110 | if (unlikely(p->data)) { |
| 111 | kill_pipe(p); |
| 112 | return; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 113 | } |
Willy Tarreau | a945cfd | 2020-01-29 10:41:34 +0100 | [diff] [blame] | 114 | |
Willy Tarreau | 876b411 | 2020-01-30 09:15:37 +0100 | [diff] [blame] | 115 | if (likely(local_pipes_free * global.nbthread < global.maxpipes - pipes_used)) { |
Willy Tarreau | 160287b | 2020-01-29 11:05:50 +0100 | [diff] [blame] | 116 | p->next = local_pipes; |
| 117 | local_pipes = p; |
| 118 | local_pipes_free++; |
Willy Tarreau | 876b411 | 2020-01-30 09:15:37 +0100 | [diff] [blame] | 119 | goto out; |
Willy Tarreau | 160287b | 2020-01-29 11:05:50 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Willy Tarreau | a945cfd | 2020-01-29 10:41:34 +0100 | [diff] [blame] | 122 | HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 123 | p->next = pipes_live; |
| 124 | pipes_live = p; |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 125 | HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 876b411 | 2020-01-30 09:15:37 +0100 | [diff] [blame] | 126 | out: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 127 | HA_ATOMIC_INC(&pipes_free); |
| 128 | HA_ATOMIC_DEC(&pipes_used); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 131 | /* |
| 132 | * Local variables: |
| 133 | * c-indent-level: 8 |
| 134 | * c-basic-offset: 8 |
| 135 | * End: |
| 136 | */ |