target/mips: optimize cross-page direct jumps in softmmu
[qemu/ar7.git] / net / colo.c
blob8cc166bc22258368fa2986fdec825f2c9b740d71
1 /*
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"
16 #include "trace.h"
17 #include "net/colo.h"
19 uint32_t connection_key_hash(const void *opaque)
21 const ConnectionKey *key = opaque;
22 uint32_t a, b, c;
24 /* Jenkins hash */
25 a = b = c = JHASH_INITVAL + sizeof(*key);
26 a += key->src.s_addr;
27 b += key->dst.s_addr;
28 c += (key->src_port | key->dst_port << 16);
29 __jhash_mix(a, b, c);
31 a += key->ip_proto;
32 __jhash_final(a, b, c);
34 return c;
37 int connection_key_equal(const void *key1, const void *key2)
39 return memcmp(key1, key2, sizeof(ConnectionKey)) == 0;
42 int parse_packet_early(Packet *pkt)
44 int network_length;
45 static const uint8_t vlan[] = {0x81, 0x00};
46 uint8_t *data = pkt->data;
47 uint16_t l3_proto;
48 ssize_t l2hdr_len = eth_get_l2_hdr_length(data);
50 if (pkt->size < ETH_HLEN) {
51 trace_colo_proxy_main("pkt->size < ETH_HLEN");
52 return 1;
56 * TODO: support vlan.
58 if (!memcmp(&data[12], vlan, sizeof(vlan))) {
59 trace_colo_proxy_main("COLO-proxy don't support vlan");
60 return 1;
63 pkt->network_header = data + l2hdr_len;
65 const struct iovec l2vec = {
66 .iov_base = (void *) data,
67 .iov_len = l2hdr_len
69 l3_proto = eth_get_l3_proto(&l2vec, 1, l2hdr_len);
71 if (l3_proto != ETH_P_IP) {
72 return 1;
75 network_length = pkt->ip->ip_hl * 4;
76 if (pkt->size < l2hdr_len + network_length) {
77 trace_colo_proxy_main("pkt->size < network_header + network_length");
78 return 1;
80 pkt->transport_header = pkt->network_header + network_length;
82 return 0;
85 void fill_connection_key(Packet *pkt, ConnectionKey *key)
87 uint32_t tmp_ports;
89 memset(key, 0, sizeof(*key));
90 key->ip_proto = pkt->ip->ip_p;
92 switch (key->ip_proto) {
93 case IPPROTO_TCP:
94 case IPPROTO_UDP:
95 case IPPROTO_DCCP:
96 case IPPROTO_ESP:
97 case IPPROTO_SCTP:
98 case IPPROTO_UDPLITE:
99 tmp_ports = *(uint32_t *)(pkt->transport_header);
100 key->src = pkt->ip->ip_src;
101 key->dst = pkt->ip->ip_dst;
102 key->src_port = ntohs(tmp_ports & 0xffff);
103 key->dst_port = ntohs(tmp_ports >> 16);
104 break;
105 case IPPROTO_AH:
106 tmp_ports = *(uint32_t *)(pkt->transport_header + 4);
107 key->src = pkt->ip->ip_src;
108 key->dst = pkt->ip->ip_dst;
109 key->src_port = ntohs(tmp_ports & 0xffff);
110 key->dst_port = ntohs(tmp_ports >> 16);
111 break;
112 default:
113 break;
117 void reverse_connection_key(ConnectionKey *key)
119 struct in_addr tmp_ip;
120 uint16_t tmp_port;
122 tmp_ip = key->src;
123 key->src = key->dst;
124 key->dst = tmp_ip;
126 tmp_port = key->src_port;
127 key->src_port = key->dst_port;
128 key->dst_port = tmp_port;
131 Connection *connection_new(ConnectionKey *key)
133 Connection *conn = g_slice_new(Connection);
135 conn->ip_proto = key->ip_proto;
136 conn->processing = false;
137 conn->offset = 0;
138 conn->syn_flag = 0;
139 g_queue_init(&conn->primary_list);
140 g_queue_init(&conn->secondary_list);
142 return conn;
145 void connection_destroy(void *opaque)
147 Connection *conn = opaque;
149 g_queue_foreach(&conn->primary_list, packet_destroy, NULL);
150 g_queue_clear(&conn->primary_list);
151 g_queue_foreach(&conn->secondary_list, packet_destroy, NULL);
152 g_queue_clear(&conn->secondary_list);
153 g_slice_free(Connection, conn);
156 Packet *packet_new(const void *data, int size)
158 Packet *pkt = g_slice_new(Packet);
160 pkt->data = g_memdup(data, size);
161 pkt->size = size;
162 pkt->creation_ms = qemu_clock_get_ms(QEMU_CLOCK_HOST);
164 return pkt;
167 void packet_destroy(void *opaque, void *user_data)
169 Packet *pkt = opaque;
171 g_free(pkt->data);
172 g_slice_free(Packet, pkt);
176 * Clear hashtable, stop this hash growing really huge
178 void connection_hashtable_reset(GHashTable *connection_track_table)
180 g_hash_table_remove_all(connection_track_table);
183 /* if not found, create a new connection and add to hash table */
184 Connection *connection_get(GHashTable *connection_track_table,
185 ConnectionKey *key,
186 GQueue *conn_list)
188 Connection *conn = g_hash_table_lookup(connection_track_table, key);
190 if (conn == NULL) {
191 ConnectionKey *new_key = g_memdup(key, sizeof(*key));
193 conn = connection_new(key);
195 if (g_hash_table_size(connection_track_table) > HASHTABLE_MAX_SIZE) {
196 trace_colo_proxy_main("colo proxy connection hashtable full,"
197 " clear it");
198 connection_hashtable_reset(connection_track_table);
200 * clear the conn_list
202 while (!g_queue_is_empty(conn_list)) {
203 connection_destroy(g_queue_pop_head(conn_list));
207 g_hash_table_insert(connection_track_table, new_key, conn);
210 return conn;