xref: /openbmc/qemu/net/filter-replay.c (revision c734cd40)
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 typedef struct NetFilterReplayState NetFilterReplayState;
27 DECLARE_INSTANCE_CHECKER(NetFilterReplayState, FILTER_REPLAY,
28                          TYPE_FILTER_REPLAY)
29 
30 struct NetFilterReplayState {
31     NetFilterState nfs;
32     ReplayNetState *rns;
33 };
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