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 30
vmod_rr_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed)
49
{
50
        struct vmod_directors_round_robin *rr;
51
52 30
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
53 30
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
54 30
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
55 30
        return (vdir_any_healthy(ctx, rr->vd, changed));
56
}
57
58
static VCL_BACKEND v_matchproto_(vdi_resolve_f)
59 40
vmod_rr_resolve(VRT_CTX, VCL_BACKEND dir)
60
{
61
        struct vmod_directors_round_robin *rr;
62
        unsigned u;
63 40
        VCL_BACKEND be = NULL;
64
        unsigned nxt;
65
66 40
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
67 40
        CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
68 40
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
69 40
        vdir_rdlock(rr->vd);
70 42
        for (u = 0; u < rr->vd->n_backend; u++) {
71 42
                nxt = rr->nxt % rr->vd->n_backend;
72 42
                rr->nxt = nxt + 1;
73 42
                be = rr->vd->backend[nxt];
74 42
                CHECK_OBJ_NOTNULL(be, DIRECTOR_MAGIC);
75 42
                if (VRT_Healthy(ctx, be, NULL))
76 40
                        break;
77
        }
78 40
        vdir_unlock(rr->vd);
79 40
        if (u == rr->vd->n_backend)
80 0
                be = NULL;
81 40
        return (be);
82
}
83
84
static void v_matchproto_(vdi_destroy_f)
85 3
vmod_rr_destroy(VCL_BACKEND dir)
86
{
87
        struct vmod_directors_round_robin *rr;
88
89 3
        CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
90 3
        vdir_delete(&rr->vd);
91 3
        FREE_OBJ(rr);
92 3
}
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 13
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 13
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
109 13
        AN(rrp);
110 13
        AZ(*rrp);
111 13
        ALLOC_OBJ(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
112 13
        AN(rr);
113 13
        *rrp = rr;
114 13
        vdir_new(ctx, &rr->vd, vcl_name, vmod_rr_methods, rr);
115 13
}
116
117
VCL_VOID v_matchproto_()
118 3
vmod_round_robin__fini(struct vmod_directors_round_robin **rrp)
119
{
120
        struct vmod_directors_round_robin *rr;
121
122 3
        TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
123 3
        VRT_DelDirector(&rr->vd->dir);
124 3
}
125
126
VCL_VOID v_matchproto_()
127 24
vmod_round_robin_add_backend(VRT_CTX,
128
    struct vmod_directors_round_robin *rr, VCL_BACKEND be)
129
{
130
131 24
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
132 24
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
133 24
        vdir_add_backend(ctx, rr->vd, be, 0.0);
134 24
}
135
136
VCL_VOID v_matchproto_()
137 6
vmod_round_robin_remove_backend(VRT_CTX,
138
    struct vmod_directors_round_robin *rr, VCL_BACKEND be)
139
{
140 6
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
141 6
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
142 6
        vdir_remove_backend(ctx, rr->vd, be, NULL);
143 6
}
144
145
VCL_BACKEND v_matchproto_()
146 39
vmod_round_robin_backend(VRT_CTX,
147
    struct vmod_directors_round_robin *rr)
148
{
149
150 39
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
151 39
        CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
152 39
        return (rr->vd->dir);
153
}