2
0

colo.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "qemu/osdep.h"
  15. #include "trace.h"
  16. #include "net/colo.h"
  17. uint32_t connection_key_hash(const void *opaque)
  18. {
  19. const ConnectionKey *key = opaque;
  20. uint32_t a, b, c;
  21. /* Jenkins hash */
  22. a = b = c = JHASH_INITVAL + sizeof(*key);
  23. a += key->src.s_addr;
  24. b += key->dst.s_addr;
  25. c += (key->src_port | key->dst_port << 16);
  26. __jhash_mix(a, b, c);
  27. a += key->ip_proto;
  28. __jhash_final(a, b, c);
  29. return c;
  30. }
  31. int connection_key_equal(const void *key1, const void *key2)
  32. {
  33. return memcmp(key1, key2, sizeof(ConnectionKey)) == 0;
  34. }
  35. int parse_packet_early(Packet *pkt)
  36. {
  37. int network_length;
  38. static const uint8_t vlan[] = {0x81, 0x00};
  39. uint8_t *data = pkt->data;
  40. uint16_t l3_proto;
  41. ssize_t l2hdr_len = eth_get_l2_hdr_length(data);
  42. if (pkt->size < ETH_HLEN) {
  43. trace_colo_proxy_main("pkt->size < ETH_HLEN");
  44. return 1;
  45. }
  46. /*
  47. * TODO: support vlan.
  48. */
  49. if (!memcmp(&data[12], vlan, sizeof(vlan))) {
  50. trace_colo_proxy_main("COLO-proxy don't support vlan");
  51. return 1;
  52. }
  53. pkt->network_header = data + l2hdr_len;
  54. const struct iovec l2vec = {
  55. .iov_base = (void *) data,
  56. .iov_len = l2hdr_len
  57. };
  58. l3_proto = eth_get_l3_proto(&l2vec, 1, l2hdr_len);
  59. if (l3_proto != ETH_P_IP) {
  60. return 1;
  61. }
  62. network_length = pkt->ip->ip_hl * 4;
  63. if (pkt->size < l2hdr_len + network_length) {
  64. trace_colo_proxy_main("pkt->size < network_header + network_length");
  65. return 1;
  66. }
  67. pkt->transport_header = pkt->network_header + network_length;
  68. return 0;
  69. }
  70. void fill_connection_key(Packet *pkt, ConnectionKey *key)
  71. {
  72. uint32_t tmp_ports;
  73. memset(key, 0, sizeof(*key));
  74. key->ip_proto = pkt->ip->ip_p;
  75. switch (key->ip_proto) {
  76. case IPPROTO_TCP:
  77. case IPPROTO_UDP:
  78. case IPPROTO_DCCP:
  79. case IPPROTO_ESP:
  80. case IPPROTO_SCTP:
  81. case IPPROTO_UDPLITE:
  82. tmp_ports = *(uint32_t *)(pkt->transport_header);
  83. key->src = pkt->ip->ip_src;
  84. key->dst = pkt->ip->ip_dst;
  85. key->src_port = ntohs(tmp_ports & 0xffff);
  86. key->dst_port = ntohs(tmp_ports >> 16);
  87. break;
  88. case IPPROTO_AH:
  89. tmp_ports = *(uint32_t *)(pkt->transport_header + 4);
  90. key->src = pkt->ip->ip_src;
  91. key->dst = pkt->ip->ip_dst;
  92. key->src_port = ntohs(tmp_ports & 0xffff);
  93. key->dst_port = ntohs(tmp_ports >> 16);
  94. break;
  95. default:
  96. break;
  97. }
  98. }
  99. void reverse_connection_key(ConnectionKey *key)
  100. {
  101. struct in_addr tmp_ip;
  102. uint16_t tmp_port;
  103. tmp_ip = key->src;
  104. key->src = key->dst;
  105. key->dst = tmp_ip;
  106. tmp_port = key->src_port;
  107. key->src_port = key->dst_port;
  108. key->dst_port = tmp_port;
  109. }
  110. Connection *connection_new(ConnectionKey *key)
  111. {
  112. Connection *conn = g_slice_new(Connection);
  113. conn->ip_proto = key->ip_proto;
  114. conn->processing = false;
  115. conn->offset = 0;
  116. conn->syn_flag = 0;
  117. g_queue_init(&conn->primary_list);
  118. g_queue_init(&conn->secondary_list);
  119. return conn;
  120. }
  121. void connection_destroy(void *opaque)
  122. {
  123. Connection *conn = opaque;
  124. g_queue_foreach(&conn->primary_list, packet_destroy, NULL);
  125. g_queue_clear(&conn->primary_list);
  126. g_queue_foreach(&conn->secondary_list, packet_destroy, NULL);
  127. g_queue_clear(&conn->secondary_list);
  128. g_slice_free(Connection, conn);
  129. }
  130. Packet *packet_new(const void *data, int size)
  131. {
  132. Packet *pkt = g_slice_new(Packet);
  133. pkt->data = g_memdup(data, size);
  134. pkt->size = size;
  135. pkt->creation_ms = qemu_clock_get_ms(QEMU_CLOCK_HOST);
  136. return pkt;
  137. }
  138. void packet_destroy(void *opaque, void *user_data)
  139. {
  140. Packet *pkt = opaque;
  141. g_free(pkt->data);
  142. g_slice_free(Packet, pkt);
  143. }
  144. /*
  145. * Clear hashtable, stop this hash growing really huge
  146. */
  147. void connection_hashtable_reset(GHashTable *connection_track_table)
  148. {
  149. g_hash_table_remove_all(connection_track_table);
  150. }
  151. /* if not found, create a new connection and add to hash table */
  152. Connection *connection_get(GHashTable *connection_track_table,
  153. ConnectionKey *key,
  154. GQueue *conn_list)
  155. {
  156. Connection *conn = g_hash_table_lookup(connection_track_table, key);
  157. if (conn == NULL) {
  158. ConnectionKey *new_key = g_memdup(key, sizeof(*key));
  159. conn = connection_new(key);
  160. if (g_hash_table_size(connection_track_table) > HASHTABLE_MAX_SIZE) {
  161. trace_colo_proxy_main("colo proxy connection hashtable full,"
  162. " clear it");
  163. connection_hashtable_reset(connection_track_table);
  164. /*
  165. * clear the conn_list
  166. */
  167. while (!g_queue_is_empty(conn_list)) {
  168. connection_destroy(g_queue_pop_head(conn_list));
  169. }
  170. }
  171. g_hash_table_insert(connection_track_table, new_key, conn);
  172. }
  173. return conn;
  174. }