1 /* 2 * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO) 3 * (a.k.a. Fault Tolerance or Continuous Replication) 4 * 5 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. 6 * Copyright (c) 2016 FUJITSU LIMITED 7 * Copyright (c) 2016 Intel Corporation 8 * 9 * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com> 10 * 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. 13 */ 14 15 #include "qemu/osdep.h" 16 #include "trace.h" 17 #include "colo.h" 18 #include "util.h" 19 20 uint32_t connection_key_hash(const void *opaque) 21 { 22 const ConnectionKey *key = opaque; 23 uint32_t a, b, c; 24 25 /* Jenkins hash */ 26 a = b = c = JHASH_INITVAL + sizeof(*key); 27 a += key->src.s_addr; 28 b += key->dst.s_addr; 29 c += (key->src_port | key->dst_port << 16); 30 __jhash_mix(a, b, c); 31 32 a += key->ip_proto; 33 __jhash_final(a, b, c); 34 35 return c; 36 } 37 38 int connection_key_equal(const void *key1, const void *key2) 39 { 40 return memcmp(key1, key2, sizeof(ConnectionKey)) == 0; 41 } 42 43 int parse_packet_early(Packet *pkt) 44 { 45 int network_length; 46 static const uint8_t vlan[] = {0x81, 0x00}; 47 uint8_t *data = pkt->data + pkt->vnet_hdr_len; 48 uint16_t l3_proto; 49 ssize_t l2hdr_len = eth_get_l2_hdr_length(data); 50 51 if (pkt->size < ETH_HLEN + pkt->vnet_hdr_len) { 52 trace_colo_proxy_main("pkt->size < ETH_HLEN"); 53 return 1; 54 } 55 56 /* 57 * TODO: support vlan. 58 */ 59 if (!memcmp(&data[12], vlan, sizeof(vlan))) { 60 trace_colo_proxy_main("COLO-proxy don't support vlan"); 61 return 1; 62 } 63 64 pkt->network_header = data + l2hdr_len; 65 66 const struct iovec l2vec = { 67 .iov_base = (void *) data, 68 .iov_len = l2hdr_len 69 }; 70 l3_proto = eth_get_l3_proto(&l2vec, 1, l2hdr_len); 71 72 if (l3_proto != ETH_P_IP) { 73 return 1; 74 } 75 76 network_length = pkt->ip->ip_hl * 4; 77 if (pkt->size < l2hdr_len + network_length + pkt->vnet_hdr_len) { 78 trace_colo_proxy_main("pkt->size < network_header + network_length"); 79 return 1; 80 } 81 pkt->transport_header = pkt->network_header + network_length; 82 83 return 0; 84 } 85 86 void extract_ip_and_port(uint32_t tmp_ports, ConnectionKey *key, Packet *pkt) 87 { 88 key->src = pkt->ip->ip_src; 89 key->dst = pkt->ip->ip_dst; 90 key->src_port = ntohs(tmp_ports >> 16); 91 key->dst_port = ntohs(tmp_ports & 0xffff); 92 } 93 94 void fill_connection_key(Packet *pkt, ConnectionKey *key) 95 { 96 uint32_t tmp_ports; 97 98 memset(key, 0, sizeof(*key)); 99 key->ip_proto = pkt->ip->ip_p; 100 101 switch (key->ip_proto) { 102 case IPPROTO_TCP: 103 case IPPROTO_UDP: 104 case IPPROTO_DCCP: 105 case IPPROTO_ESP: 106 case IPPROTO_SCTP: 107 case IPPROTO_UDPLITE: 108 tmp_ports = *(uint32_t *)(pkt->transport_header); 109 extract_ip_and_port(tmp_ports, key, pkt); 110 break; 111 case IPPROTO_AH: 112 tmp_ports = *(uint32_t *)(pkt->transport_header + 4); 113 extract_ip_and_port(tmp_ports, key, pkt); 114 break; 115 default: 116 break; 117 } 118 } 119 120 void reverse_connection_key(ConnectionKey *key) 121 { 122 struct in_addr tmp_ip; 123 uint16_t tmp_port; 124 125 tmp_ip = key->src; 126 key->src = key->dst; 127 key->dst = tmp_ip; 128 129 tmp_port = key->src_port; 130 key->src_port = key->dst_port; 131 key->dst_port = tmp_port; 132 } 133 134 Connection *connection_new(ConnectionKey *key) 135 { 136 Connection *conn = g_slice_new(Connection); 137 138 conn->ip_proto = key->ip_proto; 139 conn->processing = false; 140 conn->offset = 0; 141 conn->tcp_state = TCPS_CLOSED; 142 conn->pack = 0; 143 conn->sack = 0; 144 g_queue_init(&conn->primary_list); 145 g_queue_init(&conn->secondary_list); 146 147 return conn; 148 } 149 150 void connection_destroy(void *opaque) 151 { 152 Connection *conn = opaque; 153 154 g_queue_foreach(&conn->primary_list, packet_destroy, NULL); 155 g_queue_clear(&conn->primary_list); 156 g_queue_foreach(&conn->secondary_list, packet_destroy, NULL); 157 g_queue_clear(&conn->secondary_list); 158 g_slice_free(Connection, conn); 159 } 160 161 Packet *packet_new(const void *data, int size, int vnet_hdr_len) 162 { 163 Packet *pkt = g_slice_new(Packet); 164 165 pkt->data = g_memdup(data, size); 166 pkt->size = size; 167 pkt->creation_ms = qemu_clock_get_ms(QEMU_CLOCK_HOST); 168 pkt->vnet_hdr_len = vnet_hdr_len; 169 pkt->tcp_seq = 0; 170 pkt->tcp_ack = 0; 171 pkt->seq_end = 0; 172 pkt->header_size = 0; 173 pkt->payload_size = 0; 174 pkt->offset = 0; 175 pkt->flags = 0; 176 177 return pkt; 178 } 179 180 void packet_destroy(void *opaque, void *user_data) 181 { 182 Packet *pkt = opaque; 183 184 g_free(pkt->data); 185 g_slice_free(Packet, pkt); 186 } 187 188 /* 189 * Clear hashtable, stop this hash growing really huge 190 */ 191 void connection_hashtable_reset(GHashTable *connection_track_table) 192 { 193 g_hash_table_remove_all(connection_track_table); 194 } 195 196 /* if not found, create a new connection and add to hash table */ 197 Connection *connection_get(GHashTable *connection_track_table, 198 ConnectionKey *key, 199 GQueue *conn_list) 200 { 201 Connection *conn = g_hash_table_lookup(connection_track_table, key); 202 203 if (conn == NULL) { 204 ConnectionKey *new_key = g_memdup(key, sizeof(*key)); 205 206 conn = connection_new(key); 207 208 if (g_hash_table_size(connection_track_table) > HASHTABLE_MAX_SIZE) { 209 trace_colo_proxy_main("colo proxy connection hashtable full," 210 " clear it"); 211 connection_hashtable_reset(connection_track_table); 212 /* 213 * clear the conn_list 214 */ 215 while (!g_queue_is_empty(conn_list)) { 216 connection_destroy(g_queue_pop_head(conn_list)); 217 } 218 } 219 220 g_hash_table_insert(connection_track_table, new_key, conn); 221 } 222 223 return conn; 224 } 225 226 bool connection_has_tracked(GHashTable *connection_track_table, 227 ConnectionKey *key) 228 { 229 Connection *conn = g_hash_table_lookup(connection_track_table, key); 230 231 return conn ? true : false; 232 } 233