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 1720
vmod_rr_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed)
50
{
51
        struct vmod_directors_round_robin *rr;
52
53 1720
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
54 1720
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
55 1720
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
56 1720
        return (vdir_any_healthy(ctx, rr->vd, changed));
57
}
58
59
static void v_matchproto_(vdi_list_f)
60 1440
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 1440
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
65 1440
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
66 1440
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
67 1440
        vdir_list(ctx, rr->vd, vsb, pflag, jflag, 0);
68 1440
}
69
70
static VCL_BACKEND v_matchproto_(vdi_resolve_f)
71 1720
vmod_rr_resolve(VRT_CTX, VCL_BACKEND dir)
72
{
73
        struct vmod_directors_round_robin *rr;
74
        unsigned u;
75 1720
        VCL_BACKEND be = NULL;
76
        unsigned nxt;
77
78 1720
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
79 1720
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
80 1720
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
81 1720
        vdir_rdlock(rr->vd);
82 1720
        nxt = rr->nxt;
83 1800
        for (u = 0; u < rr->vd->n_backend; u++) {
84 1800
                be = rr->vd->backend[nxt];
85 1800
                nxt++;
86 1800
                nxt %= rr->vd->n_backend;
87 1800
                CHECK_OBJ_NOTNULL(be, DIRECTOR_MAGIC);
88 1800
                if (VRT_Healthy(ctx, be, NULL))
89 1720
                        break;
90 80
        }
91 1720
        rr->nxt = nxt;
92 1720
        if (u == rr->vd->n_backend)
93 0
                be = NULL;
94 1720
        vdir_unlock(rr->vd);
95 1720
        return (be);
96
}
97
98
static void v_matchproto_(vdi_release_f)
99 200
vmod_rr_release(VCL_BACKEND dir)
100
{
101
        struct vmod_directors_round_robin *rr;
102
103 200
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
104 200
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
105 200
        vdir_release(rr->vd);
106 200
}
107
108
static void v_matchproto_(vdi_destroy_f)
109 200
vmod_rr_destroy(VCL_BACKEND dir)
110
{
111
        struct vmod_directors_round_robin *rr;
112
113 200
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
114 200
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
115 200
        vdir_delete(&rr->vd);
116 200
        FREE_OBJ(rr);
117 200
}
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 760
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 760
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
136 760
        AN(rrp);
137 760
        AZ(*rrp);
138 760
        ALLOC_OBJ(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
139 760
        AN(rr);
140 760
        *rrp = rr;
141 760
        vdir_new(ctx, &rr->vd, vcl_name, vmod_rr_methods, rr);
142 760
}
143
144
VCL_VOID v_matchproto_()
145 200
vmod_round_robin__fini(struct vmod_directors_round_robin **rrp)
146
{
147
        struct vmod_directors_round_robin *rr;
148
149 200
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
150 200
        VRT_DelDirector(&rr->vd->dir);
151 200
}
152
153
VCL_VOID v_matchproto_()
154 1640
vmod_round_robin_add_backend(VRT_CTX,
155
    struct vmod_directors_round_robin *rr, VCL_BACKEND be)
156
{
157
158 1640
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
159 1640
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
160 1640
        vdir_add_backend(ctx, rr->vd, be, 0.0);
161 1640
}
162
163
VCL_VOID v_matchproto_()
164 240
vmod_round_robin_remove_backend(VRT_CTX,
165
    struct vmod_directors_round_robin *rr, VCL_BACKEND be)
166
{
167 240
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
168 240
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
169 240
        vdir_remove_backend(ctx, rr->vd, be, NULL);
170 240
}
171
172
VCL_BACKEND v_matchproto_()
173 1880
vmod_round_robin_backend(VRT_CTX,
174
    struct vmod_directors_round_robin *rr)
175
{
176
177 1880
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
178 1880
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
179 1880
        return (rr->vd->dir);
180
}