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 | |
| 16 | #include <common/config.h> |
Willy Tarreau | 3e5e417 | 2017-11-11 17:58:31 +0100 | [diff] [blame] | 17 | #include <common/hathreads.h> |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 18 | #include <common/memory.h> |
| 19 | |
| 20 | #include <types/global.h> |
| 21 | #include <types/pipe.h> |
| 22 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 23 | struct pool_head *pool_head_pipe = NULL; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 24 | struct pipe *pipes_live = NULL; /* pipes which are still ready to use */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 25 | |
| 26 | __decl_hathreads(HA_SPINLOCK_T pipes_lock); /* lock used to protect pipes list */ |
| 27 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 28 | int pipes_used = 0; /* # of pipes in use (2 fds each) */ |
| 29 | int pipes_free = 0; /* # of pipes unused */ |
| 30 | |
| 31 | /* allocate memory for the pipes */ |
| 32 | static void init_pipe() |
| 33 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 34 | pool_head_pipe = create_pool("pipe", sizeof(struct pipe), MEM_F_SHARED); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 35 | pipes_used = 0; |
| 36 | pipes_free = 0; |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 37 | HA_SPIN_INIT(&pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /* return a pre-allocated empty pipe. Try to allocate one if there isn't any |
| 41 | * left. NULL is returned if a pipe could not be allocated. |
| 42 | */ |
| 43 | struct pipe *get_pipe() |
| 44 | { |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 45 | struct pipe *ret = NULL; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 46 | int pipefd[2]; |
| 47 | |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 48 | HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 49 | if (likely(pipes_live)) { |
| 50 | ret = pipes_live; |
| 51 | pipes_live = pipes_live->next; |
| 52 | pipes_free--; |
| 53 | pipes_used++; |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 54 | goto out; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (pipes_used >= global.maxpipes) |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 58 | goto out; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 59 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 60 | ret = pool_alloc(pool_head_pipe); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 61 | if (!ret) |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 62 | goto out; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 63 | |
| 64 | if (pipe(pipefd) < 0) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 65 | pool_free(pool_head_pipe, ret); |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 66 | goto out; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 67 | } |
Willy Tarreau | bd9a0a7 | 2011-10-23 21:14:29 +0200 | [diff] [blame] | 68 | #ifdef F_SETPIPE_SZ |
| 69 | if (global.tune.pipesize) |
| 70 | fcntl(pipefd[0], F_SETPIPE_SZ, global.tune.pipesize); |
| 71 | #endif |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 72 | ret->data = 0; |
| 73 | ret->prod = pipefd[1]; |
| 74 | ret->cons = pipefd[0]; |
| 75 | ret->next = NULL; |
| 76 | pipes_used++; |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 77 | out: |
| 78 | HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 79 | return ret; |
| 80 | } |
| 81 | |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 82 | static void inline __kill_pipe(struct pipe *p) |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 83 | { |
| 84 | close(p->prod); |
| 85 | close(p->cons); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 86 | pool_free(pool_head_pipe, p); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 87 | pipes_used--; |
| 88 | return; |
| 89 | } |
| 90 | |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 91 | /* destroy a pipe, possibly because an error was encountered on it. Its FDs |
| 92 | * will be closed and it will not be reinjected into the live pool. |
| 93 | */ |
| 94 | void kill_pipe(struct pipe *p) |
| 95 | { |
| 96 | HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock); |
| 97 | __kill_pipe(p); |
| 98 | HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock); |
| 99 | return; |
| 100 | } |
| 101 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 102 | /* put back a unused pipe into the live pool. If it still has data in it, it is |
| 103 | * closed and not reinjected into the live pool. The caller is not allowed to |
| 104 | * use it once released. |
| 105 | */ |
| 106 | void put_pipe(struct pipe *p) |
| 107 | { |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 108 | HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 109 | if (p->data) { |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 110 | __kill_pipe(p); |
| 111 | goto out; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 112 | } |
| 113 | p->next = pipes_live; |
| 114 | pipes_live = p; |
| 115 | pipes_free++; |
| 116 | pipes_used--; |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 117 | out: |
| 118 | HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
| 122 | __attribute__((constructor)) |
| 123 | static void __pipe_module_init(void) |
| 124 | { |
| 125 | init_pipe(); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Local variables: |
| 130 | * c-indent-level: 8 |
| 131 | * c-basic-offset: 8 |
| 132 | * End: |
| 133 | */ |