2 * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO)
3 * (a.k.a. Fault Tolerance or Continuous Replication)
5 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
6 * Copyright (c) 2016 FUJITSU LIMITED
7 * Copyright (c) 2016 Intel Corporation
9 * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or
12 * later. See the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
20 uint32_t connection_key_hash(const void *opaque
)
22 const ConnectionKey
*key
= opaque
;
26 a
= b
= c
= JHASH_INITVAL
+ sizeof(*key
);
29 c
+= (key
->src_port
| key
->dst_port
<< 16);
33 __jhash_final(a
, b
, c
);
38 int connection_key_equal(const void *key1
, const void *key2
)
40 return memcmp(key1
, key2
, sizeof(ConnectionKey
)) == 0;
43 int parse_packet_early(Packet
*pkt
)
46 static const uint8_t vlan
[] = {0x81, 0x00};
47 uint8_t *data
= pkt
->data
;
53 /* Check the received vnet_hdr_len then add the offset */
54 if ((pkt
->vnet_hdr_len
> sizeof(struct virtio_net_hdr_v1_hash
)) ||
55 (pkt
->size
< sizeof(struct eth_header
) + sizeof(struct vlan_header
) +
58 * The received remote packet maybe misconfiguration here,
59 * Please enable/disable filter module's the vnet_hdr flag at
62 trace_colo_proxy_main_vnet_info("This received packet load wrong ",
63 pkt
->vnet_hdr_len
, pkt
->size
);
66 data
+= pkt
->vnet_hdr_len
;
68 l2hdr_len
= eth_get_l2_hdr_length(data
);
73 if (!memcmp(&data
[12], vlan
, sizeof(vlan
))) {
74 trace_colo_proxy_main("COLO-proxy don't support vlan");
78 pkt
->network_header
= data
+ l2hdr_len
;
80 const struct iovec l2vec
= {
81 .iov_base
= (void *) data
,
84 l3_proto
= eth_get_l3_proto(&l2vec
, 1, l2hdr_len
);
86 if (l3_proto
!= ETH_P_IP
) {
90 network_length
= pkt
->ip
->ip_hl
* 4;
91 if (pkt
->size
< l2hdr_len
+ network_length
+ pkt
->vnet_hdr_len
) {
92 trace_colo_proxy_main("pkt->size < network_header + network_length");
95 pkt
->transport_header
= pkt
->network_header
+ network_length
;
100 void extract_ip_and_port(uint32_t tmp_ports
, ConnectionKey
*key
,
101 Packet
*pkt
, bool reverse
)
104 key
->src
= pkt
->ip
->ip_dst
;
105 key
->dst
= pkt
->ip
->ip_src
;
106 key
->src_port
= ntohs(tmp_ports
& 0xffff);
107 key
->dst_port
= ntohs(tmp_ports
>> 16);
109 key
->src
= pkt
->ip
->ip_src
;
110 key
->dst
= pkt
->ip
->ip_dst
;
111 key
->src_port
= ntohs(tmp_ports
>> 16);
112 key
->dst_port
= ntohs(tmp_ports
& 0xffff);
116 void fill_connection_key(Packet
*pkt
, ConnectionKey
*key
, bool reverse
)
118 uint32_t tmp_ports
= 0;
120 key
->ip_proto
= pkt
->ip
->ip_p
;
122 switch (key
->ip_proto
) {
128 case IPPROTO_UDPLITE
:
129 tmp_ports
= *(uint32_t *)(pkt
->transport_header
);
132 tmp_ports
= *(uint32_t *)(pkt
->transport_header
+ 4);
138 extract_ip_and_port(tmp_ports
, key
, pkt
, reverse
);
141 Connection
*connection_new(ConnectionKey
*key
)
143 Connection
*conn
= g_slice_new0(Connection
);
145 conn
->ip_proto
= key
->ip_proto
;
146 conn
->processing
= false;
147 conn
->tcp_state
= TCPS_CLOSED
;
148 g_queue_init(&conn
->primary_list
);
149 g_queue_init(&conn
->secondary_list
);
154 void connection_destroy(void *opaque
)
156 Connection
*conn
= opaque
;
158 g_queue_foreach(&conn
->primary_list
, packet_destroy
, NULL
);
159 g_queue_clear(&conn
->primary_list
);
160 g_queue_foreach(&conn
->secondary_list
, packet_destroy
, NULL
);
161 g_queue_clear(&conn
->secondary_list
);
162 g_slice_free(Connection
, conn
);
165 Packet
*packet_new(const void *data
, int size
, int vnet_hdr_len
)
167 Packet
*pkt
= g_slice_new0(Packet
);
169 pkt
->data
= g_memdup(data
, size
);
171 pkt
->creation_ms
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
172 pkt
->vnet_hdr_len
= vnet_hdr_len
;
178 * packet_new_nocopy will not copy data, so the caller can't release
179 * the data. And it will be released in packet_destroy.
181 Packet
*packet_new_nocopy(void *data
, int size
, int vnet_hdr_len
)
183 Packet
*pkt
= g_slice_new0(Packet
);
187 pkt
->creation_ms
= qemu_clock_get_ms(QEMU_CLOCK_HOST
);
188 pkt
->vnet_hdr_len
= vnet_hdr_len
;
193 void packet_destroy(void *opaque
, void *user_data
)
195 Packet
*pkt
= opaque
;
198 g_slice_free(Packet
, pkt
);
201 void packet_destroy_partial(void *opaque
, void *user_data
)
203 Packet
*pkt
= opaque
;
205 g_slice_free(Packet
, pkt
);
209 * Clear hashtable, stop this hash growing really huge
211 void connection_hashtable_reset(GHashTable
*connection_track_table
)
213 g_hash_table_remove_all(connection_track_table
);
216 /* if not found, create a new connection and add to hash table */
217 Connection
*connection_get(GHashTable
*connection_track_table
,
221 Connection
*conn
= g_hash_table_lookup(connection_track_table
, key
);
224 ConnectionKey
*new_key
= g_memdup(key
, sizeof(*key
));
226 conn
= connection_new(key
);
228 if (g_hash_table_size(connection_track_table
) > HASHTABLE_MAX_SIZE
) {
229 trace_colo_proxy_main("colo proxy connection hashtable full,"
231 connection_hashtable_reset(connection_track_table
);
233 * clear the conn_list
235 while (conn_list
&& !g_queue_is_empty(conn_list
)) {
236 connection_destroy(g_queue_pop_head(conn_list
));
240 g_hash_table_insert(connection_track_table
, new_key
, conn
);
246 bool connection_has_tracked(GHashTable
*connection_track_table
,
249 Connection
*conn
= g_hash_table_lookup(connection_track_table
, key
);
251 return conn
? true : false;