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