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 | 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 | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 29 | int pipes_used = 0; /* # of pipes in use (2 fds each) */ |
| 30 | int pipes_free = 0; /* # of pipes unused */ |
| 31 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 32 | /* return a pre-allocated empty pipe. Try to allocate one if there isn't any |
| 33 | * left. NULL is returned if a pipe could not be allocated. |
| 34 | */ |
| 35 | struct pipe *get_pipe() |
| 36 | { |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 37 | struct pipe *ret = NULL; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 38 | int pipefd[2]; |
| 39 | |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 40 | HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 41 | if (likely(pipes_live)) { |
| 42 | ret = pipes_live; |
| 43 | pipes_live = pipes_live->next; |
| 44 | pipes_free--; |
| 45 | pipes_used++; |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 46 | goto out; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | if (pipes_used >= global.maxpipes) |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 50 | goto out; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 51 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 52 | ret = pool_alloc(pool_head_pipe); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 53 | if (!ret) |
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 | if (pipe(pipefd) < 0) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 57 | pool_free(pool_head_pipe, ret); |
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 | bd9a0a7 | 2011-10-23 21:14:29 +0200 | [diff] [blame] | 60 | #ifdef F_SETPIPE_SZ |
| 61 | if (global.tune.pipesize) |
| 62 | fcntl(pipefd[0], F_SETPIPE_SZ, global.tune.pipesize); |
| 63 | #endif |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 64 | ret->data = 0; |
| 65 | ret->prod = pipefd[1]; |
| 66 | ret->cons = pipefd[0]; |
| 67 | ret->next = NULL; |
| 68 | pipes_used++; |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 69 | out: |
| 70 | HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 71 | return ret; |
| 72 | } |
| 73 | |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 74 | static inline void __kill_pipe(struct pipe *p) |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 75 | { |
| 76 | close(p->prod); |
| 77 | close(p->cons); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 78 | pool_free(pool_head_pipe, p); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 79 | pipes_used--; |
| 80 | return; |
| 81 | } |
| 82 | |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 83 | /* destroy a pipe, possibly because an error was encountered on it. Its FDs |
| 84 | * will be closed and it will not be reinjected into the live pool. |
| 85 | */ |
| 86 | void kill_pipe(struct pipe *p) |
| 87 | { |
| 88 | HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock); |
| 89 | __kill_pipe(p); |
| 90 | HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock); |
| 91 | return; |
| 92 | } |
| 93 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 94 | /* put back a unused pipe into the live pool. If it still has data in it, it is |
| 95 | * closed and not reinjected into the live pool. The caller is not allowed to |
| 96 | * use it once released. |
| 97 | */ |
| 98 | void put_pipe(struct pipe *p) |
| 99 | { |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 100 | HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 101 | if (p->data) { |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 102 | __kill_pipe(p); |
| 103 | goto out; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 104 | } |
| 105 | p->next = pipes_live; |
| 106 | pipes_live = p; |
| 107 | pipes_free++; |
| 108 | pipes_used--; |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 109 | out: |
| 110 | HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 113 | /* |
| 114 | * Local variables: |
| 115 | * c-indent-level: 8 |
| 116 | * c-basic-offset: 8 |
| 117 | * End: |
| 118 | */ |