summaryrefslogtreecommitdiff
path: root/lib/stream-replay.c
blob: 21da5220ef5d8ed3e5b59448c4ea95bec89e168e (plain)
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
/*
 * Copyright (c) 2021, Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>
#include <ctype.h>
#include <errno.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "ovs-atomic.h"
#include "ovs-replay.h"
#include "util.h"
#include "stream-provider.h"
#include "stream.h"
#include "openvswitch/poll-loop.h"
#include "openvswitch/vlog.h"

VLOG_DEFINE_THIS_MODULE(stream_replay);

static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);

/* Active replay stream. */

struct stream_replay {
    struct stream stream;
    replay_file_t f;
    int seqno;
};

const struct stream_class replay_stream_class;

/* Creates a new stream named 'name' that will emulate sending and receiving
 * data using replay file and stores a pointer to the stream in '*streamp'.
 *
 * Returns 0 if successful, otherwise a positive errno value. */
static int
new_replay_stream(const char *name, struct stream **streamp)
{
    struct stream_replay *s;
    int seqno = 0, error = 0, open_result;
    replay_file_t f;

    ovs_replay_lock();
    error = ovs_replay_file_open(name, &f, &seqno);
    if (error) {
        VLOG_ERR_RL(&rl, "%s: failed to open stream.", name);
        goto unlock;
    }

    error = ovs_replay_read(f, NULL, 0, &open_result, &seqno, true);
    if (error) {
        VLOG_ERR_RL(&rl, "%s: failed to read 'open' record.", name);
        ovs_replay_file_close(f);
        goto unlock;
    }

    if (open_result) {
        error = -open_result;
        ovs_replay_file_close(f);
        goto unlock;
    }

    s = xmalloc(sizeof *s);
    stream_init(&s->stream, &replay_stream_class, 0, xstrdup(name));
    s->f = f;
    s->seqno = seqno;
    *streamp = &s->stream;
unlock:
    ovs_replay_unlock();
    return error;
}

static struct stream_replay *
stream_replay_cast(struct stream *stream)
{
    stream_assert_class(stream, &replay_stream_class);
    return CONTAINER_OF(stream, struct stream_replay, stream);
}

void
stream_replay_open_wfd(struct stream *s, int open_result, const char *name)
{
    int state = ovs_replay_get_state();
    int error = 0;
    replay_file_t f;

    if (OVS_LIKELY(state != OVS_REPLAY_WRITE)) {
        return;
    }

    ovs_replay_lock();
    error = ovs_replay_file_open(name, &f, NULL);
    if (error) {
        VLOG_ERR_RL(&rl, "%s: failed to open replay file for stream.", name);
        ovs_replay_unlock();
        return;
    }
    ovs_replay_unlock();

    if (ovs_replay_write(f, NULL, -open_result, true)) {
        VLOG_ERR_RL(&rl, "%s: failed to write 'open' failure: %d",
                    s->name, open_result);
    }
    if (open_result) {
        /* We recorded failure to open the stream. */
        ovs_replay_file_close(f);
    } else {
        s->replay_wfd = f;
    }
}

void
stream_replay_write(struct stream *s, const void *buffer, int n, bool is_read)
{
    int state = ovs_replay_get_state();

    if (OVS_LIKELY(state != OVS_REPLAY_WRITE)) {
        return;
    }

    if (ovs_replay_write(s->replay_wfd, buffer, n, is_read)) {
        VLOG_ERR_RL(&rl, "%s: failed to write buffer.", s->name);
    }
}

void
stream_replay_close_wfd(struct stream *s)
{
    if (s->replay_wfd) {
        ovs_replay_file_close(s->replay_wfd);
    }
}

static int
stream_replay_open(const char *name, char *suffix OVS_UNUSED,
                   struct stream **streamp, uint8_t dscp OVS_UNUSED)
{
    return new_replay_stream(name, streamp);
}

static void
stream_replay_close(struct stream *stream)
{
    struct stream_replay *s = stream_replay_cast(stream);
    ovs_replay_file_close(s->f);
    free(s);
}

static ssize_t
stream_replay_recv(struct stream *stream, void *buffer, size_t n)
{
    struct stream_replay *s = stream_replay_cast(stream);
    int norm_seqno = ovs_replay_normalized_seqno(s->seqno);
    int error, len;

    ovs_replay_lock();
    ovs_assert(norm_seqno >= ovs_replay_seqno());

    if (norm_seqno != ovs_replay_seqno()
        || !ovs_replay_seqno_is_read(s->seqno)) {
        error = EAGAIN;
        goto unlock;
    }

    error = ovs_replay_read(s->f, buffer, n, &len, &s->seqno, true);
    if (error) {
        VLOG_ERR_RL(&rl, "%s: failed to read from replay file.", stream->name);
        goto unlock;
    }

unlock:
    ovs_replay_unlock();
    return error ? -error : len;
}

static ssize_t
stream_replay_send(struct stream *stream OVS_UNUSED,
                   const void *buffer OVS_UNUSED, size_t n)
{
    struct stream_replay *s = stream_replay_cast(stream);
    int norm_seqno = ovs_replay_normalized_seqno(s->seqno);
    int error, len;

    ovs_replay_lock();
    ovs_assert(norm_seqno >= ovs_replay_seqno());

    if (norm_seqno != ovs_replay_seqno()
        || ovs_replay_seqno_is_read(s->seqno)) {
        error = EAGAIN;
        goto unlock;
    }

    error = ovs_replay_read(s->f, NULL, 0, &len, &s->seqno, false);
    if (error) {
        VLOG_ERR_RL(&rl, "%s: failed to read from replay file.", stream->name);
        goto unlock;
    }
    ovs_assert(len < 0 || len <= n);

unlock:
    ovs_replay_unlock();
    return error ? -error : len;
}

static void
stream_replay_wait(struct stream *stream, enum stream_wait_type wait)
{
    struct stream_replay *s = stream_replay_cast(stream);
    switch (wait) {
    case STREAM_CONNECT:
        /* Connect does nothing and always available. */
        poll_immediate_wake();
        break;

    case STREAM_SEND:
        if (s->seqno != INT_MAX && !ovs_replay_seqno_is_read(s->seqno)) {
            /* Stream waits for write. */
            poll_immediate_wake();
        }
        break;

    case STREAM_RECV:
        if (s->seqno != INT_MAX && ovs_replay_seqno_is_read(s->seqno)) {
            /* We still have something to read. */
            poll_immediate_wake();
        }
        break;

    default:
        OVS_NOT_REACHED();
    }
}

const struct stream_class replay_stream_class = {
    "replay",                   /* name */
    false,                      /* needs_probes */
    stream_replay_open,         /* open */
    stream_replay_close,        /* close */
    NULL,                       /* connect */
    stream_replay_recv,         /* recv */
    stream_replay_send,         /* send */
    NULL,                       /* run */
    NULL,                       /* run_wait */
    stream_replay_wait,         /* wait */
};

/* Passive replay stream. */

struct replay_pstream
{
    struct pstream pstream;
    replay_file_t f;
    int seqno;
};

const struct pstream_class preplay_pstream_class;

static struct replay_pstream *
replay_pstream_cast(struct pstream *pstream)
{
    pstream_assert_class(pstream, &preplay_pstream_class);
    return CONTAINER_OF(pstream, struct replay_pstream, pstream);
}

/* Creates a new pstream named 'name' that will accept new replay connections
 * reading them from the replay file and stores a pointer to the stream in
 * '*pstreamp'.
 *
 * Returns 0 if successful, otherwise a positive errno value. */
static int
pstream_replay_listen(const char *name, char *suffix OVS_UNUSED,
                      struct pstream **pstreamp, uint8_t dscp OVS_UNUSED)
{
    int seqno = 0, error = 0, listen_result;
    replay_file_t f;

    ovs_replay_lock();
    error = ovs_replay_file_open(name, &f, &seqno);
    if (error) {
        VLOG_ERR_RL(&rl, "%s: failed to open pstream.", name);
        goto unlock;
    }

    error = ovs_replay_read(f, NULL, 0, &listen_result, &seqno, true);
    if (error) {
        VLOG_ERR_RL(&rl, "%s: failed to read 'listen' record.", name);
        ovs_replay_file_close(f);
        goto unlock;
    }

    if (listen_result) {
        error = -listen_result;
        ovs_replay_file_close(f);
        goto unlock;
    }

    struct replay_pstream *ps = xmalloc(sizeof *ps);
    pstream_init(&ps->pstream, &preplay_pstream_class, xstrdup(name));
    ps->f = f;
    ps->seqno = seqno;
    *pstreamp = &ps->pstream;
unlock:
    ovs_replay_unlock();
    return error;
}

void
pstream_replay_open_wfd(struct pstream *ps, int listen_result,
                        const char *name)
{
    int state = ovs_replay_get_state();
    int error = 0;
    replay_file_t f;

    if (OVS_LIKELY(state != OVS_REPLAY_WRITE)) {
        return;
    }

    ovs_replay_lock();
    error = ovs_replay_file_open(name, &f, NULL);
    if (error) {
        VLOG_ERR_RL(&rl, "%s: failed to open replay file for pstream.", name);
        ovs_replay_unlock();
        return;
    }
    ovs_replay_unlock();

    if (ovs_replay_write(f, NULL, -listen_result, true)) {
        VLOG_ERR_RL(&rl, "%s: failed to write 'listen' result: %d",
                    ps->name, listen_result);
    }

    if (listen_result) {
        /* We recorded failure to open the stream. */
        ovs_replay_file_close(f);
    } else {
        ps->replay_wfd = f;
    }
}

void
pstream_replay_write_accept(struct pstream *ps, const struct stream *s,
                            int accept_result)
{
    int state = ovs_replay_get_state();
    int len;

    if (OVS_LIKELY(state != OVS_REPLAY_WRITE)) {
        return;
    }

    if (!accept_result) {
        len = strlen(s->name);
        if (ovs_replay_write(ps->replay_wfd, s->name, len, true)) {
            VLOG_ERR_RL(&rl, "%s: failed to write accept name: %s",
                        ps->name, s->name);
        }
    } else if (ovs_replay_write(ps->replay_wfd, NULL, -accept_result, true)) {
        VLOG_ERR_RL(&rl, "%s: failed to write 'accept' failure: %d",
                    ps->name, accept_result);
    }
}

void
pstream_replay_close_wfd(struct pstream *ps)
{
    if (ps->replay_wfd) {
        ovs_replay_file_close(ps->replay_wfd);
    }
}

static void
pstream_replay_close(struct pstream *pstream)
{
    struct replay_pstream *ps = replay_pstream_cast(pstream);

    ovs_replay_file_close(ps->f);
    free(ps);
}

#define MAX_NAME_LEN 65536

static int
pstream_replay_accept(struct pstream *pstream, struct stream **new_streamp)
{
    struct replay_pstream *ps = replay_pstream_cast(pstream);
    int norm_seqno = ovs_replay_normalized_seqno(ps->seqno);
    int retval, len;
    char name[MAX_NAME_LEN];

    ovs_replay_lock();
    ovs_assert(norm_seqno >= ovs_replay_seqno());

    if (norm_seqno != ovs_replay_seqno()
        || !ovs_replay_seqno_is_read(ps->seqno)) {
        retval = EAGAIN;
        ovs_replay_unlock();
        goto exit;
    }

    retval = ovs_replay_read(ps->f, name, MAX_NAME_LEN - 1,
                             &len, &ps->seqno, true);
    if (retval) {
        VLOG_ERR_RL(&rl, "%s: failed to read from replay file.",
                    pstream->name);
        ovs_replay_unlock();
        goto exit;
    }

    ovs_replay_unlock();

    if (len > 0) {
        name[len] = 0;
        retval = new_replay_stream(name, new_streamp);
    } else {
        retval = -len;
    }
exit:
    return retval;
}

static void
pstream_replay_wait(struct pstream *pstream)
{
    struct replay_pstream *ps = replay_pstream_cast(pstream);

    if (ps->seqno != INT_MAX) {
        /* Replay always has something to say. */
        poll_immediate_wake();
    }
}

const struct pstream_class preplay_pstream_class = {
    "preplay",
    false,
    pstream_replay_listen,
    pstream_replay_close,
    pstream_replay_accept,
    pstream_replay_wait,
};