blob: c651fa862f4e9735373d8b2cf328fad336d4de3d [file] [log] [blame]
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02001/*
2 include/proto/port_range.h
3 This file defines everything needed to manage port ranges
4
5 Copyright (C) 2000-2009 Willy Tarreau - w@1wt.eu
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation, version 2.1
10 exclusively.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22#ifndef _PROTO_PORT_RANGE_H
23#define _PROTO_PORT_RANGE_H
24
25#include <types/port_range.h>
26
Olivier Houchard07425de2019-04-29 18:52:06 +020027#define GET_NEXT_OFF(range, off) ((off) == (range)->size - 1 ? 0 : (off) + 1)
28
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020029/* return an available port from range <range>, or zero if none is left */
30static inline int port_range_alloc_port(struct port_range *range)
31{
32 int ret;
Olivier Houchard07425de2019-04-29 18:52:06 +020033 int get;
34 int put;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020035
Olivier Houchard07425de2019-04-29 18:52:06 +020036 get = _HA_ATOMIC_LOAD(&range->get);
37 do {
38 /* barrier ot make sure get is loaded before put */
39 __ha_barrier_atomic_load();
40 put = _HA_ATOMIC_LOAD(&range->put_t);
41 if (unlikely(put == get))
42 return 0;
43 ret = range->ports[get];
44 } while (!(_HA_ATOMIC_CAS(&range->get, &get, GET_NEXT_OFF(range, get))));
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020045 return ret;
46}
47
48/* release port <port> into port range <range>. Does nothing if <port> is zero
49 * nor if <range> is null. The caller is responsible for marking the port
50 * unused by either setting the port to zero or the range to NULL.
51 */
52static inline void port_range_release_port(struct port_range *range, int port)
53{
Olivier Houchard07425de2019-04-29 18:52:06 +020054 int put;
55
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020056 if (!port || !range)
57 return;
58
Olivier Houchard07425de2019-04-29 18:52:06 +020059 put = range->put_h;
60 /* put_h is reserved for producers, so that they can each get a
61 * free slot, put_t is what is used by consumers to know if there's
62 * elements available or not
63 */
64 /* First reserve or slot, we know the ring buffer can't be full,
65 * as we will only ever release port we allocated before
66 */
67 while (!(_HA_ATOMIC_CAS(&range->put_h, &put, GET_NEXT_OFF(range, put))));
68 _HA_ATOMIC_STORE(&range->ports[put], port);
69 /* Barrier to make sure the new port is visible before we change put_t */
70 __ha_barrier_atomic_store();
71 /* Wait until all the threads that got a slot before us are done */
72 while ((volatile int)range->put_t != put)
73 __ha_compiler_barrier();
74 /* Let the world know we're done, and any potential consumer they
75 * can use that port.
76 */
77 _HA_ATOMIC_STORE(&range->put_t, GET_NEXT_OFF(range, put));
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020078}
79
80/* return a new initialized port range of N ports. The ports are not
81 * filled in, it's up to the caller to do it.
82 */
83static inline struct port_range *port_range_alloc_range(int n)
84{
85 struct port_range *ret;
86 ret = calloc(1, sizeof(struct port_range) +
Olivier Houchard07425de2019-04-29 18:52:06 +020087 (n + 1) * sizeof(((struct port_range *)0)->ports[0]));
88 ret->size = n + 1;
89 /* Start at the first free element */
90 ret->put_h = ret->put_t = n;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020091 return ret;
92}
93
94#endif /* _PROTO_PORT_RANGE_H */
95
96/*
97 * Local variables:
98 * c-indent-level: 8
99 * c-basic-offset: 8
100 * End:
101 */