]>
Commit | Line | Data |
---|---|---|
e6eee8ab ZC |
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" | |
30656b09 | 13 | #include "trace.h" |
e6eee8ab ZC |
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" | |
25 | ||
26 | #define FILTER_COLO_REWRITER(obj) \ | |
27 | OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER) | |
28 | ||
29 | #define TYPE_FILTER_REWRITER "filter-rewriter" | |
30 | ||
31 | typedef struct RewriterState { | |
32 | NetFilterState parent_obj; | |
33 | NetQueue *incoming_queue; | |
34 | /* hashtable to save connection */ | |
35 | GHashTable *connection_track_table; | |
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 | ||
afe46124 ZC |
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 | ||
30656b09 ZC |
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(TRACE_COLO_FILTER_REWRITER_DEBUG)) { | |
2061c14c ZC |
71 | trace_colo_filter_rewriter_pkt_info(__func__, |
72 | inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst), | |
30656b09 ZC |
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); | |
30656b09 ZC |
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 | } | |
db0a762e HZ |
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); | |
30656b09 | 99 | |
db0a762e HZ |
100 | net_checksum_calculate((uint8_t *)pkt->data, pkt->size); |
101 | } | |
30656b09 ZC |
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(TRACE_COLO_FILTER_REWRITER_DEBUG)) { | |
2061c14c ZC |
117 | trace_colo_filter_rewriter_pkt_info(__func__, |
118 | inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst), | |
30656b09 ZC |
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); | |
30656b09 ZC |
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) { | |
db0a762e HZ |
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); | |
30656b09 | 138 | |
db0a762e HZ |
139 | net_checksum_calculate((uint8_t *)pkt->data, pkt->size); |
140 | } | |
30656b09 ZC |
141 | } |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
e6eee8ab ZC |
146 | static ssize_t colo_rewriter_receive_iov(NetFilterState *nf, |
147 | NetClientState *sender, | |
148 | unsigned flags, | |
149 | const struct iovec *iov, | |
150 | int iovcnt, | |
151 | NetPacketSent *sent_cb) | |
152 | { | |
afe46124 ZC |
153 | RewriterState *s = FILTER_COLO_REWRITER(nf); |
154 | Connection *conn; | |
155 | ConnectionKey key; | |
156 | Packet *pkt; | |
157 | ssize_t size = iov_size(iov, iovcnt); | |
158 | char *buf = g_malloc0(size); | |
159 | ||
160 | iov_to_buf(iov, iovcnt, 0, buf, size); | |
161 | pkt = packet_new(buf, size); | |
2061c14c | 162 | g_free(buf); |
afe46124 | 163 | |
e6eee8ab ZC |
164 | /* |
165 | * if we get tcp packet | |
166 | * we will rewrite it to make secondary guest's | |
167 | * connection established successfully | |
168 | */ | |
afe46124 ZC |
169 | if (pkt && is_tcp_packet(pkt)) { |
170 | ||
171 | fill_connection_key(pkt, &key); | |
172 | ||
173 | if (sender == nf->netdev) { | |
174 | /* | |
175 | * We need make tcp TX and RX packet | |
176 | * into one connection. | |
177 | */ | |
178 | reverse_connection_key(&key); | |
179 | } | |
180 | conn = connection_get(s->connection_track_table, | |
181 | &key, | |
182 | NULL); | |
183 | ||
184 | if (sender == nf->netdev) { | |
185 | /* NET_FILTER_DIRECTION_TX */ | |
30656b09 ZC |
186 | if (!handle_primary_tcp_pkt(nf, conn, pkt)) { |
187 | qemu_net_queue_send(s->incoming_queue, sender, 0, | |
188 | (const uint8_t *)pkt->data, pkt->size, NULL); | |
189 | packet_destroy(pkt, NULL); | |
190 | pkt = NULL; | |
191 | /* | |
192 | * We block the packet here,after rewrite pkt | |
193 | * and will send it | |
194 | */ | |
195 | return 1; | |
196 | } | |
afe46124 ZC |
197 | } else { |
198 | /* NET_FILTER_DIRECTION_RX */ | |
30656b09 ZC |
199 | if (!handle_secondary_tcp_pkt(nf, conn, pkt)) { |
200 | qemu_net_queue_send(s->incoming_queue, sender, 0, | |
201 | (const uint8_t *)pkt->data, pkt->size, NULL); | |
202 | packet_destroy(pkt, NULL); | |
203 | pkt = NULL; | |
204 | /* | |
205 | * We block the packet here,after rewrite pkt | |
206 | * and will send it | |
207 | */ | |
208 | return 1; | |
209 | } | |
afe46124 ZC |
210 | } |
211 | } | |
212 | ||
213 | packet_destroy(pkt, NULL); | |
214 | pkt = NULL; | |
e6eee8ab ZC |
215 | return 0; |
216 | } | |
217 | ||
218 | static void colo_rewriter_cleanup(NetFilterState *nf) | |
219 | { | |
220 | RewriterState *s = FILTER_COLO_REWRITER(nf); | |
221 | ||
222 | /* flush packets */ | |
223 | if (s->incoming_queue) { | |
224 | filter_rewriter_flush(nf); | |
225 | g_free(s->incoming_queue); | |
226 | } | |
227 | } | |
228 | ||
229 | static void colo_rewriter_setup(NetFilterState *nf, Error **errp) | |
230 | { | |
231 | RewriterState *s = FILTER_COLO_REWRITER(nf); | |
232 | ||
233 | s->connection_track_table = g_hash_table_new_full(connection_key_hash, | |
234 | connection_key_equal, | |
235 | g_free, | |
236 | connection_destroy); | |
237 | s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf); | |
238 | } | |
239 | ||
240 | static void colo_rewriter_class_init(ObjectClass *oc, void *data) | |
241 | { | |
242 | NetFilterClass *nfc = NETFILTER_CLASS(oc); | |
243 | ||
244 | nfc->setup = colo_rewriter_setup; | |
245 | nfc->cleanup = colo_rewriter_cleanup; | |
246 | nfc->receive_iov = colo_rewriter_receive_iov; | |
247 | } | |
248 | ||
249 | static const TypeInfo colo_rewriter_info = { | |
250 | .name = TYPE_FILTER_REWRITER, | |
251 | .parent = TYPE_NETFILTER, | |
252 | .class_init = colo_rewriter_class_init, | |
253 | .instance_size = sizeof(RewriterState), | |
254 | }; | |
255 | ||
256 | static void register_types(void) | |
257 | { | |
258 | type_register_static(&colo_rewriter_info); | |
259 | } | |
260 | ||
261 | type_init(register_types); |