xref: /openbmc/qemu/net/colo.c (revision 44602af8)
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;
50 
51     if (data == NULL) {
52         trace_colo_proxy_main_vnet_info("This packet is not parsed correctly, "
53                                         "pkt->vnet_hdr_len", pkt->vnet_hdr_len);
54         return 1;
55     }
56     l2hdr_len = eth_get_l2_hdr_length(data);
57 
58     if (pkt->size < ETH_HLEN + pkt->vnet_hdr_len) {
59         trace_colo_proxy_main("pkt->size < ETH_HLEN");
60         return 1;
61     }
62 
63     /*
64      * TODO: support vlan.
65      */
66     if (!memcmp(&data[12], vlan, sizeof(vlan))) {
67         trace_colo_proxy_main("COLO-proxy don't support vlan");
68         return 1;
69     }
70 
71     pkt->network_header = data + l2hdr_len;
72 
73     const struct iovec l2vec = {
74         .iov_base = (void *) data,
75         .iov_len = l2hdr_len
76     };
77     l3_proto = eth_get_l3_proto(&l2vec, 1, l2hdr_len);
78 
79     if (l3_proto != ETH_P_IP) {
80         return 1;
81     }
82 
83     network_length = pkt->ip->ip_hl * 4;
84     if (pkt->size < l2hdr_len + network_length + pkt->vnet_hdr_len) {
85         trace_colo_proxy_main("pkt->size < network_header + network_length");
86         return 1;
87     }
88     pkt->transport_header = pkt->network_header + network_length;
89 
90     return 0;
91 }
92 
93 void extract_ip_and_port(uint32_t tmp_ports, ConnectionKey *key,
94                          Packet *pkt, bool reverse)
95 {
96     if (reverse) {
97         key->src = pkt->ip->ip_dst;
98         key->dst = pkt->ip->ip_src;
99         key->src_port = ntohs(tmp_ports & 0xffff);
100         key->dst_port = ntohs(tmp_ports >> 16);
101     } else {
102         key->src = pkt->ip->ip_src;
103         key->dst = pkt->ip->ip_dst;
104         key->src_port = ntohs(tmp_ports >> 16);
105         key->dst_port = ntohs(tmp_ports & 0xffff);
106     }
107 }
108 
109 void fill_connection_key(Packet *pkt, ConnectionKey *key, bool reverse)
110 {
111     uint32_t tmp_ports = 0;
112 
113     key->ip_proto = pkt->ip->ip_p;
114 
115     switch (key->ip_proto) {
116     case IPPROTO_TCP:
117     case IPPROTO_UDP:
118     case IPPROTO_DCCP:
119     case IPPROTO_ESP:
120     case IPPROTO_SCTP:
121     case IPPROTO_UDPLITE:
122         tmp_ports = *(uint32_t *)(pkt->transport_header);
123         break;
124     case IPPROTO_AH:
125         tmp_ports = *(uint32_t *)(pkt->transport_header + 4);
126         break;
127     default:
128         break;
129     }
130 
131     extract_ip_and_port(tmp_ports, key, pkt, reverse);
132 }
133 
134 Connection *connection_new(ConnectionKey *key)
135 {
136     Connection *conn = g_slice_new0(Connection);
137 
138     conn->ip_proto = key->ip_proto;
139     conn->processing = false;
140     conn->tcp_state = TCPS_CLOSED;
141     g_queue_init(&conn->primary_list);
142     g_queue_init(&conn->secondary_list);
143 
144     return conn;
145 }
146 
147 void connection_destroy(void *opaque)
148 {
149     Connection *conn = opaque;
150 
151     g_queue_foreach(&conn->primary_list, packet_destroy, NULL);
152     g_queue_clear(&conn->primary_list);
153     g_queue_foreach(&conn->secondary_list, packet_destroy, NULL);
154     g_queue_clear(&conn->secondary_list);
155     g_slice_free(Connection, conn);
156 }
157 
158 Packet *packet_new(const void *data, int size, int vnet_hdr_len)
159 {
160     Packet *pkt = g_slice_new0(Packet);
161 
162     pkt->data = g_memdup(data, size);
163     pkt->size = size;
164     pkt->creation_ms = qemu_clock_get_ms(QEMU_CLOCK_HOST);
165     pkt->vnet_hdr_len = vnet_hdr_len;
166 
167     return pkt;
168 }
169 
170 /*
171  * packet_new_nocopy will not copy data, so the caller can't release
172  * the data. And it will be released in packet_destroy.
173  */
174 Packet *packet_new_nocopy(void *data, int size, int vnet_hdr_len)
175 {
176     Packet *pkt = g_slice_new0(Packet);
177 
178     pkt->data = data;
179     pkt->size = size;
180     pkt->creation_ms = qemu_clock_get_ms(QEMU_CLOCK_HOST);
181     pkt->vnet_hdr_len = vnet_hdr_len;
182 
183     return pkt;
184 }
185 
186 void packet_destroy(void *opaque, void *user_data)
187 {
188     Packet *pkt = opaque;
189 
190     g_free(pkt->data);
191     g_slice_free(Packet, pkt);
192 }
193 
194 void packet_destroy_partial(void *opaque, void *user_data)
195 {
196     Packet *pkt = opaque;
197 
198     g_slice_free(Packet, pkt);
199 }
200 
201 /*
202  * Clear hashtable, stop this hash growing really huge
203  */
204 void connection_hashtable_reset(GHashTable *connection_track_table)
205 {
206     g_hash_table_remove_all(connection_track_table);
207 }
208 
209 /* if not found, create a new connection and add to hash table */
210 Connection *connection_get(GHashTable *connection_track_table,
211                            ConnectionKey *key,
212                            GQueue *conn_list)
213 {
214     Connection *conn = g_hash_table_lookup(connection_track_table, key);
215 
216     if (conn == NULL) {
217         ConnectionKey *new_key = g_memdup(key, sizeof(*key));
218 
219         conn = connection_new(key);
220 
221         if (g_hash_table_size(connection_track_table) > HASHTABLE_MAX_SIZE) {
222             trace_colo_proxy_main("colo proxy connection hashtable full,"
223                                   " clear it");
224             connection_hashtable_reset(connection_track_table);
225             /*
226              * clear the conn_list
227              */
228             while (conn_list && !g_queue_is_empty(conn_list)) {
229                 connection_destroy(g_queue_pop_head(conn_list));
230             }
231         }
232 
233         g_hash_table_insert(connection_track_table, new_key, conn);
234     }
235 
236     return conn;
237 }
238 
239 bool connection_has_tracked(GHashTable *connection_track_table,
240                             ConnectionKey *key)
241 {
242     Connection *conn = g_hash_table_lookup(connection_track_table, key);
243 
244     return conn ? true : false;
245 }
246