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