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