Skip to content

Commit 4752409

Browse files
committed
Add CLI access to vmod objects with the "tell" command
Runtime modification of vmod object properties has been a long standing item on our wishlist. For example, #3652 is about a use case to change a custom director property, which is not covered by the director health state. Another simple example is to instruct a vmod object to emit log messages for tracing only when needed. This commit implements a basic interface for CLI access to vmod objects: VMOD objects now can have a single $Cli method, and the CLI gets a command to tell messages by invoking that method. vmod $Cli method ---------------- VMOD object classes gain a special method type $Cli, which is almost identical to $Method, except that only one method is supported per class, and only the specific signature $Cli INT cli_method(STRANDS) is supported. The cli method receives input via the single STRANDS arguments. It is expected to write output to ctx->msg and return the CLI status. cli tell command ---------------- The tell command takes an optional vcl name, object name and message to send. Individual message arguments are passed as constituents of the STRANDS argument to the object's cli method. demo ---- A new test case demos the functionality: The debug.obj class has gained a cli method which just returns the instance name followed by the original message: varnish> help tell 200 tell [<vcl>.]<object> <msg> ... Tell <msg> to <object> from the given <vcl> or the active vcl varnish> tell obj0 is there anybody out there? 200 obj0: is there anybody out there? varnish> tell whoisit hello? 300 No object named whoisit found varnish> tell obj0 fail 300 You asked me to fail
1 parent 041c5a9 commit 4752409

File tree

12 files changed

+261
-6
lines changed

12 files changed

+261
-6
lines changed

bin/varnishd/cache/cache_vcl.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,76 @@ vcl_cli_show(struct cli *cli, const char * const *av, void *priv)
995995
}
996996
}
997997

998+
// "tell [<vcl>.]<object> <msg> ...",
999+
1000+
static void v_matchproto_(cli_func_t)
1001+
vcl_cli_tell(struct cli *cli, const char * const *av, void *priv)
1002+
{
1003+
struct strands args;
1004+
const char *objname;
1005+
struct vcl *vcl;
1006+
struct vrt_ctx *ctx;
1007+
struct vsb *msg;
1008+
char *n;
1009+
int i;
1010+
1011+
AZ(priv);
1012+
ASSERT_CLI();
1013+
AN(av[2]);
1014+
AN(av[3]);
1015+
1016+
objname = strchr(av[2], '.');
1017+
if (objname) {
1018+
n = strndup(av[2], objname - av[2]);
1019+
objname++;
1020+
vcl = vcl_find(n);
1021+
if (vcl == NULL) {
1022+
VCLI_SetResult(cli, CLIS_CANT);
1023+
VCLI_Out(cli, "VCL %s not found", n);
1024+
REPLACE(n, NULL);
1025+
return;
1026+
}
1027+
REPLACE(n, NULL);
1028+
} else {
1029+
vcl = vcl_active;
1030+
objname = av[2];
1031+
}
1032+
1033+
AN(vcl);
1034+
AN(objname);
1035+
1036+
if (vcl->label)
1037+
vcl = vcl->label;
1038+
AN(vcl);
1039+
1040+
i = 0;
1041+
while (av[3 + i] != NULL)
1042+
i++;
1043+
1044+
const char *p[i];
1045+
args.n = i;
1046+
args.p = p;
1047+
1048+
i = 0;
1049+
while (av[3 + i] != NULL) {
1050+
args.p[i] = av[3 + i];
1051+
i++;
1052+
}
1053+
1054+
ctx = VCL_Get_CliCtx(1);
1055+
ctx->vcl = vcl;
1056+
ctx->syntax = ctx->vcl->conf->syntax;
1057+
1058+
i = VPI_Tell(ctx, objname, &args);
1059+
1060+
msg = VCL_Rel_CliCtx(&ctx);
1061+
1062+
VCLI_SetResult(cli, i);
1063+
// could have VCLI_Cat or VCLI_Vsb
1064+
VCLI_Out(cli, "%s", VSB_data(msg));
1065+
VSB_destroy(&msg);
1066+
}
1067+
9981068
/*--------------------------------------------------------------------*/
9991069

10001070
static struct cli_proto vcl_cmds[] = {
@@ -1005,6 +1075,7 @@ static struct cli_proto vcl_cmds[] = {
10051075
{ CLICMD_VCL_USE, "", vcl_cli_use },
10061076
{ CLICMD_VCL_SHOW, "", vcl_cli_show },
10071077
{ CLICMD_VCL_LABEL, "", vcl_cli_label },
1078+
{ CLICMD_TELL, "", vcl_cli_tell},
10081079
{ NULL }
10091080
};
10101081

bin/varnishd/cache/cache_vpi.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,56 @@ VPI_Call_End(VRT_CTX, unsigned n)
303303
AN(vbm);
304304
vbit_clr(vbm, n);
305305
}
306+
307+
/*--------------------------------------------------------------------
308+
* tell interface.
309+
*
310+
* we all agree it does not quite fit the purpose of VPI, but it fits here
311+
* better than anywhere else
312+
*
313+
* XXX: Replace instance info with a tree indexed by name
314+
*/
315+
316+
int
317+
VPI_Tell(VRT_CTX, VCL_STRING objname, VCL_STRANDS msg)
318+
{
319+
struct vcl *vcl;
320+
const struct VCL_conf *conf;
321+
const struct vpi_ii *ii;
322+
vmod_cli_f *cli;
323+
324+
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
325+
AN(objname);
326+
327+
ASSERT_CLI();
328+
AZ(ctx->method);
329+
AN(ctx->msg);
330+
331+
vcl = ctx->vcl;
332+
CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
333+
334+
conf = vcl->conf;
335+
CHECK_OBJ_NOTNULL(conf, VCL_CONF_MAGIC);
336+
337+
ii = conf->instance_info;
338+
AN(ii);
339+
while (ii->p != NULL) {
340+
if (! strcmp(ii->name, objname))
341+
break;
342+
ii++;
343+
}
344+
345+
if (ii->p == NULL) {
346+
VSB_printf(ctx->msg, "No object named %s found\n", objname);
347+
return (300);
348+
}
349+
if (ii->clip == NULL) {
350+
VSB_printf(ctx->msg, "Object %s has no cli method\n", objname);
351+
return (300);
352+
}
353+
354+
cli = (void *)*ii->clip;
355+
AN(cli);
356+
AN(*ii->p);
357+
return(cli(ctx, (void *)*ii->p, msg));
358+
}

include/tbl/cli_cmds.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,16 @@ CLI_CMD(PID,
449449
0, 0
450450
)
451451

452+
CLI_CMD(TELL,
453+
"tell",
454+
"tell [<vcl>.]<object> <msg> ...",
455+
"Tell <msg> to <object> from the given <vcl> or the active vcl",
456+
" It is entirely up to the cli method implementation of the"
457+
" respective vmod to interpret <msg>, implement any actions"
458+
" and generate a response",
459+
2, -1
460+
)
461+
452462
#undef CLI_CMD
453463

454464
/*lint -restore */

include/tbl/symbol_kind.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ VCC_KIND(SUB, sub)
4747
VCC_KIND(VAR, var)
4848
VCC_KIND(VCL, vcl)
4949
VCC_KIND(VMOD, vmod)
50+
VCC_KIND(CLI_METHOD, cli)
5051
#undef VCC_KIND
5152

5253
/*lint -restore */

include/vcc_interface.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ void VPI_acl_log(VRT_CTX, const char *);
8888
struct vpi_ii {
8989
uintptr_t * p;
9090
const char * const name;
91+
uintptr_t * clip;
9192
};
9293

9394
/* Compile time regexp */
@@ -113,3 +114,7 @@ enum vcl_func_fail_e VPI_Call_Check(VRT_CTX, const struct VCL_conf *conf,
113114
unsigned methods, unsigned n);
114115
void VPI_Call_Begin(VRT_CTX, unsigned n);
115116
void VPI_Call_End(VRT_CTX, unsigned n);
117+
118+
// return value should be a VCLI_status_e
119+
typedef VCL_INT vmod_cli_f(VRT_CTX, void *, VCL_STRANDS);
120+
int VPI_Tell(VRT_CTX, VCL_STRING, VCL_STRANDS);

lib/libvcc/vcc_vmod.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
STANZA(FUNC, func, SYM_FUNC) \
3636
STANZA(METHOD, method, SYM_METHOD) \
3737
STANZA(OBJ, obj, SYM_OBJECT) \
38-
STANZA(VMOD, vmod, SYM_NONE)
38+
STANZA(VMOD, vmod, SYM_NONE) \
39+
STANZA(CLI, cli, SYM_CLI_METHOD)
3940

40-
void vcc_VmodSymbols(struct vcc *tl, const struct symbol *sym);
41+
void vcc_VmodSymbols(struct vcc *tl, struct symbol *sym);

lib/libvcc/vcc_vmod_sym.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ alias_sym(struct vcc *tl, const struct symbol *psym, const struct vjsn_val *v)
108108
}
109109

110110
static void
111-
func_sym(struct vcc *tl, vcc_kind_t kind, const struct symbol *psym,
111+
func_sym(struct vcc *tl, vcc_kind_t kind, struct symbol *psym,
112112
const struct vjsn_val *v)
113113
{
114+
const struct vjsn_val *vv;
114115
struct symbol *sym;
115116
struct vsb *buf;
116117

@@ -150,15 +151,22 @@ func_sym(struct vcc *tl, vcc_kind_t kind, const struct symbol *psym,
150151
sym->eval_priv = v;
151152
v = VTAILQ_FIRST(&v->children);
152153
assert(vjsn_is_array(v));
154+
vv = v;
153155
v = VTAILQ_FIRST(&v->children);
154156
assert(vjsn_is_string(v));
155157
sym->type = VCC_Type(v->value);
156158
AN(sym->type);
157159
sym->r_methods = VCL_MET_TASK_ALL;
160+
if (kind == SYM_CLI_METHOD) {
161+
vv = VTAILQ_NEXT(vv, list);
162+
assert(vjsn_is_string(vv));
163+
AZ(psym->extra);
164+
psym->extra = vv->value;
165+
}
158166
}
159167

160168
void
161-
vcc_VmodSymbols(struct vcc *tl, const struct symbol *sym)
169+
vcc_VmodSymbols(struct vcc *tl, struct symbol *sym)
162170
{
163171
const struct vjsn *vj;
164172
const struct vjsn_val *vv, *vv1, *vv2;
@@ -223,6 +231,7 @@ vcc_Act_New(struct vcc *tl, struct token *t, struct symbol *sym)
223231

224232
/* Scratch the generic INSTANCE type */
225233
isym->type = osym->type;
234+
isym->extra = osym->extra;
226235

227236
CAST_OBJ_NOTNULL(vv, osym->eval_priv, VJSN_VAL_MAGIC);
228237
// vv = object name

lib/libvcc/vcc_xref.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,20 @@ vcc_instance_info(struct vcc *tl, const struct symbol *sym)
410410
AN(sym->rname);
411411
Fc(tl, 0, "\t{ .p = (uintptr_t *)&%s, .name = \"", sym->rname);
412412
VCC_SymName(tl->fc, sym);
413-
Fc(tl, 0, "\" },\n");
413+
Fc(tl, 0, "\", .clip = ");
414+
if (sym->extra)
415+
Fc(tl, 0, "(uintptr_t *)&%s", sym->extra);
416+
else
417+
Fc(tl, 0, "NULL");
418+
Fc(tl, 0, "},\n");
414419
}
415420

416421
void
417422
VCC_InstanceInfo(struct vcc *tl)
418423
{
419424
Fc(tl, 0, "\nstatic const struct vpi_ii VGC_instance_info[] = {\n");
420425
VCC_WalkSymbols(tl, vcc_instance_info, SYM_MAIN, SYM_INSTANCE);
421-
Fc(tl, 0, "\t{ .p = NULL, .name = \"\" }\n");
426+
Fc(tl, 0, "\t{ .p = NULL, .name = \"\", .clip = NULL }\n");
422427
Fc(tl, 0, "};\n");
423428
}
424429

lib/libvcc/vmodtool.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ def parse(self):
745745
self.rstlbl = '%s.%s()' % (self.vcc.modname, self.proto.name)
746746
self.vcc.contents.append(self)
747747
self.methods = []
748+
self.cli = None
748749

749750
def rsthead(self, fo, man):
750751
if self.rstlbl:
@@ -778,11 +779,15 @@ def cstuff(self, fo, w):
778779
fo.write(self.fini.cproto(['struct %s **' % sn], w))
779780
for i in self.methods:
780781
fo.write(i.proto.cproto(['VRT_CTX', 'struct %s *' % sn], w))
782+
if self.cli is not None:
783+
fo.write(self.cli.proto.cproto(['VRT_CTX', 'struct %s *' % sn], w))
781784
fo.write("\n")
782785

783786
def cstruct(self, fo, define):
784787
self.fmt_cstruct_proto(fo, self.init, define)
785788
self.fmt_cstruct_proto(fo, self.fini, define)
789+
if self.cli is not None:
790+
self.cli.cstruct(fo, define)
786791
for i in self.methods:
787792
i.cstruct(fo, define)
788793
fo.write("\n")
@@ -804,6 +809,9 @@ def json(self, jl):
804809
ll.append(l2)
805810
self.fini.jsonproto(l2, self.fini.name)
806811

812+
if self.cli is not None:
813+
self.cli.json(ll)
814+
807815
for i in self.methods:
808816
i.json(ll)
809817

@@ -883,6 +891,39 @@ def json(self, jl):
883891
jl.append(["$ALIAS", self.sym_alias, self.sym_name])
884892

885893

894+
#######################################################################
895+
896+
897+
class CliStanza(Stanza):
898+
899+
''' $Cli INT name (STRANDS) '''
900+
901+
def parse(self):
902+
p = self.vcc.contents[-1]
903+
assert isinstance(p, ObjectStanza)
904+
self.pfx = p.proto.name
905+
self.proto = ProtoType(self, prefix=self.pfx)
906+
if p.cli is not None:
907+
err("$Cli %s.%s: Cli method already defined for this class"
908+
% (self.pfx, self.proto.bname), warn=False)
909+
if self.proto.retval.vt != "INT":
910+
err("$Cli %s.%s: Return value needs to be INT"
911+
% (self.pfx, self.proto.bname), warn=False)
912+
if len(self.proto.args) != 1 or self.proto.args[0].vt != 'STRANDS':
913+
err("$Cli %s.%s: Need single argument of type STRANDS"
914+
% (self.pfx, self.proto.bname), warn=False)
915+
# self.proto.obj = "x" + self.pfx
916+
# self.rstlbl = 'x%s()' % self.proto.name
917+
p.cli = self
918+
919+
def cstruct(self, fo, define):
920+
self.fmt_cstruct_proto(fo, self.proto, define)
921+
922+
def json(self, jl):
923+
jl.append(["$CLI", self.proto.name])
924+
self.proto.jsonproto(jl[-1], self.proto.cname())
925+
926+
886927
#######################################################################
887928

888929
DISPATCH = {
@@ -893,6 +934,7 @@ def json(self, jl):
893934
"Function": FunctionStanza,
894935
"Object": ObjectStanza,
895936
"Method": MethodStanza,
937+
"Cli": CliStanza,
896938
"Synopsis": SynopsisStanza,
897939
"Alias": AliasStanza,
898940
}

vmod/tests/debug_c00001.vtc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
varnishtest "Test vmod cli methods / vcl tell"
2+
3+
varnish v1 -vcl+backend {
4+
import debug;
5+
6+
backend proforma none;
7+
8+
sub vcl_init {
9+
new obj0 = debug.obj();
10+
new obj1 = debug.obj("only_argument");
11+
new oo0 = debug.obj_opt();
12+
}
13+
} -start
14+
15+
# vcl2 not found
16+
varnish v1 -clierr "300" "tell vcl2.obj0 a b c"
17+
# No object named objX found
18+
varnish v1 -clierr "300" "tell objX a b c"
19+
# Object oo0 has no cli method
20+
varnish v1 -clierr "300" "tell oo0 a b c"
21+
# Too few parameters
22+
varnish v1 -clierr "104" "tell obj0"
23+
24+
varnish v1 -cliexpect "obj0: a b c" "tell obj0 a b c"
25+
varnish v1 -cliexpect "obj0: a b c" "tell vcl1.obj0 a b c"
26+
varnish v1 -cliexpect "obj1: a b c" "tell obj1 a b c"
27+
28+
varnish v1 -vcl { backend proforma none; }
29+
30+
varnish v1 -cliok "vcl.use vcl2"
31+
varnish v1 -cliok "vcl.state vcl1 cold"
32+
varnish v1 -cliexpect "obj0: a b c" "tell vcl1.obj0 a b c"

vmod/vmod_debug.vcc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ $Method VOID .enum(ENUM { phk, des, kristian, mithrandir, martin })
8181
Testing that enums work as part of object and that the parser isn't
8282
(too) buggy.
8383

84+
$Cli INT cli(STRANDS)
85+
8486
$Method VOID .obj()
8587

8688
Covering the fact that a method can be named like the constructor.

0 commit comments

Comments
 (0)