1 /*
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 
27 #define GET_NEXT_OFF(range, off) ((off) == (range)->size - 1 ? 0 : (off) + 1)
28 
29 /* return an available port from range <range>, or zero if none is left */
port_range_alloc_port(struct port_range * range)30 static inline int port_range_alloc_port(struct port_range *range)
31 {
32 	int ret;
33 	int get;
34 	int put;
35 
36 	get = HA_ATOMIC_LOAD(&range->get);
37 	do {
38 		put = HA_ATOMIC_LOAD(&range->put_t);
39 		if (unlikely(put == get))
40 			return 0;
41 		ret = range->ports[get];
42 	} while (!(HA_ATOMIC_CAS(&range->get, &get, GET_NEXT_OFF(range, get))));
43 	return ret;
44 }
45 
46 /* release port <port> into port range <range>. Does nothing if <port> is zero
47  * nor if <range> is null. The caller is responsible for marking the port
48  * unused by either setting the port to zero or the range to NULL.
49  */
port_range_release_port(struct port_range * range,int port)50 static inline void port_range_release_port(struct port_range *range, int port)
51 {
52 	int put;
53 
54 	if (!port || !range)
55 		return;
56 
57 	put = range->put_h;
58 	/* put_h is reserved for producers, so that they can each get a
59 	 * free slot, put_t is what is used by consumers to know if there's
60 	 * elements available or not
61 	 */
62 	/* First reserve or slot, we know the ring buffer can't be full,
63 	 * as we will only ever release port we allocated before
64 	 */
65 	while (!(HA_ATOMIC_CAS(&range->put_h, &put, GET_NEXT_OFF(range, put))));
66 	HA_ATOMIC_STORE(&range->ports[put], port);
67 	/* Wait until all the threads that got a slot before us are done */
68 	while ((volatile int)range->put_t != put)
69 		__ha_compiler_barrier();
70 	/* Let the world know we're done, and any potential consumer they
71 	 * can use that port.
72 	 */
73 	HA_ATOMIC_STORE(&range->put_t, GET_NEXT_OFF(range, put));
74 }
75 
76 /* return a new initialized port range of N ports. The ports are not
77  * filled in, it's up to the caller to do it.
78  */
port_range_alloc_range(int n)79 static inline struct port_range *port_range_alloc_range(int n)
80 {
81 	struct port_range *ret;
82 	ret = calloc(1, sizeof(struct port_range) +
83 		     (n + 1) * sizeof(((struct port_range *)0)->ports[0]));
84 	ret->size = n + 1;
85 	/* Start at the first free element */
86 	ret->put_h = ret->put_t = n;
87 	return ret;
88 }
89 
90 #endif /* _PROTO_PORT_RANGE_H */
91 
92 /*
93  * Local variables:
94  *  c-indent-level: 8
95  *  c-basic-offset: 8
96  * End:
97  */
98