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