xref: /openbmc/qemu/net/filter-replay.c (revision a9ded601)
1 /*
2  * filter-replay.c
3  *
4  * Copyright (c) 2010-2016 Institute for System Programming
5  *                         of the Russian Academy of Sciences.
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2 or later.
8  * See the COPYING file in the top-level directory.
9  *
10  */
11 
12 #include "qemu/osdep.h"
13 #include "clients.h"
14 #include "qapi/error.h"
15 #include "qemu-common.h"
16 #include "qemu/error-report.h"
17 #include "qemu/iov.h"
18 #include "qemu/log.h"
19 #include "qemu/timer.h"
20 #include "qapi/visitor.h"
21 #include "net/filter.h"
22 #include "sysemu/replay.h"
23 
24 #define TYPE_FILTER_REPLAY "filter-replay"
25 
26 #define FILTER_REPLAY(obj) \
27     OBJECT_CHECK(NetFilterReplayState, (obj), TYPE_FILTER_REPLAY)
28 
29 struct NetFilterReplayState {
30     NetFilterState nfs;
31     ReplayNetState *rns;
32 };
33 typedef struct NetFilterReplayState NetFilterReplayState;
34 
35 static ssize_t filter_replay_receive_iov(NetFilterState *nf,
36                                          NetClientState *sndr,
37                                          unsigned flags,
38                                          const struct iovec *iov,
39                                          int iovcnt, NetPacketSent *sent_cb)
40 {
41     NetFilterReplayState *nfrs = FILTER_REPLAY(nf);
42     switch (replay_mode) {
43     case REPLAY_MODE_RECORD:
44         if (nf->netdev == sndr) {
45             replay_net_packet_event(nfrs->rns, flags, iov, iovcnt);
46             return iov_size(iov, iovcnt);
47         }
48         return 0;
49     case REPLAY_MODE_PLAY:
50         /* Drop all packets in replay mode.
51            Packets from the log will be injected by the replay module. */
52         return iov_size(iov, iovcnt);
53     default:
54         /* Pass all the packets. */
55         return 0;
56     }
57 }
58 
59 static void filter_replay_instance_init(Object *obj)
60 {
61     NetFilterReplayState *nfrs = FILTER_REPLAY(obj);
62     nfrs->rns = replay_register_net(&nfrs->nfs);
63 }
64 
65 static void filter_replay_instance_finalize(Object *obj)
66 {
67     NetFilterReplayState *nfrs = FILTER_REPLAY(obj);
68     replay_unregister_net(nfrs->rns);
69 }
70 
71 static void filter_replay_class_init(ObjectClass *oc, void *data)
72 {
73     NetFilterClass *nfc = NETFILTER_CLASS(oc);
74 
75     nfc->receive_iov = filter_replay_receive_iov;
76 }
77 
78 static const TypeInfo filter_replay_info = {
79     .name = TYPE_FILTER_REPLAY,
80     .parent = TYPE_NETFILTER,
81     .class_init = filter_replay_class_init,
82     .instance_init = filter_replay_instance_init,
83     .instance_finalize = filter_replay_instance_finalize,
84     .instance_size = sizeof(NetFilterReplayState),
85 };
86 
87 static void filter_replay_register_types(void)
88 {
89     type_register_static(&filter_replay_info);
90 }
91 
92 type_init(filter_replay_register_types);
93