filter-rewriter.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
  3. * Copyright (c) 2016 FUJITSU LIMITED
  4. * Copyright (c) 2016 Intel Corporation
  5. *
  6. * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
  7. *
  8. * This work is licensed under the terms of the GNU GPL, version 2 or
  9. * later. See the COPYING file in the top-level directory.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "trace.h"
  13. #include "colo.h"
  14. #include "net/filter.h"
  15. #include "net/net.h"
  16. #include "qemu/error-report.h"
  17. #include "qom/object.h"
  18. #include "qemu/main-loop.h"
  19. #include "qemu/iov.h"
  20. #include "net/checksum.h"
  21. #include "net/colo.h"
  22. #include "migration/colo.h"
  23. #include "util.h"
  24. #define FILTER_COLO_REWRITER(obj) \
  25. OBJECT_CHECK(RewriterState, (obj), TYPE_FILTER_REWRITER)
  26. #define TYPE_FILTER_REWRITER "filter-rewriter"
  27. #define FAILOVER_MODE_ON true
  28. #define FAILOVER_MODE_OFF false
  29. typedef struct RewriterState {
  30. NetFilterState parent_obj;
  31. NetQueue *incoming_queue;
  32. /* hashtable to save connection */
  33. GHashTable *connection_track_table;
  34. bool vnet_hdr;
  35. bool failover_mode;
  36. } RewriterState;
  37. static void filter_rewriter_failover_mode(RewriterState *s)
  38. {
  39. s->failover_mode = FAILOVER_MODE_ON;
  40. }
  41. static void filter_rewriter_flush(NetFilterState *nf)
  42. {
  43. RewriterState *s = FILTER_COLO_REWRITER(nf);
  44. if (!qemu_net_queue_flush(s->incoming_queue)) {
  45. /* Unable to empty the queue, purge remaining packets */
  46. qemu_net_queue_purge(s->incoming_queue, nf->netdev);
  47. }
  48. }
  49. /*
  50. * Return 1 on success, if return 0 means the pkt
  51. * is not TCP packet
  52. */
  53. static int is_tcp_packet(Packet *pkt)
  54. {
  55. if (!parse_packet_early(pkt) &&
  56. pkt->ip->ip_p == IPPROTO_TCP) {
  57. return 1;
  58. } else {
  59. return 0;
  60. }
  61. }
  62. /* handle tcp packet from primary guest */
  63. static int handle_primary_tcp_pkt(RewriterState *rf,
  64. Connection *conn,
  65. Packet *pkt, ConnectionKey *key)
  66. {
  67. struct tcp_hdr *tcp_pkt;
  68. tcp_pkt = (struct tcp_hdr *)pkt->transport_header;
  69. if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
  70. trace_colo_filter_rewriter_pkt_info(__func__,
  71. inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
  72. ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
  73. tcp_pkt->th_flags);
  74. trace_colo_filter_rewriter_conn_offset(conn->offset);
  75. }
  76. if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN)) &&
  77. conn->tcp_state == TCPS_SYN_SENT) {
  78. conn->tcp_state = TCPS_ESTABLISHED;
  79. }
  80. if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
  81. /*
  82. * we use this flag update offset func
  83. * run once in independent tcp connection
  84. */
  85. conn->tcp_state = TCPS_SYN_RECEIVED;
  86. }
  87. if (((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK)) {
  88. if (conn->tcp_state == TCPS_SYN_RECEIVED) {
  89. /*
  90. * offset = secondary_seq - primary seq
  91. * ack packet sent by guest from primary node,
  92. * so we use th_ack - 1 get primary_seq
  93. */
  94. conn->offset -= (ntohl(tcp_pkt->th_ack) - 1);
  95. conn->tcp_state = TCPS_ESTABLISHED;
  96. }
  97. if (conn->offset) {
  98. /* handle packets to the secondary from the primary */
  99. tcp_pkt->th_ack = htonl(ntohl(tcp_pkt->th_ack) + conn->offset);
  100. net_checksum_calculate((uint8_t *)pkt->data + pkt->vnet_hdr_len,
  101. pkt->size - pkt->vnet_hdr_len);
  102. }
  103. /*
  104. * Passive close step 3
  105. */
  106. if ((conn->tcp_state == TCPS_LAST_ACK) &&
  107. (ntohl(tcp_pkt->th_ack) == (conn->fin_ack_seq + 1))) {
  108. conn->tcp_state = TCPS_CLOSED;
  109. g_hash_table_remove(rf->connection_track_table, key);
  110. }
  111. }
  112. if ((tcp_pkt->th_flags & TH_FIN) == TH_FIN) {
  113. /*
  114. * Passive close.
  115. * Step 1:
  116. * The *server* side of this connect is VM, *client* tries to close
  117. * the connection. We will into CLOSE_WAIT status.
  118. *
  119. * Step 2:
  120. * In this step we will into LAST_ACK status.
  121. *
  122. * We got 'fin=1, ack=1' packet from server side, we need to
  123. * record the seq of 'fin=1, ack=1' packet.
  124. *
  125. * Step 3:
  126. * We got 'ack=1' packets from client side, it acks 'fin=1, ack=1'
  127. * packet from server side. From this point, we can ensure that there
  128. * will be no packets in the connection, except that, some errors
  129. * happen between the path of 'filter object' and vNIC, if this rare
  130. * case really happen, we can still create a new connection,
  131. * So it is safe to remove the connection from connection_track_table.
  132. *
  133. */
  134. if (conn->tcp_state == TCPS_ESTABLISHED) {
  135. conn->tcp_state = TCPS_CLOSE_WAIT;
  136. }
  137. /*
  138. * Active close step 2.
  139. */
  140. if (conn->tcp_state == TCPS_FIN_WAIT_1) {
  141. /*
  142. * For simplify implementation, we needn't wait 2MSL time
  143. * in filter rewriter. Because guest kernel will track the
  144. * TCP status and wait 2MSL time, if client resend the FIN
  145. * packet, guest will apply the last ACK too.
  146. * So, we skip the TCPS_TIME_WAIT state here and go straight
  147. * to TCPS_CLOSED state.
  148. */
  149. conn->tcp_state = TCPS_CLOSED;
  150. g_hash_table_remove(rf->connection_track_table, key);
  151. }
  152. }
  153. return 0;
  154. }
  155. /* handle tcp packet from secondary guest */
  156. static int handle_secondary_tcp_pkt(RewriterState *rf,
  157. Connection *conn,
  158. Packet *pkt, ConnectionKey *key)
  159. {
  160. struct tcp_hdr *tcp_pkt;
  161. tcp_pkt = (struct tcp_hdr *)pkt->transport_header;
  162. if (trace_event_get_state_backends(TRACE_COLO_FILTER_REWRITER_DEBUG)) {
  163. trace_colo_filter_rewriter_pkt_info(__func__,
  164. inet_ntoa(pkt->ip->ip_src), inet_ntoa(pkt->ip->ip_dst),
  165. ntohl(tcp_pkt->th_seq), ntohl(tcp_pkt->th_ack),
  166. tcp_pkt->th_flags);
  167. trace_colo_filter_rewriter_conn_offset(conn->offset);
  168. }
  169. if (conn->tcp_state == TCPS_SYN_RECEIVED &&
  170. ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == (TH_ACK | TH_SYN))) {
  171. /*
  172. * save offset = secondary_seq and then
  173. * in handle_primary_tcp_pkt make offset
  174. * = secondary_seq - primary_seq
  175. */
  176. conn->offset = ntohl(tcp_pkt->th_seq);
  177. }
  178. /* VM active connect */
  179. if (conn->tcp_state == TCPS_CLOSED &&
  180. ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_SYN)) {
  181. conn->tcp_state = TCPS_SYN_SENT;
  182. }
  183. if ((tcp_pkt->th_flags & (TH_ACK | TH_SYN)) == TH_ACK) {
  184. /* Only need to adjust seq while offset is Non-zero */
  185. if (conn->offset) {
  186. /* handle packets to the primary from the secondary*/
  187. tcp_pkt->th_seq = htonl(ntohl(tcp_pkt->th_seq) - conn->offset);
  188. net_checksum_calculate((uint8_t *)pkt->data + pkt->vnet_hdr_len,
  189. pkt->size - pkt->vnet_hdr_len);
  190. }
  191. }
  192. /*
  193. * Passive close step 2:
  194. */
  195. if (conn->tcp_state == TCPS_CLOSE_WAIT &&
  196. (tcp_pkt->th_flags & (TH_ACK | TH_FIN)) == (TH_ACK | TH_FIN)) {
  197. conn->fin_ack_seq = ntohl(tcp_pkt->th_seq);
  198. conn->tcp_state = TCPS_LAST_ACK;
  199. }
  200. /*
  201. * Active close
  202. *
  203. * Step 1:
  204. * The *server* side of this connect is VM, *server* tries to close
  205. * the connection.
  206. *
  207. * Step 2:
  208. * We will into CLOSE_WAIT status.
  209. * We simplify the TCPS_FIN_WAIT_2, TCPS_TIME_WAIT and
  210. * CLOSING status.
  211. */
  212. if (conn->tcp_state == TCPS_ESTABLISHED &&
  213. (tcp_pkt->th_flags & (TH_ACK | TH_FIN)) == TH_FIN) {
  214. conn->tcp_state = TCPS_FIN_WAIT_1;
  215. }
  216. return 0;
  217. }
  218. static ssize_t colo_rewriter_receive_iov(NetFilterState *nf,
  219. NetClientState *sender,
  220. unsigned flags,
  221. const struct iovec *iov,
  222. int iovcnt,
  223. NetPacketSent *sent_cb)
  224. {
  225. RewriterState *s = FILTER_COLO_REWRITER(nf);
  226. Connection *conn;
  227. ConnectionKey key;
  228. Packet *pkt;
  229. ssize_t size = iov_size(iov, iovcnt);
  230. ssize_t vnet_hdr_len = 0;
  231. char *buf = g_malloc0(size);
  232. iov_to_buf(iov, iovcnt, 0, buf, size);
  233. if (s->vnet_hdr) {
  234. vnet_hdr_len = nf->netdev->vnet_hdr_len;
  235. }
  236. pkt = packet_new(buf, size, vnet_hdr_len);
  237. g_free(buf);
  238. /*
  239. * if we get tcp packet
  240. * we will rewrite it to make secondary guest's
  241. * connection established successfully
  242. */
  243. if (pkt && is_tcp_packet(pkt)) {
  244. fill_connection_key(pkt, &key);
  245. if (sender == nf->netdev) {
  246. /*
  247. * We need make tcp TX and RX packet
  248. * into one connection.
  249. */
  250. reverse_connection_key(&key);
  251. }
  252. /* After failover we needn't change new TCP packet */
  253. if (s->failover_mode &&
  254. !connection_has_tracked(s->connection_track_table, &key)) {
  255. goto out;
  256. }
  257. conn = connection_get(s->connection_track_table,
  258. &key,
  259. NULL);
  260. if (sender == nf->netdev) {
  261. /* NET_FILTER_DIRECTION_TX */
  262. if (!handle_primary_tcp_pkt(s, conn, pkt, &key)) {
  263. qemu_net_queue_send(s->incoming_queue, sender, 0,
  264. (const uint8_t *)pkt->data, pkt->size, NULL);
  265. packet_destroy(pkt, NULL);
  266. pkt = NULL;
  267. /*
  268. * We block the packet here,after rewrite pkt
  269. * and will send it
  270. */
  271. return 1;
  272. }
  273. } else {
  274. /* NET_FILTER_DIRECTION_RX */
  275. if (!handle_secondary_tcp_pkt(s, conn, pkt, &key)) {
  276. qemu_net_queue_send(s->incoming_queue, sender, 0,
  277. (const uint8_t *)pkt->data, pkt->size, NULL);
  278. packet_destroy(pkt, NULL);
  279. pkt = NULL;
  280. /*
  281. * We block the packet here,after rewrite pkt
  282. * and will send it
  283. */
  284. return 1;
  285. }
  286. }
  287. }
  288. out:
  289. packet_destroy(pkt, NULL);
  290. pkt = NULL;
  291. return 0;
  292. }
  293. static void reset_seq_offset(gpointer key, gpointer value, gpointer user_data)
  294. {
  295. Connection *conn = (Connection *)value;
  296. conn->offset = 0;
  297. }
  298. static gboolean offset_is_nonzero(gpointer key,
  299. gpointer value,
  300. gpointer user_data)
  301. {
  302. Connection *conn = (Connection *)value;
  303. return conn->offset ? true : false;
  304. }
  305. static void colo_rewriter_handle_event(NetFilterState *nf, int event,
  306. Error **errp)
  307. {
  308. RewriterState *rs = FILTER_COLO_REWRITER(nf);
  309. switch (event) {
  310. case COLO_EVENT_CHECKPOINT:
  311. g_hash_table_foreach(rs->connection_track_table,
  312. reset_seq_offset, NULL);
  313. break;
  314. case COLO_EVENT_FAILOVER:
  315. if (!g_hash_table_find(rs->connection_track_table,
  316. offset_is_nonzero, NULL)) {
  317. filter_rewriter_failover_mode(rs);
  318. }
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. static void colo_rewriter_cleanup(NetFilterState *nf)
  325. {
  326. RewriterState *s = FILTER_COLO_REWRITER(nf);
  327. /* flush packets */
  328. if (s->incoming_queue) {
  329. filter_rewriter_flush(nf);
  330. g_free(s->incoming_queue);
  331. }
  332. }
  333. static void colo_rewriter_setup(NetFilterState *nf, Error **errp)
  334. {
  335. RewriterState *s = FILTER_COLO_REWRITER(nf);
  336. s->connection_track_table = g_hash_table_new_full(connection_key_hash,
  337. connection_key_equal,
  338. g_free,
  339. connection_destroy);
  340. s->incoming_queue = qemu_new_net_queue(qemu_netfilter_pass_to_next, nf);
  341. }
  342. static bool filter_rewriter_get_vnet_hdr(Object *obj, Error **errp)
  343. {
  344. RewriterState *s = FILTER_COLO_REWRITER(obj);
  345. return s->vnet_hdr;
  346. }
  347. static void filter_rewriter_set_vnet_hdr(Object *obj,
  348. bool value,
  349. Error **errp)
  350. {
  351. RewriterState *s = FILTER_COLO_REWRITER(obj);
  352. s->vnet_hdr = value;
  353. }
  354. static void filter_rewriter_init(Object *obj)
  355. {
  356. RewriterState *s = FILTER_COLO_REWRITER(obj);
  357. s->vnet_hdr = false;
  358. s->failover_mode = FAILOVER_MODE_OFF;
  359. object_property_add_bool(obj, "vnet_hdr_support",
  360. filter_rewriter_get_vnet_hdr,
  361. filter_rewriter_set_vnet_hdr);
  362. }
  363. static void colo_rewriter_class_init(ObjectClass *oc, void *data)
  364. {
  365. NetFilterClass *nfc = NETFILTER_CLASS(oc);
  366. nfc->setup = colo_rewriter_setup;
  367. nfc->cleanup = colo_rewriter_cleanup;
  368. nfc->receive_iov = colo_rewriter_receive_iov;
  369. nfc->handle_event = colo_rewriter_handle_event;
  370. }
  371. static const TypeInfo colo_rewriter_info = {
  372. .name = TYPE_FILTER_REWRITER,
  373. .parent = TYPE_NETFILTER,
  374. .class_init = colo_rewriter_class_init,
  375. .instance_init = filter_rewriter_init,
  376. .instance_size = sizeof(RewriterState),
  377. };
  378. static void register_types(void)
  379. {
  380. type_register_static(&colo_rewriter_info);
  381. }
  382. type_init(register_types);