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