| | varnish-cache/vmod/vmod_directors_round_robin.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2013-2015 Varnish Software AS |
2 |
|
* All rights reserved. |
3 |
|
* |
4 |
|
* Author: Poul-Henning Kamp <phk@FreeBSD.org> |
5 |
|
* |
6 |
|
* SPDX-License-Identifier: BSD-2-Clause |
7 |
|
* |
8 |
|
* Redistribution and use in source and binary forms, with or without |
9 |
|
* modification, are permitted provided that the following conditions |
10 |
|
* are met: |
11 |
|
* 1. Redistributions of source code must retain the above copyright |
12 |
|
* notice, this list of conditions and the following disclaimer. |
13 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
|
* notice, this list of conditions and the following disclaimer in the |
15 |
|
* documentation and/or other materials provided with the distribution. |
16 |
|
* |
17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
27 |
|
* SUCH DAMAGE. |
28 |
|
*/ |
29 |
|
|
30 |
|
#include "config.h" |
31 |
|
|
32 |
|
#include <stdlib.h> |
33 |
|
#include <string.h> |
34 |
|
|
35 |
|
#include "cache/cache.h" |
36 |
|
|
37 |
|
#include "vcc_directors_if.h" |
38 |
|
|
39 |
|
#include "vmod_directors.h" |
40 |
|
|
41 |
|
struct vmod_directors_round_robin { |
42 |
|
unsigned magic; |
43 |
|
#define VMOD_DIRECTORS_ROUND_ROBIN_MAGIC 0xa80970cf |
44 |
|
struct vdir *vd; |
45 |
|
unsigned nxt; |
46 |
|
}; |
47 |
|
|
48 |
|
static VCL_BOOL v_matchproto_(vdi_healthy_f) |
49 |
602 |
vmod_rr_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed) |
50 |
|
{ |
51 |
|
struct vmod_directors_round_robin *rr; |
52 |
|
|
53 |
602 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
54 |
602 |
CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC); |
55 |
602 |
CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC); |
56 |
602 |
return (vdir_any_healthy(ctx, rr->vd, changed)); |
57 |
|
} |
58 |
|
|
59 |
|
static void v_matchproto_(vdi_list_f) |
60 |
504 |
vmod_rr_list(VRT_CTX, VCL_BACKEND dir, struct vsb *vsb, int pflag, int jflag) |
61 |
|
{ |
62 |
|
struct vmod_directors_round_robin *rr; |
63 |
|
|
64 |
504 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
65 |
504 |
CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC); |
66 |
504 |
CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC); |
67 |
504 |
vdir_list(ctx, rr->vd, vsb, pflag, jflag, 0); |
68 |
504 |
} |
69 |
|
|
70 |
|
static VCL_BACKEND v_matchproto_(vdi_resolve_f) |
71 |
602 |
vmod_rr_resolve(VRT_CTX, VCL_BACKEND dir) |
72 |
|
{ |
73 |
|
struct vmod_directors_round_robin *rr; |
74 |
|
unsigned u; |
75 |
602 |
VCL_BACKEND be = NULL; |
76 |
|
unsigned nxt; |
77 |
|
|
78 |
602 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
79 |
602 |
CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC); |
80 |
602 |
CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC); |
81 |
602 |
vdir_rdlock(rr->vd); |
82 |
602 |
nxt = rr->nxt; |
83 |
630 |
for (u = 0; u < rr->vd->n_backend; u++) { |
84 |
630 |
be = rr->vd->backend[nxt]; |
85 |
630 |
nxt++; |
86 |
630 |
nxt %= rr->vd->n_backend; |
87 |
630 |
CHECK_OBJ_NOTNULL(be, DIRECTOR_MAGIC); |
88 |
630 |
if (VRT_Healthy(ctx, be, NULL)) |
89 |
602 |
break; |
90 |
28 |
} |
91 |
602 |
rr->nxt = nxt; |
92 |
602 |
if (u == rr->vd->n_backend) |
93 |
0 |
be = NULL; |
94 |
602 |
vdir_unlock(rr->vd); |
95 |
602 |
return (be); |
96 |
|
} |
97 |
|
|
98 |
|
static void v_matchproto_(vdi_release_f) |
99 |
70 |
vmod_rr_release(VCL_BACKEND dir) |
100 |
|
{ |
101 |
|
struct vmod_directors_round_robin *rr; |
102 |
|
|
103 |
70 |
CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC); |
104 |
70 |
CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC); |
105 |
70 |
vdir_release(rr->vd); |
106 |
70 |
} |
107 |
|
|
108 |
|
static void v_matchproto_(vdi_destroy_f) |
109 |
70 |
vmod_rr_destroy(VCL_BACKEND dir) |
110 |
|
{ |
111 |
|
struct vmod_directors_round_robin *rr; |
112 |
|
|
113 |
70 |
CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC); |
114 |
70 |
CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC); |
115 |
70 |
vdir_delete(&rr->vd); |
116 |
70 |
FREE_OBJ(rr); |
117 |
70 |
} |
118 |
|
|
119 |
|
static const struct vdi_methods vmod_rr_methods[1] = {{ |
120 |
|
.magic = VDI_METHODS_MAGIC, |
121 |
|
.type = "round-robin", |
122 |
|
.healthy = vmod_rr_healthy, |
123 |
|
.resolve = vmod_rr_resolve, |
124 |
|
.release = vmod_rr_release, |
125 |
|
.destroy = vmod_rr_destroy, |
126 |
|
.list = vmod_rr_list |
127 |
|
}}; |
128 |
|
|
129 |
|
VCL_VOID v_matchproto_() |
130 |
266 |
vmod_round_robin__init(VRT_CTX, |
131 |
|
struct vmod_directors_round_robin **rrp, const char *vcl_name) |
132 |
|
{ |
133 |
|
struct vmod_directors_round_robin *rr; |
134 |
|
|
135 |
266 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
136 |
266 |
AN(rrp); |
137 |
266 |
AZ(*rrp); |
138 |
266 |
ALLOC_OBJ(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC); |
139 |
266 |
AN(rr); |
140 |
266 |
*rrp = rr; |
141 |
266 |
vdir_new(ctx, &rr->vd, vcl_name, vmod_rr_methods, rr); |
142 |
266 |
} |
143 |
|
|
144 |
|
VCL_VOID v_matchproto_() |
145 |
70 |
vmod_round_robin__fini(struct vmod_directors_round_robin **rrp) |
146 |
|
{ |
147 |
|
struct vmod_directors_round_robin *rr; |
148 |
|
|
149 |
70 |
TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC); |
150 |
70 |
VRT_DelDirector(&rr->vd->dir); |
151 |
70 |
} |
152 |
|
|
153 |
|
VCL_VOID v_matchproto_() |
154 |
574 |
vmod_round_robin_add_backend(VRT_CTX, |
155 |
|
struct vmod_directors_round_robin *rr, VCL_BACKEND be) |
156 |
|
{ |
157 |
|
|
158 |
574 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
159 |
574 |
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC); |
160 |
574 |
vdir_add_backend(ctx, rr->vd, be, 0.0); |
161 |
574 |
} |
162 |
|
|
163 |
|
VCL_VOID v_matchproto_() |
164 |
84 |
vmod_round_robin_remove_backend(VRT_CTX, |
165 |
|
struct vmod_directors_round_robin *rr, VCL_BACKEND be) |
166 |
|
{ |
167 |
84 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
168 |
84 |
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC); |
169 |
84 |
vdir_remove_backend(ctx, rr->vd, be, NULL); |
170 |
84 |
} |
171 |
|
|
172 |
|
VCL_BACKEND v_matchproto_() |
173 |
658 |
vmod_round_robin_backend(VRT_CTX, |
174 |
|
struct vmod_directors_round_robin *rr) |
175 |
|
{ |
176 |
|
|
177 |
658 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
178 |
658 |
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC); |
179 |
658 |
return (rr->vd->dir); |
180 |
|
} |