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