1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2015 Joyent, Inc.
*/
/*
* Shootdown processing logic.
*
* For more information, see the big theory statement in
* lib/varpd/svp/common/libvarpd_svp.c.
*/
#include <umem.h>
#include <sys/uuid.h>
#include <assert.h>
#include <strings.h>
#include <errno.h>
#include <sys/debug.h>
#include <libvarpd_provider.h>
#include <libvarpd_svp.h>
/*
* When we've determined that there's nothing left for us to do, then we go
* ahead and wait svp_shootdown_base seconds + up to an additional
* svp_shootdown_base seconds before asking again. However, if there is actually
* some work going on, just use the svp_shootdown_cont time.
*/
static int svp_shootdown_base = 5;
static int svp_shootdown_cont = 1;
/*
* These are sizes for our logack and logrm buffers. The sizing of the shootdown
* buffere would give us approximately 18 or so VL3 entries and 32 VL2 entries
* or some combination thereof. While it's a bit of overkill, we just use the
* same sized buffer for the list of uuids that we pass to remove log entries
* that we've acted upon.
*/
static int svp_shootdown_buf = 1024;
static void
svp_shootdown_schedule(svp_sdlog_t *sdl, boolean_t cont)
{
assert(MUTEX_HELD(&sdl->sdl_lock));
if (cont == B_TRUE) {
sdl->sdl_timer.st_value = svp_shootdown_cont;
} else {
sdl->sdl_timer.st_value = svp_shootdown_base +
arc4random_uniform(svp_shootdown_base + 1);
}
svp_timer_add(&sdl->sdl_timer);
}
void
svp_shootdown_lrm_cb(svp_remote_t *srp, svp_status_t status)
{
svp_sdlog_t *sdl = &srp->sr_shoot;
mutex_enter(&sdl->sdl_lock);
sdl->sdl_flags &= ~SVP_SD_RUNNING;
svp_shootdown_schedule(sdl, B_TRUE);
mutex_exit(&sdl->sdl_lock);
if (status != SVP_S_OK) {
(void) bunyan_warn(svp_bunyan, "SVP_R_LOG_RM failed",
BUNYAN_T_STRING, "remote_host", srp->sr_hostname,
BUNYAN_T_INT32, "remote_port", srp->sr_rport,
BUNYAN_T_INT32, "status", status,
BUNYAN_T_END);
}
}
static void
svp_shootdown_ref(svp_sdlog_t *sdl)
{
mutex_enter(&sdl->sdl_lock);
sdl->sdl_ref++;
mutex_exit(&sdl->sdl_lock);
}
static void
svp_shootdown_rele(svp_sdlog_t *sdl)
{
svp_lrm_req_t *svrr = sdl->sdl_logrm;
boolean_t next;
mutex_enter(&sdl->sdl_lock);
VERIFY(sdl->sdl_ref > 0);
sdl->sdl_ref--;
if (sdl->sdl_ref > 0) {
mutex_exit(&sdl->sdl_lock);
return;
}
/*
* At this point we know that we hold the last reference, therefore it's
* safe for us to go ahead and clean up and move on and attempt to
* deliver the reply. We always deliver the reply by going through the
* timer. This can be rather important as the final reference may be
* coming through a failed query and it's not always safe for us to
* callback into the remote routines from this context.
*
* We should only do this if we have a non-zero number of entries to
* take down.
*/
sdl->sdl_flags &= ~SVP_SD_RUNNING;
if (svrr->svrr_count > 0) {
sdl->sdl_flags |= SVP_SD_DORM;
next = B_TRUE;
} else {
next = B_FALSE;
}
svp_shootdown_schedule(sdl, next);
mutex_exit(&sdl->sdl_lock);
}
/*
* This is a callback used to indicate that the VL3 lookup has completed and an
* entry, if any, has been injected. If the command succeeded, eg. we got that
* the status was OK or that it was not found, then we will add it to he list to
* shoot down. Otherwise, there's nothing else for us to really do here.
*/
void
svp_shootdown_vl3_cb(svp_status_t status, svp_log_vl3_t *vl3, svp_sdlog_t *sdl)
{
svp_lrm_req_t *svrr = sdl->sdl_logrm;
mutex_enter(&sdl->sdl_lock);
if (status == SVP_S_OK || status == SVP_S_NOTFOUND) {
bcopy(vl3->svl3_id, &svrr->svrr_ids[svrr->svrr_count * 16],
UUID_LEN);
svrr->svrr_count++;
}
mutex_exit(&sdl->sdl_lock);
svp_shootdown_rele(sdl);
}
static int
svp_shootdown_logr_shoot(void *data, svp_log_type_t type, void *arg)
{
svp_sdlog_t *sdl = arg;
svp_remote_t *srp = sdl->sdl_remote;
svp_lrm_req_t *svrr = sdl->sdl_logrm;
if (type != SVP_LOG_VL2 && type != SVP_LOG_VL3)
libvarpd_panic("encountered unknown type: %d\n", type);
if (type == SVP_LOG_VL2) {
svp_log_vl2_t *svl2 = data;
svp_remote_shootdown_vl2(srp, svl2);
mutex_enter(&sdl->sdl_lock);
bcopy(svl2->svl2_id, &svrr->svrr_ids[svrr->svrr_count * 16],
UUID_LEN);
svrr->svrr_count++;
mutex_exit(&sdl->sdl_lock);
} else {
svp_log_vl3_t *svl3 = data;
/* Take a hold for the duration of this request */
svp_shootdown_ref(sdl);
svp_remote_shootdown_vl3(srp, svl3, sdl);
}
return (0);
}
static int
svp_shootdown_logr_count(void *data, svp_log_type_t type, void *arg)
{
uint_t *u = arg;
*u = *u + 1;
return (0);
}
static int
svp_shootdown_logr_iter(svp_remote_t *srp, void *buf, size_t len,
int (*cb)(void *, svp_log_type_t, void *), void *arg)
{
int ret;
off_t cboff = 0;
uint32_t *typep, type;
svp_log_vl2_t *svl2;
svp_log_vl3_t *svl3;
/* Adjust for initial status word */
assert(len >= sizeof (uint32_t));
len -= sizeof (uint32_t);
cboff += sizeof (uint32_t);
while (len > 0) {
size_t opsz;
if (len < sizeof (uint32_t)) {
(void) bunyan_warn(svp_bunyan,
"failed to get initial shootdown tag",
BUNYAN_T_STRING, "remote_host", srp->sr_hostname,
BUNYAN_T_INT32, "remote_port", srp->sr_rport,
BUNYAN_T_INT32, "response_size", cboff + len,
BUNYAN_T_INT32, "response_offset", cboff,
BUNYAN_T_END);
return (-1);
}
typep = buf + cboff;
type = ntohl(*typep);
if (type == SVP_LOG_VL2) {
opsz = sizeof (svp_log_vl2_t);
if (len < opsz) {
(void) bunyan_warn(svp_bunyan,
"not enough data for svp_log_vl2_t",
BUNYAN_T_STRING, "remote_host",
srp->sr_hostname,
BUNYAN_T_INT32, "remote_port",
srp->sr_rport,
BUNYAN_T_INT32, "response_size",
cboff + len,
BUNYAN_T_INT32, "response_offset", cboff,
BUNYAN_T_END);
return (-1);
}
svl2 = (void *)typep;
if ((ret = cb(svl2, type, arg)) != 0)
return (ret);
} else if (type == SVP_LOG_VL3) {
opsz = sizeof (svp_log_vl3_t);
if (len < opsz) {
(void) bunyan_warn(svp_bunyan,
"not enough data for svp_log_vl3_t",
BUNYAN_T_STRING, "remote_host",
srp->sr_hostname,
BUNYAN_T_INT32, "remote_port",
srp->sr_rport,
BUNYAN_T_INT32, "response_size",
cboff + len,
BUNYAN_T_INT32, "response_offset", cboff,
BUNYAN_T_END);
return (-1);
}
svl3 = (void *)typep;
if ((ret = cb(svl3, type, arg)) != 0)
return (ret);
} else {
(void) bunyan_warn(svp_bunyan,
"unknown log structure type",
BUNYAN_T_STRING, "remote_host",
srp->sr_hostname,
BUNYAN_T_INT32, "remote_port", srp->sr_rport,
BUNYAN_T_INT32, "response_size", cboff + len,
BUNYAN_T_INT32, "response_offset", cboff,
BUNYAN_T_INT32, "structure_type", type,
BUNYAN_T_END);
return (-1);
}
len -= opsz;
cboff += opsz;
}
return (0);
}
void
svp_shootdown_logr_cb(svp_remote_t *srp, svp_status_t status, void *cbdata,
size_t cbsize)
{
uint_t count;
svp_sdlog_t *sdl = &srp->sr_shoot;
if (status != SVP_S_OK) {
(void) bunyan_warn(svp_bunyan,
"log request not OK",
BUNYAN_T_STRING, "remote_host", srp->sr_hostname,
BUNYAN_T_INT32, "remote_port", srp->sr_rport,
BUNYAN_T_INT32, "response_size", cbsize,
BUNYAN_T_INT32, "status", status,
BUNYAN_T_END);
mutex_enter(&sdl->sdl_lock);
sdl->sdl_flags &= ~SVP_SD_RUNNING;
svp_shootdown_schedule(sdl, B_FALSE);
mutex_exit(&sdl->sdl_lock);
return;
}
/*
* First go ahead and count the number of entries. This effectively
* allows us to validate that all the data is valid, if this fails, then
* we fail the request.
*/
count = 0;
if ((svp_shootdown_logr_iter(srp, cbdata, cbsize,
svp_shootdown_logr_count, &count)) != 0) {
mutex_enter(&sdl->sdl_lock);
sdl->sdl_flags &= ~SVP_SD_RUNNING;
svp_shootdown_schedule(sdl, B_FALSE);
mutex_exit(&sdl->sdl_lock);
return;
}
/*
* If we have no entries, then we're also done.
*/
if (count == 0) {
mutex_enter(&sdl->sdl_lock);
sdl->sdl_flags &= ~SVP_SD_RUNNING;
svp_shootdown_schedule(sdl, B_FALSE);
mutex_exit(&sdl->sdl_lock);
return;
}
/*
* We have work to do. Because we may have asynchronous VL3 tasks, we're
* going to first grab a reference before we do the iteration. Then, for
* each asynchronous VL3 request we make, that'll also grab a hold. Once
* we're done with the iteration, we'll drop our hold. If that's the
* last one, it'll move on accordingly.
*/
svp_shootdown_ref(sdl);
bzero(sdl->sdl_logrm, svp_shootdown_buf);
/*
* If this fails, we're going to determine what to do next based on the
* number of entries that were entered into the log removal. At this
* point success or failure don't really look different, all it changes
* is how many entries we have to remove.
*/
(void) svp_shootdown_logr_iter(srp, cbdata, cbsize,
svp_shootdown_logr_shoot, sdl);
/*
* Now that we're done with our work, release the hold. If we don't have
* any vl3 tasks outstanding, this'll trigger the next phase of the log
* removals.
*/
svp_shootdown_rele(sdl);
}
static void
svp_shootdown_timer(void *arg)
{
svp_sdlog_t *sdl = arg;
svp_remote_t *srp = sdl->sdl_remote;
boolean_t init = B_TRUE;
mutex_enter(&sdl->sdl_lock);
/*
* If we've been asked to quiesce, we're done.
*/
if ((sdl->sdl_flags & SVP_SD_QUIESCE) != 0) {
mutex_exit(&sdl->sdl_lock);
return;
}
/*
* We shouldn't be able to have ourselves currently be running and reach
* here. If that's the case, we should immediately panic.
*/
if ((sdl->sdl_flags & SVP_SD_RUNNING) != 0) {
libvarpd_panic("remote %p shootdown timer fired while still "
"running", srp);
}
if ((sdl->sdl_flags & SVP_SD_DORM) != 0) {
sdl->sdl_flags &= ~SVP_SD_DORM;
init = B_FALSE;
}
sdl->sdl_flags |= SVP_SD_RUNNING;
mutex_exit(&sdl->sdl_lock);
if (init == B_FALSE) {
svp_lrm_req_t *svrr = sdl->sdl_logrm;
bzero(&sdl->sdl_query, sizeof (svp_query_t));
svp_remote_lrm_request(sdl->sdl_remote, &sdl->sdl_query, svrr,
sizeof (*svrr) + 16 * svrr->svrr_count);
} else {
bzero(&sdl->sdl_query, sizeof (svp_query_t));
svp_remote_log_request(srp, &sdl->sdl_query, sdl->sdl_logack,
svp_shootdown_buf);
}
}
void
svp_shootdown_fini(svp_remote_t *srp)
{
svp_sdlog_t *sdl = &srp->sr_shoot;
mutex_enter(&sdl->sdl_lock);
sdl->sdl_flags |= SVP_SD_QUIESCE;
mutex_exit(&sdl->sdl_lock);
svp_timer_remove(&sdl->sdl_timer);
mutex_enter(&sdl->sdl_lock);
/*
* Normally svp_timer_remove would be enough. However, the query could
* have been put out again outside of the svp_timer interface. Therefore
* we still need to check for SVP_SD_RUNNING.
*/
while (sdl->sdl_flags & SVP_SD_RUNNING)
(void) cond_wait(&sdl->sdl_cond, &sdl->sdl_lock);
mutex_exit(&sdl->sdl_lock);
umem_free(sdl->sdl_logack, svp_shootdown_buf);
umem_free(sdl->sdl_logrm, svp_shootdown_buf);
sdl->sdl_logack = NULL;
sdl->sdl_logrm = NULL;
(void) cond_destroy(&sdl->sdl_cond);
(void) mutex_destroy(&sdl->sdl_lock);
}
void
svp_shootdown_start(svp_remote_t *srp)
{
svp_sdlog_t *sdl = &srp->sr_shoot;
mutex_enter(&sdl->sdl_lock);
svp_shootdown_schedule(sdl, B_FALSE);
mutex_exit(&sdl->sdl_lock);
}
int
svp_shootdown_init(svp_remote_t *srp)
{
int ret;
svp_sdlog_t *sdl = &srp->sr_shoot;
if ((ret = mutex_init(&sdl->sdl_lock, USYNC_THREAD | LOCK_ERRORCHECK,
NULL)) != 0)
return (ret);
if ((ret = cond_init(&sdl->sdl_cond, USYNC_THREAD, NULL)) != 0) {
(void) mutex_destroy(&sdl->sdl_lock);
return (ret);
}
if ((sdl->sdl_logack = umem_alloc(svp_shootdown_buf, UMEM_DEFAULT)) ==
NULL) {
ret = errno;
(void) cond_destroy(&sdl->sdl_cond);
(void) mutex_destroy(&sdl->sdl_lock);
return (ret);
}
if ((sdl->sdl_logrm = umem_alloc(svp_shootdown_buf, UMEM_DEFAULT)) ==
NULL) {
ret = errno;
umem_free(sdl->sdl_logack, svp_shootdown_buf);
(void) cond_destroy(&sdl->sdl_cond);
(void) mutex_destroy(&sdl->sdl_lock);
return (ret);
}
sdl->sdl_remote = srp;
sdl->sdl_timer.st_oneshot = B_TRUE;
sdl->sdl_timer.st_func = svp_shootdown_timer;
sdl->sdl_timer.st_arg = sdl;
return (0);
}
|