]> git.proxmox.com Git - mirror_qemu.git/blob - net/filter-rewriter.c
Merge remote-tracking branch 'remotes/jasowang/tags/net-pull-request' into staging
[mirror_qemu.git] / net / filter-rewriter.c
1 /*
2 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
3 * Copyright (c) 2016 FUJITSU LIMITED
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
7 *
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.
10 */
11
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 "qemu/error-report.h"
19 #include "qom/object.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/iov.h"
22 #include "net/checksum.h"
23
24 #define FILTER_COLO_REWRITER(obj) \
25 OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER)
26
27 #define TYPE_FILTER_REWRITER "filter-rewriter"
28
29 typedef struct RewriterState {
30 NetFilterState parent_obj;
31 NetQueue *incoming_queue;
32 /* hashtable to save connection */
33 GHashTable *connection_track_table;
34 bool vnet_hdr;
35 } RewriterState;
36
37 static void filter_rewriter_flush(NetFilterState *nf)
38 {
39 RewriterState *s = FILTER_COLO_REWRITER(nf);
40
41 if (!qemu_net_queue_flush(s->incoming_queue)) {
42 /* Unable to empty the queue, purge remaining packets */
43 qemu_net_queue_purge(s->incoming_queue, nf->netdev);
44 }
45 }
46
47 /*
48 * Return 1 on success, if return 0 means the pkt
49 * is not TCP packet
50 */
51 static int is_tcp_packet(Packet *pkt)
52 {
53 if (!parse_packet_early(pkt) &&
54 pkt->ip->ip_p == IPPROTO_TCP) {
55 return 1;
56 } else {
57 return 0;
58 }
59 }
60
61 /* handle tcp packet from primary guest */
62 static int handle_primary_tcp_pkt(NetFilterState *nf,
63 Connection *conn,
64 Packet *pkt)
65 {
66 struct tcphdr *tcp_pkt;
67
68 tcp_pkt = (struct tcphdr *)pkt->transport_header;
69 if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
70 trace_colo_filter_rewriter_pkt_info(__func__,
71 inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
72 ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
73 tcp_pkt->th_flags);
74 trace_colo_filter_rewriter_conn_offset(conn->offset);
75 }
76
77 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
78 /*
79 * we use this flag update offset func
80 * run once in independent tcp connection
81 */
82 conn->syn_flag = 1;
83 }
84
85 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK)) {
86 if (conn->syn_flag) {
87 /*
88 * offset = secondary_seq - primary seq
89 * ack packet sent by guest from primary node,
90 * so we use th_ack - 1 get primary_seq
91 */
92 conn->offset -= (ntohl(tcp_pkt->th_ack) - 1);
93 conn->syn_flag = 0;
94 }
95 if (conn->offset) {
96 /* handle packets to the secondary from the primary */
97 tcp_pkt->th_ack = htonl(ntohl(tcp_pkt->th_ack) + conn->offset);
98
99 net_checksum_calculate((uint8_t *)pkt->data + pkt->vnet_hdr_len,
100 pkt->size - pkt->vnet_hdr_len);
101 }
102 }
103
104 return 0;
105 }
106
107 /* handle tcp packet from secondary guest */
108 static int handle_secondary_tcp_pkt(NetFilterState *nf,
109 Connection *conn,
110 Packet *pkt)
111 {
112 struct tcphdr *tcp_pkt;
113
114 tcp_pkt = (struct tcphdr *)pkt->transport_header;
115
116 if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
117 trace_colo_filter_rewriter_pkt_info(__func__,
118 inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
119 ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
120 tcp_pkt->th_flags);
121 trace_colo_filter_rewriter_conn_offset(conn->offset);
122 }
123
124 if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))) {
125 /*
126 * save offset = secondary_seq and then
127 * in handle_primary_tcp_pkt make offset
128 * = secondary_seq - primary_seq
129 */
130 conn->offset = ntohl(tcp_pkt->th_seq);
131 }
132
133 if ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK) {
134 /* Only need to adjust seq while offset is Non-zero */
135 if (conn->offset) {
136 /* handle packets to the primary from the secondary*/
137 tcp_pkt->th_seq = htonl(ntohl(tcp_pkt->th_seq) - conn->offset);
138
139 net_checksum_calculate((uint8_t *)pkt->data + pkt->vnet_hdr_len,
140 pkt->size - pkt->vnet_hdr_len);
141 }
142 }
143
144 return 0;
145 }
146
147 static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
148 NetClientState *sender,
149 unsigned flags,
150 const struct iovec *iov,
151 int iovcnt,
152 NetPacketSent *sent_cb)
153 {
154 RewriterState *s = FILTER_COLO_REWRITER(nf);
155 Connection *conn;
156 ConnectionKey key;
157 Packet *pkt;
158 ssize_t size = iov_size(iov, iovcnt);
159 ssize_t vnet_hdr_len = 0;
160 char *buf = g_malloc0(size);
161
162 iov_to_buf(iov, iovcnt, 0, buf, size);
163
164 if (s->vnet_hdr) {
165 vnet_hdr_len = nf->netdev->vnet_hdr_len;
166 }
167
168 pkt = packet_new(buf, size, vnet_hdr_len);
169 g_free(buf);
170
171 /*
172 * if we get tcp packet
173 * we will rewrite it to make secondary guest's
174 * connection established successfully
175 */
176 if (pkt && is_tcp_packet(pkt)) {
177
178 fill_connection_key(pkt, &key);
179
180 if (sender == nf->netdev) {
181 /*
182 * We need make tcp TX and RX packet
183 * into one connection.
184 */
185 reverse_connection_key(&key);
186 }
187 conn = connection_get(s->connection_track_table,
188 &key,
189 NULL);
190
191 if (sender == nf->netdev) {
192 /* NET_FILTER_DIRECTION_TX */
193 if (!handle_primary_tcp_pkt(nf, conn, pkt)) {
194 qemu_net_queue_send(s->incoming_queue, sender, 0,
195 (const uint8_t *)pkt->data, pkt->size, NULL);
196 packet_destroy(pkt, NULL);
197 pkt = NULL;
198 /*
199 * We block the packet here,after rewrite pkt
200 * and will send it
201 */
202 return 1;
203 }
204 } else {
205 /* NET_FILTER_DIRECTION_RX */
206 if (!handle_secondary_tcp_pkt(nf, conn, pkt)) {
207 qemu_net_queue_send(s->incoming_queue, sender, 0,
208 (const uint8_t *)pkt->data, pkt->size, NULL);
209 packet_destroy(pkt, NULL);
210 pkt = NULL;
211 /*
212 * We block the packet here,after rewrite pkt
213 * and will send it
214 */
215 return 1;
216 }
217 }
218 }
219
220 packet_destroy(pkt, NULL);
221 pkt = NULL;
222 return 0;
223 }
224
225 static void colo_rewriter_cleanup(NetFilterState *nf)
226 {
227 RewriterState *s = FILTER_COLO_REWRITER(nf);
228
229 /* flush packets */
230 if (s->incoming_queue) {
231 filter_rewriter_flush(nf);
232 g_free(s->incoming_queue);
233 }
234 }
235
236 static void colo_rewriter_setup(NetFilterState *nf, Error **errp)
237 {
238 RewriterState *s = FILTER_COLO_REWRITER(nf);
239
240 s->connection_track_table = g_hash_table_new_full(connection_key_hash,
241 connection_key_equal,
242 g_free,
243 connection_destroy);
244 s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
245 }
246
247 static bool filter_rewriter_get_vnet_hdr(Object *obj, Error **errp)
248 {
249 RewriterState *s = FILTER_COLO_REWRITER(obj);
250
251 return s->vnet_hdr;
252 }
253
254 static void filter_rewriter_set_vnet_hdr(Object *obj,
255 bool value,
256 Error **errp)
257 {
258 RewriterState *s = FILTER_COLO_REWRITER(obj);
259
260 s->vnet_hdr = value;
261 }
262
263 static void filter_rewriter_init(Object *obj)
264 {
265 RewriterState *s = FILTER_COLO_REWRITER(obj);
266
267 s->vnet_hdr = false;
268 object_property_add_bool(obj, "vnet_hdr_support",
269 filter_rewriter_get_vnet_hdr,
270 filter_rewriter_set_vnet_hdr, NULL);
271 }
272
273 static void colo_rewriter_class_init(ObjectClass *oc, void *data)
274 {
275 NetFilterClass *nfc = NETFILTER_CLASS(oc);
276
277 nfc->setup = colo_rewriter_setup;
278 nfc->cleanup = colo_rewriter_cleanup;
279 nfc->receive_iov = colo_rewriter_receive_iov;
280 }
281
282 static const TypeInfo colo_rewriter_info = {
283 .name = TYPE_FILTER_REWRITER,
284 .parent = TYPE_NETFILTER,
285 .class_init = colo_rewriter_class_init,
286 .instance_init = filter_rewriter_init,
287 .instance_size = sizeof(RewriterState),
288 };
289
290 static void register_types(void)
291 {
292 type_register_static(&colo_rewriter_info);
293 }
294
295 type_init(register_types);