xref: /openbmc/qemu/net/filter-replay.c (revision 53ba2eee)
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 "qemu/error-report.h"
15 #include "qemu/iov.h"
16 #include "qemu/log.h"
17 #include "qemu/module.h"
18 #include "qemu/timer.h"
19 #include "qapi/visitor.h"
20 #include "net/filter.h"
21 #include "sysemu/replay.h"
22 #include "qom/object.h"
23 
24 #define TYPE_FILTER_REPLAY "filter-replay"
25 
26 OBJECT_DECLARE_SIMPLE_TYPE(NetFilterReplayState, FILTER_REPLAY)
27 
28 struct NetFilterReplayState {
29     NetFilterState nfs;
30     ReplayNetState *rns;
31 };
32 
33 static ssize_t filter_replay_receive_iov(NetFilterState *nf,
34                                          NetClientState *sndr,
35                                          unsigned flags,
36                                          const struct iovec *iov,
37                                          int iovcnt, NetPacketSent *sent_cb)
38 {
39     NetFilterReplayState *nfrs = FILTER_REPLAY(nf);
40     switch (replay_mode) {
41     case REPLAY_MODE_RECORD:
42         if (nf->netdev == sndr) {
43             replay_net_packet_event(nfrs->rns, flags, iov, iovcnt);
44             return iov_size(iov, iovcnt);
45         }
46         return 0;
47     case REPLAY_MODE_PLAY:
48         /* Drop all packets in replay mode.
49            Packets from the log will be injected by the replay module. */
50         return iov_size(iov, iovcnt);
51     default:
52         /* Pass all the packets. */
53         return 0;
54     }
55 }
56 
57 static void filter_replay_instance_init(Object *obj)
58 {
59     NetFilterReplayState *nfrs = FILTER_REPLAY(obj);
60     nfrs->rns = replay_register_net(&nfrs->nfs);
61 }
62 
63 static void filter_replay_instance_finalize(Object *obj)
64 {
65     NetFilterReplayState *nfrs = FILTER_REPLAY(obj);
66     replay_unregister_net(nfrs->rns);
67 }
68 
69 static void filter_replay_class_init(ObjectClass *oc, void *data)
70 {
71     NetFilterClass *nfc = NETFILTER_CLASS(oc);
72 
73     nfc->receive_iov = filter_replay_receive_iov;
74 }
75 
76 static const TypeInfo filter_replay_info = {
77     .name = TYPE_FILTER_REPLAY,
78     .parent = TYPE_NETFILTER,
79     .class_init = filter_replay_class_init,
80     .instance_init = filter_replay_instance_init,
81     .instance_finalize = filter_replay_instance_finalize,
82     .instance_size = sizeof(NetFilterReplayState),
83 };
84 
85 static void filter_replay_register_types(void)
86 {
87     type_register_static(&filter_replay_info);
88 }
89 
90 type_init(filter_replay_register_types);
91