trace: add trace event iterator APIs
[qemu.git] / net / filter-rewriter.c
blob89abe72d4e609c3dbbb8798d82b95ed774366ba7
1 /*
2 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
3 * Copyright (c) 2016 FUJITSU LIMITED
4 * Copyright (c) 2016 Intel Corporation
6 * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
8 * This work is licensed under the terms of the GNU GPL, version 2 or
9 * later. See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "trace.h"
14 #include "net/colo.h"
15 #include "net/filter.h"
16 #include "net/net.h"
17 #include "qemu-common.h"
18 #include "qapi/error.h"
19 #include "qapi/qmp/qerror.h"
20 #include "qapi-visit.h"
21 #include "qom/object.h"
22 #include "qemu/main-loop.h"
23 #include "qemu/iov.h"
24 #include "net/checksum.h"
26 #define FILTER_COLO_REWRITER(obj) \
27 OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER)
29 #define TYPE_FILTER_REWRITER "filter-rewriter"
31 typedef struct RewriterState {
32 NetFilterState parent_obj;
33 NetQueue *incoming_queue;
34 /* hashtable to save connection */
35 GHashTable *connection_track_table;
36 } RewriterState;
38 static void filter_rewriter_flush(NetFilterState *nf)
40 RewriterState *s = FILTER_COLO_REWRITER(nf);
42 if (!qemu_net_queue_flush(s->incoming_queue)) {
43 /* Unable to empty the queue, purge remaining packets */
44 qemu_net_queue_purge(s->incoming_queue, nf->netdev);
49 * Return 1 on success, if return 0 means the pkt
50 * is not TCP packet
52 static int is_tcp_packet(Packet *pkt)
54 if (!parse_packet_early(pkt) &&
55 pkt->ip->ip_p == IPPROTO_TCP) {
56 return 1;
57 } else {
58 return 0;
62 /* handle tcp packet from primary guest */
63 static int handle_primary_tcp_pkt(NetFilterState *nf,
64 Connection *conn,
65 Packet *pkt)
67 struct tcphdr *tcp_pkt;
69 tcp_pkt = (struct tcphdr *)pkt->transport_header;
70 if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
71 char *sdebug, *ddebug;
72 sdebug = strdup(inet_ntoa(pkt->ip->ip_src));
73 ddebug = strdup(inet_ntoa(pkt->ip->ip_dst));
74 trace_colo_filter_rewriter_pkt_info(__func__, sdebug, ddebug,
75 ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
76 tcp_pkt->th_flags);
77 trace_colo_filter_rewriter_conn_offset(conn->offset);
78 g_free(sdebug);
79 g_free(ddebug);
82 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
84 * we use this flag update offset func
85 * run once in independent tcp connection
87 conn->syn_flag = 1;
90 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK)) {
91 if (conn->syn_flag) {
93 * offset = secondary_seq - primary seq
94 * ack packet sent by guest from primary node,
95 * so we use th_ack - 1 get primary_seq
97 conn->offset -= (ntohl(tcp_pkt->th_ack) - 1);
98 conn->syn_flag = 0;
100 /* handle packets to the secondary from the primary */
101 tcp_pkt->th_ack = htonl(ntohl(tcp_pkt->th_ack) + conn->offset);
103 net_checksum_calculate((uint8_t *)pkt->data, pkt->size);
106 return 0;
109 /* handle tcp packet from secondary guest */
110 static int handle_secondary_tcp_pkt(NetFilterState *nf,
111 Connection *conn,
112 Packet *pkt)
114 struct tcphdr *tcp_pkt;
116 tcp_pkt = (struct tcphdr *)pkt->transport_header;
118 if (trace_event_get_state(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
119 char *sdebug, *ddebug;
120 sdebug = strdup(inet_ntoa(pkt->ip->ip_src));
121 ddebug = strdup(inet_ntoa(pkt->ip->ip_dst));
122 trace_colo_filter_rewriter_pkt_info(__func__, sdebug, ddebug,
123 ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
124 tcp_pkt->th_flags);
125 trace_colo_filter_rewriter_conn_offset(conn->offset);
126 g_free(sdebug);
127 g_free(ddebug);
130 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))) {
132 * save offset = secondary_seq and then
133 * in handle_primary_tcp_pkt make offset
134 * = secondary_seq - primary_seq
136 conn->offset = ntohl(tcp_pkt->th_seq);
139 if ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK) {
140 /* handle packets to the primary from the secondary*/
141 tcp_pkt->th_seq = htonl(ntohl(tcp_pkt->th_seq) - conn->offset);
143 net_checksum_calculate((uint8_t *)pkt->data, pkt->size);
146 return 0;
149 static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
150 NetClientState *sender,
151 unsigned flags,
152 const struct iovec *iov,
153 int iovcnt,
154 NetPacketSent *sent_cb)
156 RewriterState *s = FILTER_COLO_REWRITER(nf);
157 Connection *conn;
158 ConnectionKey key;
159 Packet *pkt;
160 ssize_t size = iov_size(iov, iovcnt);
161 char *buf = g_malloc0(size);
163 iov_to_buf(iov, iovcnt, 0, buf, size);
164 pkt = packet_new(buf, size);
167 * if we get tcp packet
168 * we will rewrite it to make secondary guest's
169 * connection established successfully
171 if (pkt && is_tcp_packet(pkt)) {
173 fill_connection_key(pkt, &key);
175 if (sender == nf->netdev) {
177 * We need make tcp TX and RX packet
178 * into one connection.
180 reverse_connection_key(&key);
182 conn = connection_get(s->connection_track_table,
183 &key,
184 NULL);
186 if (sender == nf->netdev) {
187 /* NET_FILTER_DIRECTION_TX */
188 if (!handle_primary_tcp_pkt(nf, conn, pkt)) {
189 qemu_net_queue_send(s->incoming_queue, sender, 0,
190 (const uint8_t *)pkt->data, pkt->size, NULL);
191 packet_destroy(pkt, NULL);
192 pkt = NULL;
194 * We block the packet here,after rewrite pkt
195 * and will send it
197 return 1;
199 } else {
200 /* NET_FILTER_DIRECTION_RX */
201 if (!handle_secondary_tcp_pkt(nf, conn, pkt)) {
202 qemu_net_queue_send(s->incoming_queue, sender, 0,
203 (const uint8_t *)pkt->data, pkt->size, NULL);
204 packet_destroy(pkt, NULL);
205 pkt = NULL;
207 * We block the packet here,after rewrite pkt
208 * and will send it
210 return 1;
215 packet_destroy(pkt, NULL);
216 pkt = NULL;
217 return 0;
220 static void colo_rewriter_cleanup(NetFilterState *nf)
222 RewriterState *s = FILTER_COLO_REWRITER(nf);
224 /* flush packets */
225 if (s->incoming_queue) {
226 filter_rewriter_flush(nf);
227 g_free(s->incoming_queue);
231 static void colo_rewriter_setup(NetFilterState *nf, Error **errp)
233 RewriterState *s = FILTER_COLO_REWRITER(nf);
235 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
236 connection_key_equal,
237 g_free,
238 connection_destroy);
239 s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
242 static void colo_rewriter_class_init(ObjectClass *oc, void *data)
244 NetFilterClass *nfc = NETFILTER_CLASS(oc);
246 nfc->setup = colo_rewriter_setup;
247 nfc->cleanup = colo_rewriter_cleanup;
248 nfc->receive_iov = colo_rewriter_receive_iov;
251 static const TypeInfo colo_rewriter_info = {
252 .name = TYPE_FILTER_REWRITER,
253 .parent = TYPE_NETFILTER,
254 .class_init = colo_rewriter_class_init,
255 .instance_size = sizeof(RewriterState),
258 static void register_types(void)
260 type_register_static(&colo_rewriter_info);
263 type_init(register_types);