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); |
Willy Tarreau | b071079 | 2020-02-04 10:23:54 +0100 | [diff] [blame] | 58 | ret = NULL; |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 59 | goto out; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 60 | } |
Willy Tarreau | bd9a0a7 | 2011-10-23 21:14:29 +0200 | [diff] [blame] | 61 | #ifdef F_SETPIPE_SZ |
| 62 | if (global.tune.pipesize) |
| 63 | fcntl(pipefd[0], F_SETPIPE_SZ, global.tune.pipesize); |
| 64 | #endif |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 65 | ret->data = 0; |
| 66 | ret->prod = pipefd[1]; |
| 67 | ret->cons = pipefd[0]; |
| 68 | ret->next = NULL; |
| 69 | pipes_used++; |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 70 | out: |
| 71 | HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 72 | return ret; |
| 73 | } |
| 74 | |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 75 | static inline void __kill_pipe(struct pipe *p) |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 76 | { |
| 77 | close(p->prod); |
| 78 | close(p->cons); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 79 | pool_free(pool_head_pipe, p); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 80 | pipes_used--; |
| 81 | return; |
| 82 | } |
| 83 | |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 84 | /* destroy a pipe, possibly because an error was encountered on it. Its FDs |
| 85 | * will be closed and it will not be reinjected into the live pool. |
| 86 | */ |
| 87 | void kill_pipe(struct pipe *p) |
| 88 | { |
| 89 | HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock); |
| 90 | __kill_pipe(p); |
| 91 | HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock); |
| 92 | return; |
| 93 | } |
| 94 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 95 | /* put back a unused pipe into the live pool. If it still has data in it, it is |
| 96 | * closed and not reinjected into the live pool. The caller is not allowed to |
| 97 | * use it once released. |
| 98 | */ |
| 99 | void put_pipe(struct pipe *p) |
| 100 | { |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 101 | HA_SPIN_LOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 102 | if (p->data) { |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 103 | __kill_pipe(p); |
| 104 | goto out; |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 105 | } |
| 106 | p->next = pipes_live; |
| 107 | pipes_live = p; |
| 108 | pipes_free++; |
| 109 | pipes_used--; |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 110 | out: |
| 111 | HA_SPIN_UNLOCK(PIPES_LOCK, &pipes_lock); |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Willy Tarreau | 982b6e3 | 2009-01-25 13:49:53 +0100 | [diff] [blame] | 114 | /* |
| 115 | * Local variables: |
| 116 | * c-indent-level: 8 |
| 117 | * c-basic-offset: 8 |
| 118 | * End: |
| 119 | */ |