migration.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823
  1. /*
  2. * QEMU live migration
  3. *
  4. * Copyright IBM, Corp. 2008
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "qemu/cutils.h"
  17. #include "qemu/error-report.h"
  18. #include "qemu/main-loop.h"
  19. #include "migration/blocker.h"
  20. #include "exec.h"
  21. #include "fd.h"
  22. #include "socket.h"
  23. #include "sysemu/runstate.h"
  24. #include "sysemu/sysemu.h"
  25. #include "sysemu/cpu-throttle.h"
  26. #include "rdma.h"
  27. #include "ram.h"
  28. #include "migration/global_state.h"
  29. #include "migration/misc.h"
  30. #include "migration.h"
  31. #include "savevm.h"
  32. #include "qemu-file-channel.h"
  33. #include "qemu-file.h"
  34. #include "migration/vmstate.h"
  35. #include "block/block.h"
  36. #include "qapi/error.h"
  37. #include "qapi/clone-visitor.h"
  38. #include "qapi/qapi-visit-sockets.h"
  39. #include "qapi/qapi-commands-migration.h"
  40. #include "qapi/qapi-events-migration.h"
  41. #include "qapi/qmp/qerror.h"
  42. #include "qapi/qmp/qnull.h"
  43. #include "qemu/rcu.h"
  44. #include "block.h"
  45. #include "postcopy-ram.h"
  46. #include "qemu/thread.h"
  47. #include "trace.h"
  48. #include "exec/target_page.h"
  49. #include "io/channel-buffer.h"
  50. #include "migration/colo.h"
  51. #include "hw/boards.h"
  52. #include "hw/qdev-properties.h"
  53. #include "monitor/monitor.h"
  54. #include "net/announce.h"
  55. #include "qemu/queue.h"
  56. #include "multifd.h"
  57. #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */
  58. /* Amount of time to allocate to each "chunk" of bandwidth-throttled
  59. * data. */
  60. #define BUFFER_DELAY 100
  61. #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
  62. /* Time in milliseconds we are allowed to stop the source,
  63. * for sending the last part */
  64. #define DEFAULT_MIGRATE_SET_DOWNTIME 300
  65. /* Maximum migrate downtime set to 2000 seconds */
  66. #define MAX_MIGRATE_DOWNTIME_SECONDS 2000
  67. #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000)
  68. /* Default compression thread count */
  69. #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8
  70. /* Default decompression thread count, usually decompression is at
  71. * least 4 times as fast as compression.*/
  72. #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2
  73. /*0: means nocompress, 1: best speed, ... 9: best compress ratio */
  74. #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1
  75. /* Define default autoconverge cpu throttle migration parameters */
  76. #define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50
  77. #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20
  78. #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10
  79. #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99
  80. /* Migration XBZRLE default cache size */
  81. #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024)
  82. /* The delay time (in ms) between two COLO checkpoints */
  83. #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100)
  84. #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2
  85. #define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE
  86. /* 0: means nocompress, 1: best speed, ... 9: best compress ratio */
  87. #define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1
  88. /* 0: means nocompress, 1: best speed, ... 20: best compress ratio */
  89. #define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1
  90. /* Background transfer rate for postcopy, 0 means unlimited, note
  91. * that page requests can still exceed this limit.
  92. */
  93. #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0
  94. /*
  95. * Parameters for self_announce_delay giving a stream of RARP/ARP
  96. * packets after migration.
  97. */
  98. #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50
  99. #define DEFAULT_MIGRATE_ANNOUNCE_MAX 550
  100. #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5
  101. #define DEFAULT_MIGRATE_ANNOUNCE_STEP 100
  102. static NotifierList migration_state_notifiers =
  103. NOTIFIER_LIST_INITIALIZER(migration_state_notifiers);
  104. static bool deferred_incoming;
  105. /* Messages sent on the return path from destination to source */
  106. enum mig_rp_message_type {
  107. MIG_RP_MSG_INVALID = 0, /* Must be 0 */
  108. MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */
  109. MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */
  110. MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */
  111. MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */
  112. MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */
  113. MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */
  114. MIG_RP_MSG_MAX
  115. };
  116. /* When we add fault tolerance, we could have several
  117. migrations at once. For now we don't need to add
  118. dynamic creation of migration */
  119. static MigrationState *current_migration;
  120. static MigrationIncomingState *current_incoming;
  121. static bool migration_object_check(MigrationState *ms, Error **errp);
  122. static int migration_maybe_pause(MigrationState *s,
  123. int *current_active_state,
  124. int new_state);
  125. static void migrate_fd_cancel(MigrationState *s);
  126. void migration_object_init(void)
  127. {
  128. MachineState *ms = MACHINE(qdev_get_machine());
  129. Error *err = NULL;
  130. /* This can only be called once. */
  131. assert(!current_migration);
  132. current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION));
  133. /*
  134. * Init the migrate incoming object as well no matter whether
  135. * we'll use it or not.
  136. */
  137. assert(!current_incoming);
  138. current_incoming = g_new0(MigrationIncomingState, 1);
  139. current_incoming->state = MIGRATION_STATUS_NONE;
  140. current_incoming->postcopy_remote_fds =
  141. g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD));
  142. qemu_mutex_init(&current_incoming->rp_mutex);
  143. qemu_event_init(&current_incoming->main_thread_load_event, false);
  144. qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
  145. qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
  146. if (!migration_object_check(current_migration, &err)) {
  147. error_report_err(err);
  148. exit(1);
  149. }
  150. /*
  151. * We cannot really do this in migration_instance_init() since at
  152. * that time global properties are not yet applied, then this
  153. * value will be definitely replaced by something else.
  154. */
  155. if (ms->enforce_config_section) {
  156. current_migration->send_configuration = true;
  157. }
  158. }
  159. void migration_shutdown(void)
  160. {
  161. /*
  162. * Cancel the current migration - that will (eventually)
  163. * stop the migration using this structure
  164. */
  165. migrate_fd_cancel(current_migration);
  166. object_unref(OBJECT(current_migration));
  167. /*
  168. * Cancel outgoing migration of dirty bitmaps. It should
  169. * at least unref used block nodes.
  170. */
  171. dirty_bitmap_mig_cancel_outgoing();
  172. /*
  173. * Cancel incoming migration of dirty bitmaps. Dirty bitmaps
  174. * are non-critical data, and their loss never considered as
  175. * something serious.
  176. */
  177. dirty_bitmap_mig_cancel_incoming();
  178. }
  179. /* For outgoing */
  180. MigrationState *migrate_get_current(void)
  181. {
  182. /* This can only be called after the object created. */
  183. assert(current_migration);
  184. return current_migration;
  185. }
  186. MigrationIncomingState *migration_incoming_get_current(void)
  187. {
  188. assert(current_incoming);
  189. return current_incoming;
  190. }
  191. void migration_incoming_state_destroy(void)
  192. {
  193. struct MigrationIncomingState *mis = migration_incoming_get_current();
  194. if (mis->to_src_file) {
  195. /* Tell source that we are done */
  196. migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
  197. qemu_fclose(mis->to_src_file);
  198. mis->to_src_file = NULL;
  199. }
  200. if (mis->from_src_file) {
  201. qemu_fclose(mis->from_src_file);
  202. mis->from_src_file = NULL;
  203. }
  204. if (mis->postcopy_remote_fds) {
  205. g_array_free(mis->postcopy_remote_fds, TRUE);
  206. mis->postcopy_remote_fds = NULL;
  207. }
  208. qemu_event_reset(&mis->main_thread_load_event);
  209. if (mis->socket_address_list) {
  210. qapi_free_SocketAddressList(mis->socket_address_list);
  211. mis->socket_address_list = NULL;
  212. }
  213. }
  214. static void migrate_generate_event(int new_state)
  215. {
  216. if (migrate_use_events()) {
  217. qapi_event_send_migration(new_state);
  218. }
  219. }
  220. static bool migrate_late_block_activate(void)
  221. {
  222. MigrationState *s;
  223. s = migrate_get_current();
  224. return s->enabled_capabilities[
  225. MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE];
  226. }
  227. /*
  228. * Called on -incoming with a defer: uri.
  229. * The migration can be started later after any parameters have been
  230. * changed.
  231. */
  232. static void deferred_incoming_migration(Error **errp)
  233. {
  234. if (deferred_incoming) {
  235. error_setg(errp, "Incoming migration already deferred");
  236. }
  237. deferred_incoming = true;
  238. }
  239. /*
  240. * Send a message on the return channel back to the source
  241. * of the migration.
  242. */
  243. static int migrate_send_rp_message(MigrationIncomingState *mis,
  244. enum mig_rp_message_type message_type,
  245. uint16_t len, void *data)
  246. {
  247. int ret = 0;
  248. trace_migrate_send_rp_message((int)message_type, len);
  249. qemu_mutex_lock(&mis->rp_mutex);
  250. /*
  251. * It's possible that the file handle got lost due to network
  252. * failures.
  253. */
  254. if (!mis->to_src_file) {
  255. ret = -EIO;
  256. goto error;
  257. }
  258. qemu_put_be16(mis->to_src_file, (unsigned int)message_type);
  259. qemu_put_be16(mis->to_src_file, len);
  260. qemu_put_buffer(mis->to_src_file, data, len);
  261. qemu_fflush(mis->to_src_file);
  262. /* It's possible that qemu file got error during sending */
  263. ret = qemu_file_get_error(mis->to_src_file);
  264. error:
  265. qemu_mutex_unlock(&mis->rp_mutex);
  266. return ret;
  267. }
  268. /* Request a range of pages from the source VM at the given
  269. * start address.
  270. * rbname: Name of the RAMBlock to request the page in, if NULL it's the same
  271. * as the last request (a name must have been given previously)
  272. * Start: Address offset within the RB
  273. * Len: Length in bytes required - must be a multiple of pagesize
  274. */
  275. int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname,
  276. ram_addr_t start, size_t len)
  277. {
  278. uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
  279. size_t msglen = 12; /* start + len */
  280. enum mig_rp_message_type msg_type;
  281. *(uint64_t *)bufc = cpu_to_be64((uint64_t)start);
  282. *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len);
  283. if (rbname) {
  284. int rbname_len = strlen(rbname);
  285. assert(rbname_len < 256);
  286. bufc[msglen++] = rbname_len;
  287. memcpy(bufc + msglen, rbname, rbname_len);
  288. msglen += rbname_len;
  289. msg_type = MIG_RP_MSG_REQ_PAGES_ID;
  290. } else {
  291. msg_type = MIG_RP_MSG_REQ_PAGES;
  292. }
  293. return migrate_send_rp_message(mis, msg_type, msglen, bufc);
  294. }
  295. static bool migration_colo_enabled;
  296. bool migration_incoming_colo_enabled(void)
  297. {
  298. return migration_colo_enabled;
  299. }
  300. void migration_incoming_disable_colo(void)
  301. {
  302. ram_block_discard_disable(false);
  303. migration_colo_enabled = false;
  304. }
  305. int migration_incoming_enable_colo(void)
  306. {
  307. if (ram_block_discard_disable(true)) {
  308. error_report("COLO: cannot disable RAM discard");
  309. return -EBUSY;
  310. }
  311. migration_colo_enabled = true;
  312. return 0;
  313. }
  314. void migrate_add_address(SocketAddress *address)
  315. {
  316. MigrationIncomingState *mis = migration_incoming_get_current();
  317. SocketAddressList *addrs;
  318. addrs = g_new0(SocketAddressList, 1);
  319. addrs->next = mis->socket_address_list;
  320. mis->socket_address_list = addrs;
  321. addrs->value = QAPI_CLONE(SocketAddress, address);
  322. }
  323. void qemu_start_incoming_migration(const char *uri, Error **errp)
  324. {
  325. const char *p;
  326. qapi_event_send_migration(MIGRATION_STATUS_SETUP);
  327. if (!strcmp(uri, "defer")) {
  328. deferred_incoming_migration(errp);
  329. } else if (strstart(uri, "tcp:", &p)) {
  330. tcp_start_incoming_migration(p, errp);
  331. #ifdef CONFIG_RDMA
  332. } else if (strstart(uri, "rdma:", &p)) {
  333. rdma_start_incoming_migration(p, errp);
  334. #endif
  335. } else if (strstart(uri, "exec:", &p)) {
  336. exec_start_incoming_migration(p, errp);
  337. } else if (strstart(uri, "unix:", &p)) {
  338. unix_start_incoming_migration(p, errp);
  339. } else if (strstart(uri, "fd:", &p)) {
  340. fd_start_incoming_migration(p, errp);
  341. } else {
  342. error_setg(errp, "unknown migration protocol: %s", uri);
  343. }
  344. }
  345. static void process_incoming_migration_bh(void *opaque)
  346. {
  347. Error *local_err = NULL;
  348. MigrationIncomingState *mis = opaque;
  349. /* If capability late_block_activate is set:
  350. * Only fire up the block code now if we're going to restart the
  351. * VM, else 'cont' will do it.
  352. * This causes file locking to happen; so we don't want it to happen
  353. * unless we really are starting the VM.
  354. */
  355. if (!migrate_late_block_activate() ||
  356. (autostart && (!global_state_received() ||
  357. global_state_get_runstate() == RUN_STATE_RUNNING))) {
  358. /* Make sure all file formats flush their mutable metadata.
  359. * If we get an error here, just don't restart the VM yet. */
  360. bdrv_invalidate_cache_all(&local_err);
  361. if (local_err) {
  362. error_report_err(local_err);
  363. local_err = NULL;
  364. autostart = false;
  365. }
  366. }
  367. /*
  368. * This must happen after all error conditions are dealt with and
  369. * we're sure the VM is going to be running on this host.
  370. */
  371. qemu_announce_self(&mis->announce_timer, migrate_announce_params());
  372. if (multifd_load_cleanup(&local_err) != 0) {
  373. error_report_err(local_err);
  374. autostart = false;
  375. }
  376. /* If global state section was not received or we are in running
  377. state, we need to obey autostart. Any other state is set with
  378. runstate_set. */
  379. dirty_bitmap_mig_before_vm_start();
  380. if (!global_state_received() ||
  381. global_state_get_runstate() == RUN_STATE_RUNNING) {
  382. if (autostart) {
  383. vm_start();
  384. } else {
  385. runstate_set(RUN_STATE_PAUSED);
  386. }
  387. } else if (migration_incoming_colo_enabled()) {
  388. migration_incoming_disable_colo();
  389. vm_start();
  390. } else {
  391. runstate_set(global_state_get_runstate());
  392. }
  393. /*
  394. * This must happen after any state changes since as soon as an external
  395. * observer sees this event they might start to prod at the VM assuming
  396. * it's ready to use.
  397. */
  398. migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
  399. MIGRATION_STATUS_COMPLETED);
  400. qemu_bh_delete(mis->bh);
  401. migration_incoming_state_destroy();
  402. }
  403. static void process_incoming_migration_co(void *opaque)
  404. {
  405. MigrationIncomingState *mis = migration_incoming_get_current();
  406. PostcopyState ps;
  407. int ret;
  408. Error *local_err = NULL;
  409. assert(mis->from_src_file);
  410. mis->migration_incoming_co = qemu_coroutine_self();
  411. mis->largest_page_size = qemu_ram_pagesize_largest();
  412. postcopy_state_set(POSTCOPY_INCOMING_NONE);
  413. migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
  414. MIGRATION_STATUS_ACTIVE);
  415. ret = qemu_loadvm_state(mis->from_src_file);
  416. ps = postcopy_state_get();
  417. trace_process_incoming_migration_co_end(ret, ps);
  418. if (ps != POSTCOPY_INCOMING_NONE) {
  419. if (ps == POSTCOPY_INCOMING_ADVISE) {
  420. /*
  421. * Where a migration had postcopy enabled (and thus went to advise)
  422. * but managed to complete within the precopy period, we can use
  423. * the normal exit.
  424. */
  425. postcopy_ram_incoming_cleanup(mis);
  426. } else if (ret >= 0) {
  427. /*
  428. * Postcopy was started, cleanup should happen at the end of the
  429. * postcopy thread.
  430. */
  431. trace_process_incoming_migration_co_postcopy_end_main();
  432. return;
  433. }
  434. /* Else if something went wrong then just fall out of the normal exit */
  435. }
  436. /* we get COLO info, and know if we are in COLO mode */
  437. if (!ret && migration_incoming_colo_enabled()) {
  438. /* Make sure all file formats flush their mutable metadata */
  439. bdrv_invalidate_cache_all(&local_err);
  440. if (local_err) {
  441. error_report_err(local_err);
  442. goto fail;
  443. }
  444. qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming",
  445. colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE);
  446. mis->have_colo_incoming_thread = true;
  447. qemu_coroutine_yield();
  448. /* Wait checkpoint incoming thread exit before free resource */
  449. qemu_thread_join(&mis->colo_incoming_thread);
  450. /* We hold the global iothread lock, so it is safe here */
  451. colo_release_ram_cache();
  452. }
  453. if (ret < 0) {
  454. error_report("load of migration failed: %s", strerror(-ret));
  455. goto fail;
  456. }
  457. mis->bh = qemu_bh_new(process_incoming_migration_bh, mis);
  458. qemu_bh_schedule(mis->bh);
  459. mis->migration_incoming_co = NULL;
  460. return;
  461. fail:
  462. local_err = NULL;
  463. migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
  464. MIGRATION_STATUS_FAILED);
  465. qemu_fclose(mis->from_src_file);
  466. if (multifd_load_cleanup(&local_err) != 0) {
  467. error_report_err(local_err);
  468. }
  469. exit(EXIT_FAILURE);
  470. }
  471. /**
  472. * @migration_incoming_setup: Setup incoming migration
  473. *
  474. * Returns 0 for no error or 1 for error
  475. *
  476. * @f: file for main migration channel
  477. * @errp: where to put errors
  478. */
  479. static int migration_incoming_setup(QEMUFile *f, Error **errp)
  480. {
  481. MigrationIncomingState *mis = migration_incoming_get_current();
  482. Error *local_err = NULL;
  483. if (multifd_load_setup(&local_err) != 0) {
  484. /* We haven't been able to create multifd threads
  485. nothing better to do */
  486. error_report_err(local_err);
  487. exit(EXIT_FAILURE);
  488. }
  489. if (!mis->from_src_file) {
  490. mis->from_src_file = f;
  491. }
  492. qemu_file_set_blocking(f, false);
  493. return 0;
  494. }
  495. void migration_incoming_process(void)
  496. {
  497. Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL);
  498. qemu_coroutine_enter(co);
  499. }
  500. /* Returns true if recovered from a paused migration, otherwise false */
  501. static bool postcopy_try_recover(QEMUFile *f)
  502. {
  503. MigrationIncomingState *mis = migration_incoming_get_current();
  504. if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
  505. /* Resumed from a paused postcopy migration */
  506. mis->from_src_file = f;
  507. /* Postcopy has standalone thread to do vm load */
  508. qemu_file_set_blocking(f, true);
  509. /* Re-configure the return path */
  510. mis->to_src_file = qemu_file_get_return_path(f);
  511. migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
  512. MIGRATION_STATUS_POSTCOPY_RECOVER);
  513. /*
  514. * Here, we only wake up the main loading thread (while the
  515. * fault thread will still be waiting), so that we can receive
  516. * commands from source now, and answer it if needed. The
  517. * fault thread will be woken up afterwards until we are sure
  518. * that source is ready to reply to page requests.
  519. */
  520. qemu_sem_post(&mis->postcopy_pause_sem_dst);
  521. return true;
  522. }
  523. return false;
  524. }
  525. void migration_fd_process_incoming(QEMUFile *f, Error **errp)
  526. {
  527. Error *local_err = NULL;
  528. if (postcopy_try_recover(f)) {
  529. return;
  530. }
  531. if (migration_incoming_setup(f, &local_err)) {
  532. if (local_err) {
  533. error_propagate(errp, local_err);
  534. }
  535. return;
  536. }
  537. migration_incoming_process();
  538. }
  539. void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp)
  540. {
  541. MigrationIncomingState *mis = migration_incoming_get_current();
  542. Error *local_err = NULL;
  543. bool start_migration;
  544. if (!mis->from_src_file) {
  545. /* The first connection (multifd may have multiple) */
  546. QEMUFile *f = qemu_fopen_channel_input(ioc);
  547. /* If it's a recovery, we're done */
  548. if (postcopy_try_recover(f)) {
  549. return;
  550. }
  551. if (migration_incoming_setup(f, &local_err)) {
  552. if (local_err) {
  553. error_propagate(errp, local_err);
  554. }
  555. return;
  556. }
  557. /*
  558. * Common migration only needs one channel, so we can start
  559. * right now. Multifd needs more than one channel, we wait.
  560. */
  561. start_migration = !migrate_use_multifd();
  562. } else {
  563. /* Multiple connections */
  564. assert(migrate_use_multifd());
  565. start_migration = multifd_recv_new_channel(ioc, &local_err);
  566. if (local_err) {
  567. error_propagate(errp, local_err);
  568. return;
  569. }
  570. }
  571. if (start_migration) {
  572. migration_incoming_process();
  573. }
  574. }
  575. /**
  576. * @migration_has_all_channels: We have received all channels that we need
  577. *
  578. * Returns true when we have got connections to all the channels that
  579. * we need for migration.
  580. */
  581. bool migration_has_all_channels(void)
  582. {
  583. MigrationIncomingState *mis = migration_incoming_get_current();
  584. bool all_channels;
  585. all_channels = multifd_recv_all_channels_created();
  586. return all_channels && mis->from_src_file != NULL;
  587. }
  588. /*
  589. * Send a 'SHUT' message on the return channel with the given value
  590. * to indicate that we've finished with the RP. Non-0 value indicates
  591. * error.
  592. */
  593. void migrate_send_rp_shut(MigrationIncomingState *mis,
  594. uint32_t value)
  595. {
  596. uint32_t buf;
  597. buf = cpu_to_be32(value);
  598. migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf);
  599. }
  600. /*
  601. * Send a 'PONG' message on the return channel with the given value
  602. * (normally in response to a 'PING')
  603. */
  604. void migrate_send_rp_pong(MigrationIncomingState *mis,
  605. uint32_t value)
  606. {
  607. uint32_t buf;
  608. buf = cpu_to_be32(value);
  609. migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf);
  610. }
  611. void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
  612. char *block_name)
  613. {
  614. char buf[512];
  615. int len;
  616. int64_t res;
  617. /*
  618. * First, we send the header part. It contains only the len of
  619. * idstr, and the idstr itself.
  620. */
  621. len = strlen(block_name);
  622. buf[0] = len;
  623. memcpy(buf + 1, block_name, len);
  624. if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
  625. error_report("%s: MSG_RP_RECV_BITMAP only used for recovery",
  626. __func__);
  627. return;
  628. }
  629. migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf);
  630. /*
  631. * Next, we dump the received bitmap to the stream.
  632. *
  633. * TODO: currently we are safe since we are the only one that is
  634. * using the to_src_file handle (fault thread is still paused),
  635. * and it's ok even not taking the mutex. However the best way is
  636. * to take the lock before sending the message header, and release
  637. * the lock after sending the bitmap.
  638. */
  639. qemu_mutex_lock(&mis->rp_mutex);
  640. res = ramblock_recv_bitmap_send(mis->to_src_file, block_name);
  641. qemu_mutex_unlock(&mis->rp_mutex);
  642. trace_migrate_send_rp_recv_bitmap(block_name, res);
  643. }
  644. void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value)
  645. {
  646. uint32_t buf;
  647. buf = cpu_to_be32(value);
  648. migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf);
  649. }
  650. MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp)
  651. {
  652. MigrationCapabilityStatusList *head = NULL;
  653. MigrationCapabilityStatusList *caps;
  654. MigrationState *s = migrate_get_current();
  655. int i;
  656. caps = NULL; /* silence compiler warning */
  657. for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
  658. #ifndef CONFIG_LIVE_BLOCK_MIGRATION
  659. if (i == MIGRATION_CAPABILITY_BLOCK) {
  660. continue;
  661. }
  662. #endif
  663. if (head == NULL) {
  664. head = g_malloc0(sizeof(*caps));
  665. caps = head;
  666. } else {
  667. caps->next = g_malloc0(sizeof(*caps));
  668. caps = caps->next;
  669. }
  670. caps->value =
  671. g_malloc(sizeof(*caps->value));
  672. caps->value->capability = i;
  673. caps->value->state = s->enabled_capabilities[i];
  674. }
  675. return head;
  676. }
  677. MigrationParameters *qmp_query_migrate_parameters(Error **errp)
  678. {
  679. MigrationParameters *params;
  680. MigrationState *s = migrate_get_current();
  681. /* TODO use QAPI_CLONE() instead of duplicating it inline */
  682. params = g_malloc0(sizeof(*params));
  683. params->has_compress_level = true;
  684. params->compress_level = s->parameters.compress_level;
  685. params->has_compress_threads = true;
  686. params->compress_threads = s->parameters.compress_threads;
  687. params->has_compress_wait_thread = true;
  688. params->compress_wait_thread = s->parameters.compress_wait_thread;
  689. params->has_decompress_threads = true;
  690. params->decompress_threads = s->parameters.decompress_threads;
  691. params->has_throttle_trigger_threshold = true;
  692. params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold;
  693. params->has_cpu_throttle_initial = true;
  694. params->cpu_throttle_initial = s->parameters.cpu_throttle_initial;
  695. params->has_cpu_throttle_increment = true;
  696. params->cpu_throttle_increment = s->parameters.cpu_throttle_increment;
  697. params->has_cpu_throttle_tailslow = true;
  698. params->cpu_throttle_tailslow = s->parameters.cpu_throttle_tailslow;
  699. params->has_tls_creds = true;
  700. params->tls_creds = g_strdup(s->parameters.tls_creds);
  701. params->has_tls_hostname = true;
  702. params->tls_hostname = g_strdup(s->parameters.tls_hostname);
  703. params->has_tls_authz = true;
  704. params->tls_authz = g_strdup(s->parameters.tls_authz ?
  705. s->parameters.tls_authz : "");
  706. params->has_max_bandwidth = true;
  707. params->max_bandwidth = s->parameters.max_bandwidth;
  708. params->has_downtime_limit = true;
  709. params->downtime_limit = s->parameters.downtime_limit;
  710. params->has_x_checkpoint_delay = true;
  711. params->x_checkpoint_delay = s->parameters.x_checkpoint_delay;
  712. params->has_block_incremental = true;
  713. params->block_incremental = s->parameters.block_incremental;
  714. params->has_multifd_channels = true;
  715. params->multifd_channels = s->parameters.multifd_channels;
  716. params->has_multifd_compression = true;
  717. params->multifd_compression = s->parameters.multifd_compression;
  718. params->has_multifd_zlib_level = true;
  719. params->multifd_zlib_level = s->parameters.multifd_zlib_level;
  720. params->has_multifd_zstd_level = true;
  721. params->multifd_zstd_level = s->parameters.multifd_zstd_level;
  722. params->has_xbzrle_cache_size = true;
  723. params->xbzrle_cache_size = s->parameters.xbzrle_cache_size;
  724. params->has_max_postcopy_bandwidth = true;
  725. params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth;
  726. params->has_max_cpu_throttle = true;
  727. params->max_cpu_throttle = s->parameters.max_cpu_throttle;
  728. params->has_announce_initial = true;
  729. params->announce_initial = s->parameters.announce_initial;
  730. params->has_announce_max = true;
  731. params->announce_max = s->parameters.announce_max;
  732. params->has_announce_rounds = true;
  733. params->announce_rounds = s->parameters.announce_rounds;
  734. params->has_announce_step = true;
  735. params->announce_step = s->parameters.announce_step;
  736. return params;
  737. }
  738. AnnounceParameters *migrate_announce_params(void)
  739. {
  740. static AnnounceParameters ap;
  741. MigrationState *s = migrate_get_current();
  742. ap.initial = s->parameters.announce_initial;
  743. ap.max = s->parameters.announce_max;
  744. ap.rounds = s->parameters.announce_rounds;
  745. ap.step = s->parameters.announce_step;
  746. return &ap;
  747. }
  748. /*
  749. * Return true if we're already in the middle of a migration
  750. * (i.e. any of the active or setup states)
  751. */
  752. bool migration_is_setup_or_active(int state)
  753. {
  754. switch (state) {
  755. case MIGRATION_STATUS_ACTIVE:
  756. case MIGRATION_STATUS_POSTCOPY_ACTIVE:
  757. case MIGRATION_STATUS_POSTCOPY_PAUSED:
  758. case MIGRATION_STATUS_POSTCOPY_RECOVER:
  759. case MIGRATION_STATUS_SETUP:
  760. case MIGRATION_STATUS_PRE_SWITCHOVER:
  761. case MIGRATION_STATUS_DEVICE:
  762. case MIGRATION_STATUS_WAIT_UNPLUG:
  763. case MIGRATION_STATUS_COLO:
  764. return true;
  765. default:
  766. return false;
  767. }
  768. }
  769. bool migration_is_running(int state)
  770. {
  771. switch (state) {
  772. case MIGRATION_STATUS_ACTIVE:
  773. case MIGRATION_STATUS_POSTCOPY_ACTIVE:
  774. case MIGRATION_STATUS_POSTCOPY_PAUSED:
  775. case MIGRATION_STATUS_POSTCOPY_RECOVER:
  776. case MIGRATION_STATUS_SETUP:
  777. case MIGRATION_STATUS_PRE_SWITCHOVER:
  778. case MIGRATION_STATUS_DEVICE:
  779. case MIGRATION_STATUS_WAIT_UNPLUG:
  780. case MIGRATION_STATUS_CANCELLING:
  781. return true;
  782. default:
  783. return false;
  784. }
  785. }
  786. static void populate_time_info(MigrationInfo *info, MigrationState *s)
  787. {
  788. info->has_status = true;
  789. info->has_setup_time = true;
  790. info->setup_time = s->setup_time;
  791. if (s->state == MIGRATION_STATUS_COMPLETED) {
  792. info->has_total_time = true;
  793. info->total_time = s->total_time;
  794. info->has_downtime = true;
  795. info->downtime = s->downtime;
  796. } else {
  797. info->has_total_time = true;
  798. info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
  799. s->start_time;
  800. info->has_expected_downtime = true;
  801. info->expected_downtime = s->expected_downtime;
  802. }
  803. }
  804. static void populate_ram_info(MigrationInfo *info, MigrationState *s)
  805. {
  806. info->has_ram = true;
  807. info->ram = g_malloc0(sizeof(*info->ram));
  808. info->ram->transferred = ram_counters.transferred;
  809. info->ram->total = ram_bytes_total();
  810. info->ram->duplicate = ram_counters.duplicate;
  811. /* legacy value. It is not used anymore */
  812. info->ram->skipped = 0;
  813. info->ram->normal = ram_counters.normal;
  814. info->ram->normal_bytes = ram_counters.normal *
  815. qemu_target_page_size();
  816. info->ram->mbps = s->mbps;
  817. info->ram->dirty_sync_count = ram_counters.dirty_sync_count;
  818. info->ram->postcopy_requests = ram_counters.postcopy_requests;
  819. info->ram->page_size = qemu_target_page_size();
  820. info->ram->multifd_bytes = ram_counters.multifd_bytes;
  821. info->ram->pages_per_second = s->pages_per_second;
  822. if (migrate_use_xbzrle()) {
  823. info->has_xbzrle_cache = true;
  824. info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache));
  825. info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size();
  826. info->xbzrle_cache->bytes = xbzrle_counters.bytes;
  827. info->xbzrle_cache->pages = xbzrle_counters.pages;
  828. info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss;
  829. info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate;
  830. info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate;
  831. info->xbzrle_cache->overflow = xbzrle_counters.overflow;
  832. }
  833. if (migrate_use_compression()) {
  834. info->has_compression = true;
  835. info->compression = g_malloc0(sizeof(*info->compression));
  836. info->compression->pages = compression_counters.pages;
  837. info->compression->busy = compression_counters.busy;
  838. info->compression->busy_rate = compression_counters.busy_rate;
  839. info->compression->compressed_size =
  840. compression_counters.compressed_size;
  841. info->compression->compression_rate =
  842. compression_counters.compression_rate;
  843. }
  844. if (cpu_throttle_active()) {
  845. info->has_cpu_throttle_percentage = true;
  846. info->cpu_throttle_percentage = cpu_throttle_get_percentage();
  847. }
  848. if (s->state != MIGRATION_STATUS_COMPLETED) {
  849. info->ram->remaining = ram_bytes_remaining();
  850. info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate;
  851. }
  852. }
  853. static void populate_disk_info(MigrationInfo *info)
  854. {
  855. if (blk_mig_active()) {
  856. info->has_disk = true;
  857. info->disk = g_malloc0(sizeof(*info->disk));
  858. info->disk->transferred = blk_mig_bytes_transferred();
  859. info->disk->remaining = blk_mig_bytes_remaining();
  860. info->disk->total = blk_mig_bytes_total();
  861. }
  862. }
  863. static void fill_source_migration_info(MigrationInfo *info)
  864. {
  865. MigrationState *s = migrate_get_current();
  866. switch (s->state) {
  867. case MIGRATION_STATUS_NONE:
  868. /* no migration has happened ever */
  869. /* do not overwrite destination migration status */
  870. return;
  871. case MIGRATION_STATUS_SETUP:
  872. info->has_status = true;
  873. info->has_total_time = false;
  874. break;
  875. case MIGRATION_STATUS_ACTIVE:
  876. case MIGRATION_STATUS_CANCELLING:
  877. case MIGRATION_STATUS_POSTCOPY_ACTIVE:
  878. case MIGRATION_STATUS_PRE_SWITCHOVER:
  879. case MIGRATION_STATUS_DEVICE:
  880. case MIGRATION_STATUS_POSTCOPY_PAUSED:
  881. case MIGRATION_STATUS_POSTCOPY_RECOVER:
  882. /* TODO add some postcopy stats */
  883. populate_time_info(info, s);
  884. populate_ram_info(info, s);
  885. populate_disk_info(info);
  886. break;
  887. case MIGRATION_STATUS_COLO:
  888. info->has_status = true;
  889. /* TODO: display COLO specific information (checkpoint info etc.) */
  890. break;
  891. case MIGRATION_STATUS_COMPLETED:
  892. populate_time_info(info, s);
  893. populate_ram_info(info, s);
  894. break;
  895. case MIGRATION_STATUS_FAILED:
  896. info->has_status = true;
  897. if (s->error) {
  898. info->has_error_desc = true;
  899. info->error_desc = g_strdup(error_get_pretty(s->error));
  900. }
  901. break;
  902. case MIGRATION_STATUS_CANCELLED:
  903. info->has_status = true;
  904. break;
  905. case MIGRATION_STATUS_WAIT_UNPLUG:
  906. info->has_status = true;
  907. break;
  908. }
  909. info->status = s->state;
  910. }
  911. /**
  912. * @migration_caps_check - check capability validity
  913. *
  914. * @cap_list: old capability list, array of bool
  915. * @params: new capabilities to be applied soon
  916. * @errp: set *errp if the check failed, with reason
  917. *
  918. * Returns true if check passed, otherwise false.
  919. */
  920. static bool migrate_caps_check(bool *cap_list,
  921. MigrationCapabilityStatusList *params,
  922. Error **errp)
  923. {
  924. MigrationCapabilityStatusList *cap;
  925. bool old_postcopy_cap;
  926. MigrationIncomingState *mis = migration_incoming_get_current();
  927. old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM];
  928. for (cap = params; cap; cap = cap->next) {
  929. cap_list[cap->value->capability] = cap->value->state;
  930. }
  931. #ifndef CONFIG_LIVE_BLOCK_MIGRATION
  932. if (cap_list[MIGRATION_CAPABILITY_BLOCK]) {
  933. error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) "
  934. "block migration");
  935. error_append_hint(errp, "Use drive_mirror+NBD instead.\n");
  936. return false;
  937. }
  938. #endif
  939. #ifndef CONFIG_REPLICATION
  940. if (cap_list[MIGRATION_CAPABILITY_X_COLO]) {
  941. error_setg(errp, "QEMU compiled without replication module"
  942. " can't enable COLO");
  943. error_append_hint(errp, "Please enable replication before COLO.\n");
  944. return false;
  945. }
  946. #endif
  947. if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) {
  948. /* This check is reasonably expensive, so only when it's being
  949. * set the first time, also it's only the destination that needs
  950. * special support.
  951. */
  952. if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) &&
  953. !postcopy_ram_supported_by_host(mis)) {
  954. /* postcopy_ram_supported_by_host will have emitted a more
  955. * detailed message
  956. */
  957. error_setg(errp, "Postcopy is not supported");
  958. return false;
  959. }
  960. if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) {
  961. error_setg(errp, "Postcopy is not compatible with ignore-shared");
  962. return false;
  963. }
  964. }
  965. return true;
  966. }
  967. static void fill_destination_migration_info(MigrationInfo *info)
  968. {
  969. MigrationIncomingState *mis = migration_incoming_get_current();
  970. if (mis->socket_address_list) {
  971. info->has_socket_address = true;
  972. info->socket_address =
  973. QAPI_CLONE(SocketAddressList, mis->socket_address_list);
  974. }
  975. switch (mis->state) {
  976. case MIGRATION_STATUS_NONE:
  977. return;
  978. case MIGRATION_STATUS_SETUP:
  979. case MIGRATION_STATUS_CANCELLING:
  980. case MIGRATION_STATUS_CANCELLED:
  981. case MIGRATION_STATUS_ACTIVE:
  982. case MIGRATION_STATUS_POSTCOPY_ACTIVE:
  983. case MIGRATION_STATUS_POSTCOPY_PAUSED:
  984. case MIGRATION_STATUS_POSTCOPY_RECOVER:
  985. case MIGRATION_STATUS_FAILED:
  986. case MIGRATION_STATUS_COLO:
  987. info->has_status = true;
  988. break;
  989. case MIGRATION_STATUS_COMPLETED:
  990. info->has_status = true;
  991. fill_destination_postcopy_migration_info(info);
  992. break;
  993. }
  994. info->status = mis->state;
  995. }
  996. MigrationInfo *qmp_query_migrate(Error **errp)
  997. {
  998. MigrationInfo *info = g_malloc0(sizeof(*info));
  999. fill_destination_migration_info(info);
  1000. fill_source_migration_info(info);
  1001. return info;
  1002. }
  1003. void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
  1004. Error **errp)
  1005. {
  1006. MigrationState *s = migrate_get_current();
  1007. MigrationCapabilityStatusList *cap;
  1008. bool cap_list[MIGRATION_CAPABILITY__MAX];
  1009. if (migration_is_running(s->state)) {
  1010. error_setg(errp, QERR_MIGRATION_ACTIVE);
  1011. return;
  1012. }
  1013. memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list));
  1014. if (!migrate_caps_check(cap_list, params, errp)) {
  1015. return;
  1016. }
  1017. for (cap = params; cap; cap = cap->next) {
  1018. s->enabled_capabilities[cap->value->capability] = cap->value->state;
  1019. }
  1020. }
  1021. /*
  1022. * Check whether the parameters are valid. Error will be put into errp
  1023. * (if provided). Return true if valid, otherwise false.
  1024. */
  1025. static bool migrate_params_check(MigrationParameters *params, Error **errp)
  1026. {
  1027. if (params->has_compress_level &&
  1028. (params->compress_level > 9)) {
  1029. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level",
  1030. "is invalid, it should be in the range of 0 to 9");
  1031. return false;
  1032. }
  1033. if (params->has_compress_threads && (params->compress_threads < 1)) {
  1034. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1035. "compress_threads",
  1036. "is invalid, it should be in the range of 1 to 255");
  1037. return false;
  1038. }
  1039. if (params->has_decompress_threads && (params->decompress_threads < 1)) {
  1040. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1041. "decompress_threads",
  1042. "is invalid, it should be in the range of 1 to 255");
  1043. return false;
  1044. }
  1045. if (params->has_throttle_trigger_threshold &&
  1046. (params->throttle_trigger_threshold < 1 ||
  1047. params->throttle_trigger_threshold > 100)) {
  1048. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1049. "throttle_trigger_threshold",
  1050. "an integer in the range of 1 to 100");
  1051. return false;
  1052. }
  1053. if (params->has_cpu_throttle_initial &&
  1054. (params->cpu_throttle_initial < 1 ||
  1055. params->cpu_throttle_initial > 99)) {
  1056. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1057. "cpu_throttle_initial",
  1058. "an integer in the range of 1 to 99");
  1059. return false;
  1060. }
  1061. if (params->has_cpu_throttle_increment &&
  1062. (params->cpu_throttle_increment < 1 ||
  1063. params->cpu_throttle_increment > 99)) {
  1064. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1065. "cpu_throttle_increment",
  1066. "an integer in the range of 1 to 99");
  1067. return false;
  1068. }
  1069. if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) {
  1070. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1071. "max_bandwidth",
  1072. "an integer in the range of 0 to "stringify(SIZE_MAX)
  1073. " bytes/second");
  1074. return false;
  1075. }
  1076. if (params->has_downtime_limit &&
  1077. (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) {
  1078. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1079. "downtime_limit",
  1080. "an integer in the range of 0 to "
  1081. stringify(MAX_MIGRATE_DOWNTIME)" ms");
  1082. return false;
  1083. }
  1084. /* x_checkpoint_delay is now always positive */
  1085. if (params->has_multifd_channels && (params->multifd_channels < 1)) {
  1086. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1087. "multifd_channels",
  1088. "is invalid, it should be in the range of 1 to 255");
  1089. return false;
  1090. }
  1091. if (params->has_multifd_zlib_level &&
  1092. (params->multifd_zlib_level > 9)) {
  1093. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level",
  1094. "is invalid, it should be in the range of 0 to 9");
  1095. return false;
  1096. }
  1097. if (params->has_multifd_zstd_level &&
  1098. (params->multifd_zstd_level > 20)) {
  1099. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level",
  1100. "is invalid, it should be in the range of 0 to 20");
  1101. return false;
  1102. }
  1103. if (params->has_xbzrle_cache_size &&
  1104. (params->xbzrle_cache_size < qemu_target_page_size() ||
  1105. !is_power_of_2(params->xbzrle_cache_size))) {
  1106. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1107. "xbzrle_cache_size",
  1108. "is invalid, it should be bigger than target page size"
  1109. " and a power of 2");
  1110. return false;
  1111. }
  1112. if (params->has_max_cpu_throttle &&
  1113. (params->max_cpu_throttle < params->cpu_throttle_initial ||
  1114. params->max_cpu_throttle > 99)) {
  1115. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1116. "max_cpu_throttle",
  1117. "an integer in the range of cpu_throttle_initial to 99");
  1118. return false;
  1119. }
  1120. if (params->has_announce_initial &&
  1121. params->announce_initial > 100000) {
  1122. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1123. "announce_initial",
  1124. "is invalid, it must be less than 100000 ms");
  1125. return false;
  1126. }
  1127. if (params->has_announce_max &&
  1128. params->announce_max > 100000) {
  1129. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1130. "announce_max",
  1131. "is invalid, it must be less than 100000 ms");
  1132. return false;
  1133. }
  1134. if (params->has_announce_rounds &&
  1135. params->announce_rounds > 1000) {
  1136. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1137. "announce_rounds",
  1138. "is invalid, it must be in the range of 0 to 1000");
  1139. return false;
  1140. }
  1141. if (params->has_announce_step &&
  1142. (params->announce_step < 1 ||
  1143. params->announce_step > 10000)) {
  1144. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1145. "announce_step",
  1146. "is invalid, it must be in the range of 1 to 10000 ms");
  1147. return false;
  1148. }
  1149. return true;
  1150. }
  1151. static void migrate_params_test_apply(MigrateSetParameters *params,
  1152. MigrationParameters *dest)
  1153. {
  1154. *dest = migrate_get_current()->parameters;
  1155. /* TODO use QAPI_CLONE() instead of duplicating it inline */
  1156. if (params->has_compress_level) {
  1157. dest->compress_level = params->compress_level;
  1158. }
  1159. if (params->has_compress_threads) {
  1160. dest->compress_threads = params->compress_threads;
  1161. }
  1162. if (params->has_compress_wait_thread) {
  1163. dest->compress_wait_thread = params->compress_wait_thread;
  1164. }
  1165. if (params->has_decompress_threads) {
  1166. dest->decompress_threads = params->decompress_threads;
  1167. }
  1168. if (params->has_throttle_trigger_threshold) {
  1169. dest->throttle_trigger_threshold = params->throttle_trigger_threshold;
  1170. }
  1171. if (params->has_cpu_throttle_initial) {
  1172. dest->cpu_throttle_initial = params->cpu_throttle_initial;
  1173. }
  1174. if (params->has_cpu_throttle_increment) {
  1175. dest->cpu_throttle_increment = params->cpu_throttle_increment;
  1176. }
  1177. if (params->has_cpu_throttle_tailslow) {
  1178. dest->cpu_throttle_tailslow = params->cpu_throttle_tailslow;
  1179. }
  1180. if (params->has_tls_creds) {
  1181. assert(params->tls_creds->type == QTYPE_QSTRING);
  1182. dest->tls_creds = params->tls_creds->u.s;
  1183. }
  1184. if (params->has_tls_hostname) {
  1185. assert(params->tls_hostname->type == QTYPE_QSTRING);
  1186. dest->tls_hostname = params->tls_hostname->u.s;
  1187. }
  1188. if (params->has_max_bandwidth) {
  1189. dest->max_bandwidth = params->max_bandwidth;
  1190. }
  1191. if (params->has_downtime_limit) {
  1192. dest->downtime_limit = params->downtime_limit;
  1193. }
  1194. if (params->has_x_checkpoint_delay) {
  1195. dest->x_checkpoint_delay = params->x_checkpoint_delay;
  1196. }
  1197. if (params->has_block_incremental) {
  1198. dest->block_incremental = params->block_incremental;
  1199. }
  1200. if (params->has_multifd_channels) {
  1201. dest->multifd_channels = params->multifd_channels;
  1202. }
  1203. if (params->has_multifd_compression) {
  1204. dest->multifd_compression = params->multifd_compression;
  1205. }
  1206. if (params->has_xbzrle_cache_size) {
  1207. dest->xbzrle_cache_size = params->xbzrle_cache_size;
  1208. }
  1209. if (params->has_max_postcopy_bandwidth) {
  1210. dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth;
  1211. }
  1212. if (params->has_max_cpu_throttle) {
  1213. dest->max_cpu_throttle = params->max_cpu_throttle;
  1214. }
  1215. if (params->has_announce_initial) {
  1216. dest->announce_initial = params->announce_initial;
  1217. }
  1218. if (params->has_announce_max) {
  1219. dest->announce_max = params->announce_max;
  1220. }
  1221. if (params->has_announce_rounds) {
  1222. dest->announce_rounds = params->announce_rounds;
  1223. }
  1224. if (params->has_announce_step) {
  1225. dest->announce_step = params->announce_step;
  1226. }
  1227. }
  1228. static void migrate_params_apply(MigrateSetParameters *params, Error **errp)
  1229. {
  1230. MigrationState *s = migrate_get_current();
  1231. /* TODO use QAPI_CLONE() instead of duplicating it inline */
  1232. if (params->has_compress_level) {
  1233. s->parameters.compress_level = params->compress_level;
  1234. }
  1235. if (params->has_compress_threads) {
  1236. s->parameters.compress_threads = params->compress_threads;
  1237. }
  1238. if (params->has_compress_wait_thread) {
  1239. s->parameters.compress_wait_thread = params->compress_wait_thread;
  1240. }
  1241. if (params->has_decompress_threads) {
  1242. s->parameters.decompress_threads = params->decompress_threads;
  1243. }
  1244. if (params->has_throttle_trigger_threshold) {
  1245. s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold;
  1246. }
  1247. if (params->has_cpu_throttle_initial) {
  1248. s->parameters.cpu_throttle_initial = params->cpu_throttle_initial;
  1249. }
  1250. if (params->has_cpu_throttle_increment) {
  1251. s->parameters.cpu_throttle_increment = params->cpu_throttle_increment;
  1252. }
  1253. if (params->has_cpu_throttle_tailslow) {
  1254. s->parameters.cpu_throttle_tailslow = params->cpu_throttle_tailslow;
  1255. }
  1256. if (params->has_tls_creds) {
  1257. g_free(s->parameters.tls_creds);
  1258. assert(params->tls_creds->type == QTYPE_QSTRING);
  1259. s->parameters.tls_creds = g_strdup(params->tls_creds->u.s);
  1260. }
  1261. if (params->has_tls_hostname) {
  1262. g_free(s->parameters.tls_hostname);
  1263. assert(params->tls_hostname->type == QTYPE_QSTRING);
  1264. s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s);
  1265. }
  1266. if (params->has_tls_authz) {
  1267. g_free(s->parameters.tls_authz);
  1268. assert(params->tls_authz->type == QTYPE_QSTRING);
  1269. s->parameters.tls_authz = g_strdup(params->tls_authz->u.s);
  1270. }
  1271. if (params->has_max_bandwidth) {
  1272. s->parameters.max_bandwidth = params->max_bandwidth;
  1273. if (s->to_dst_file && !migration_in_postcopy()) {
  1274. qemu_file_set_rate_limit(s->to_dst_file,
  1275. s->parameters.max_bandwidth / XFER_LIMIT_RATIO);
  1276. }
  1277. }
  1278. if (params->has_downtime_limit) {
  1279. s->parameters.downtime_limit = params->downtime_limit;
  1280. }
  1281. if (params->has_x_checkpoint_delay) {
  1282. s->parameters.x_checkpoint_delay = params->x_checkpoint_delay;
  1283. if (migration_in_colo_state()) {
  1284. colo_checkpoint_notify(s);
  1285. }
  1286. }
  1287. if (params->has_block_incremental) {
  1288. s->parameters.block_incremental = params->block_incremental;
  1289. }
  1290. if (params->has_multifd_channels) {
  1291. s->parameters.multifd_channels = params->multifd_channels;
  1292. }
  1293. if (params->has_multifd_compression) {
  1294. s->parameters.multifd_compression = params->multifd_compression;
  1295. }
  1296. if (params->has_xbzrle_cache_size) {
  1297. s->parameters.xbzrle_cache_size = params->xbzrle_cache_size;
  1298. xbzrle_cache_resize(params->xbzrle_cache_size, errp);
  1299. }
  1300. if (params->has_max_postcopy_bandwidth) {
  1301. s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth;
  1302. if (s->to_dst_file && migration_in_postcopy()) {
  1303. qemu_file_set_rate_limit(s->to_dst_file,
  1304. s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO);
  1305. }
  1306. }
  1307. if (params->has_max_cpu_throttle) {
  1308. s->parameters.max_cpu_throttle = params->max_cpu_throttle;
  1309. }
  1310. if (params->has_announce_initial) {
  1311. s->parameters.announce_initial = params->announce_initial;
  1312. }
  1313. if (params->has_announce_max) {
  1314. s->parameters.announce_max = params->announce_max;
  1315. }
  1316. if (params->has_announce_rounds) {
  1317. s->parameters.announce_rounds = params->announce_rounds;
  1318. }
  1319. if (params->has_announce_step) {
  1320. s->parameters.announce_step = params->announce_step;
  1321. }
  1322. }
  1323. void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp)
  1324. {
  1325. MigrationParameters tmp;
  1326. /* TODO Rewrite "" to null instead */
  1327. if (params->has_tls_creds
  1328. && params->tls_creds->type == QTYPE_QNULL) {
  1329. qobject_unref(params->tls_creds->u.n);
  1330. params->tls_creds->type = QTYPE_QSTRING;
  1331. params->tls_creds->u.s = strdup("");
  1332. }
  1333. /* TODO Rewrite "" to null instead */
  1334. if (params->has_tls_hostname
  1335. && params->tls_hostname->type == QTYPE_QNULL) {
  1336. qobject_unref(params->tls_hostname->u.n);
  1337. params->tls_hostname->type = QTYPE_QSTRING;
  1338. params->tls_hostname->u.s = strdup("");
  1339. }
  1340. migrate_params_test_apply(params, &tmp);
  1341. if (!migrate_params_check(&tmp, errp)) {
  1342. /* Invalid parameter */
  1343. return;
  1344. }
  1345. migrate_params_apply(params, errp);
  1346. }
  1347. void qmp_migrate_start_postcopy(Error **errp)
  1348. {
  1349. MigrationState *s = migrate_get_current();
  1350. if (!migrate_postcopy()) {
  1351. error_setg(errp, "Enable postcopy with migrate_set_capability before"
  1352. " the start of migration");
  1353. return;
  1354. }
  1355. if (s->state == MIGRATION_STATUS_NONE) {
  1356. error_setg(errp, "Postcopy must be started after migration has been"
  1357. " started");
  1358. return;
  1359. }
  1360. /*
  1361. * we don't error if migration has finished since that would be racy
  1362. * with issuing this command.
  1363. */
  1364. atomic_set(&s->start_postcopy, true);
  1365. }
  1366. /* shared migration helpers */
  1367. void migrate_set_state(int *state, int old_state, int new_state)
  1368. {
  1369. assert(new_state < MIGRATION_STATUS__MAX);
  1370. if (atomic_cmpxchg(state, old_state, new_state) == old_state) {
  1371. trace_migrate_set_state(MigrationStatus_str(new_state));
  1372. migrate_generate_event(new_state);
  1373. }
  1374. }
  1375. static MigrationCapabilityStatusList *migrate_cap_add(
  1376. MigrationCapabilityStatusList *list,
  1377. MigrationCapability index,
  1378. bool state)
  1379. {
  1380. MigrationCapabilityStatusList *cap;
  1381. cap = g_new0(MigrationCapabilityStatusList, 1);
  1382. cap->value = g_new0(MigrationCapabilityStatus, 1);
  1383. cap->value->capability = index;
  1384. cap->value->state = state;
  1385. cap->next = list;
  1386. return cap;
  1387. }
  1388. void migrate_set_block_enabled(bool value, Error **errp)
  1389. {
  1390. MigrationCapabilityStatusList *cap;
  1391. cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value);
  1392. qmp_migrate_set_capabilities(cap, errp);
  1393. qapi_free_MigrationCapabilityStatusList(cap);
  1394. }
  1395. static void migrate_set_block_incremental(MigrationState *s, bool value)
  1396. {
  1397. s->parameters.block_incremental = value;
  1398. }
  1399. static void block_cleanup_parameters(MigrationState *s)
  1400. {
  1401. if (s->must_remove_block_options) {
  1402. /* setting to false can never fail */
  1403. migrate_set_block_enabled(false, &error_abort);
  1404. migrate_set_block_incremental(s, false);
  1405. s->must_remove_block_options = false;
  1406. }
  1407. }
  1408. static void migrate_fd_cleanup(MigrationState *s)
  1409. {
  1410. qemu_bh_delete(s->cleanup_bh);
  1411. s->cleanup_bh = NULL;
  1412. qemu_savevm_state_cleanup();
  1413. if (s->to_dst_file) {
  1414. QEMUFile *tmp;
  1415. trace_migrate_fd_cleanup();
  1416. qemu_mutex_unlock_iothread();
  1417. if (s->migration_thread_running) {
  1418. qemu_thread_join(&s->thread);
  1419. s->migration_thread_running = false;
  1420. }
  1421. qemu_mutex_lock_iothread();
  1422. multifd_save_cleanup();
  1423. qemu_mutex_lock(&s->qemu_file_lock);
  1424. tmp = s->to_dst_file;
  1425. s->to_dst_file = NULL;
  1426. qemu_mutex_unlock(&s->qemu_file_lock);
  1427. /*
  1428. * Close the file handle without the lock to make sure the
  1429. * critical section won't block for long.
  1430. */
  1431. qemu_fclose(tmp);
  1432. }
  1433. assert(!migration_is_active(s));
  1434. if (s->state == MIGRATION_STATUS_CANCELLING) {
  1435. migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
  1436. MIGRATION_STATUS_CANCELLED);
  1437. }
  1438. if (s->error) {
  1439. /* It is used on info migrate. We can't free it */
  1440. error_report_err(error_copy(s->error));
  1441. }
  1442. notifier_list_notify(&migration_state_notifiers, s);
  1443. block_cleanup_parameters(s);
  1444. }
  1445. static void migrate_fd_cleanup_schedule(MigrationState *s)
  1446. {
  1447. /*
  1448. * Ref the state for bh, because it may be called when
  1449. * there're already no other refs
  1450. */
  1451. object_ref(OBJECT(s));
  1452. qemu_bh_schedule(s->cleanup_bh);
  1453. }
  1454. static void migrate_fd_cleanup_bh(void *opaque)
  1455. {
  1456. MigrationState *s = opaque;
  1457. migrate_fd_cleanup(s);
  1458. object_unref(OBJECT(s));
  1459. }
  1460. void migrate_set_error(MigrationState *s, const Error *error)
  1461. {
  1462. QEMU_LOCK_GUARD(&s->error_mutex);
  1463. if (!s->error) {
  1464. s->error = error_copy(error);
  1465. }
  1466. }
  1467. void migrate_fd_error(MigrationState *s, const Error *error)
  1468. {
  1469. trace_migrate_fd_error(error_get_pretty(error));
  1470. assert(s->to_dst_file == NULL);
  1471. migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
  1472. MIGRATION_STATUS_FAILED);
  1473. migrate_set_error(s, error);
  1474. }
  1475. static void migrate_fd_cancel(MigrationState *s)
  1476. {
  1477. int old_state ;
  1478. QEMUFile *f = migrate_get_current()->to_dst_file;
  1479. trace_migrate_fd_cancel();
  1480. if (s->rp_state.from_dst_file) {
  1481. /* shutdown the rp socket, so causing the rp thread to shutdown */
  1482. qemu_file_shutdown(s->rp_state.from_dst_file);
  1483. }
  1484. do {
  1485. old_state = s->state;
  1486. if (!migration_is_running(old_state)) {
  1487. break;
  1488. }
  1489. /* If the migration is paused, kick it out of the pause */
  1490. if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) {
  1491. qemu_sem_post(&s->pause_sem);
  1492. }
  1493. migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING);
  1494. } while (s->state != MIGRATION_STATUS_CANCELLING);
  1495. /*
  1496. * If we're unlucky the migration code might be stuck somewhere in a
  1497. * send/write while the network has failed and is waiting to timeout;
  1498. * if we've got shutdown(2) available then we can force it to quit.
  1499. * The outgoing qemu file gets closed in migrate_fd_cleanup that is
  1500. * called in a bh, so there is no race against this cancel.
  1501. */
  1502. if (s->state == MIGRATION_STATUS_CANCELLING && f) {
  1503. qemu_file_shutdown(f);
  1504. }
  1505. if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
  1506. Error *local_err = NULL;
  1507. bdrv_invalidate_cache_all(&local_err);
  1508. if (local_err) {
  1509. error_report_err(local_err);
  1510. } else {
  1511. s->block_inactive = false;
  1512. }
  1513. }
  1514. }
  1515. void add_migration_state_change_notifier(Notifier *notify)
  1516. {
  1517. notifier_list_add(&migration_state_notifiers, notify);
  1518. }
  1519. void remove_migration_state_change_notifier(Notifier *notify)
  1520. {
  1521. notifier_remove(notify);
  1522. }
  1523. bool migration_in_setup(MigrationState *s)
  1524. {
  1525. return s->state == MIGRATION_STATUS_SETUP;
  1526. }
  1527. bool migration_has_finished(MigrationState *s)
  1528. {
  1529. return s->state == MIGRATION_STATUS_COMPLETED;
  1530. }
  1531. bool migration_has_failed(MigrationState *s)
  1532. {
  1533. return (s->state == MIGRATION_STATUS_CANCELLED ||
  1534. s->state == MIGRATION_STATUS_FAILED);
  1535. }
  1536. bool migration_in_postcopy(void)
  1537. {
  1538. MigrationState *s = migrate_get_current();
  1539. switch (s->state) {
  1540. case MIGRATION_STATUS_POSTCOPY_ACTIVE:
  1541. case MIGRATION_STATUS_POSTCOPY_PAUSED:
  1542. case MIGRATION_STATUS_POSTCOPY_RECOVER:
  1543. return true;
  1544. default:
  1545. return false;
  1546. }
  1547. }
  1548. bool migration_in_postcopy_after_devices(MigrationState *s)
  1549. {
  1550. return migration_in_postcopy() && s->postcopy_after_devices;
  1551. }
  1552. bool migration_in_incoming_postcopy(void)
  1553. {
  1554. PostcopyState ps = postcopy_state_get();
  1555. return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END;
  1556. }
  1557. bool migration_is_idle(void)
  1558. {
  1559. MigrationState *s = current_migration;
  1560. if (!s) {
  1561. return true;
  1562. }
  1563. switch (s->state) {
  1564. case MIGRATION_STATUS_NONE:
  1565. case MIGRATION_STATUS_CANCELLED:
  1566. case MIGRATION_STATUS_COMPLETED:
  1567. case MIGRATION_STATUS_FAILED:
  1568. return true;
  1569. case MIGRATION_STATUS_SETUP:
  1570. case MIGRATION_STATUS_CANCELLING:
  1571. case MIGRATION_STATUS_ACTIVE:
  1572. case MIGRATION_STATUS_POSTCOPY_ACTIVE:
  1573. case MIGRATION_STATUS_COLO:
  1574. case MIGRATION_STATUS_PRE_SWITCHOVER:
  1575. case MIGRATION_STATUS_DEVICE:
  1576. case MIGRATION_STATUS_WAIT_UNPLUG:
  1577. return false;
  1578. case MIGRATION_STATUS__MAX:
  1579. g_assert_not_reached();
  1580. }
  1581. return false;
  1582. }
  1583. bool migration_is_active(MigrationState *s)
  1584. {
  1585. return (s->state == MIGRATION_STATUS_ACTIVE ||
  1586. s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
  1587. }
  1588. void migrate_init(MigrationState *s)
  1589. {
  1590. /*
  1591. * Reinitialise all migration state, except
  1592. * parameters/capabilities that the user set, and
  1593. * locks.
  1594. */
  1595. s->cleanup_bh = 0;
  1596. s->to_dst_file = NULL;
  1597. s->state = MIGRATION_STATUS_NONE;
  1598. s->rp_state.from_dst_file = NULL;
  1599. s->rp_state.error = false;
  1600. s->mbps = 0.0;
  1601. s->pages_per_second = 0.0;
  1602. s->downtime = 0;
  1603. s->expected_downtime = 0;
  1604. s->setup_time = 0;
  1605. s->start_postcopy = false;
  1606. s->postcopy_after_devices = false;
  1607. s->migration_thread_running = false;
  1608. error_free(s->error);
  1609. s->error = NULL;
  1610. migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP);
  1611. s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  1612. s->total_time = 0;
  1613. s->vm_was_running = false;
  1614. s->iteration_initial_bytes = 0;
  1615. s->threshold_size = 0;
  1616. }
  1617. static GSList *migration_blockers;
  1618. int migrate_add_blocker(Error *reason, Error **errp)
  1619. {
  1620. if (only_migratable) {
  1621. error_propagate_prepend(errp, error_copy(reason),
  1622. "disallowing migration blocker "
  1623. "(--only-migratable) for: ");
  1624. return -EACCES;
  1625. }
  1626. if (migration_is_idle()) {
  1627. migration_blockers = g_slist_prepend(migration_blockers, reason);
  1628. return 0;
  1629. }
  1630. error_propagate_prepend(errp, error_copy(reason),
  1631. "disallowing migration blocker "
  1632. "(migration in progress) for: ");
  1633. return -EBUSY;
  1634. }
  1635. void migrate_del_blocker(Error *reason)
  1636. {
  1637. migration_blockers = g_slist_remove(migration_blockers, reason);
  1638. }
  1639. void qmp_migrate_incoming(const char *uri, Error **errp)
  1640. {
  1641. Error *local_err = NULL;
  1642. static bool once = true;
  1643. if (!deferred_incoming) {
  1644. error_setg(errp, "For use with '-incoming defer'");
  1645. return;
  1646. }
  1647. if (!once) {
  1648. error_setg(errp, "The incoming migration has already been started");
  1649. return;
  1650. }
  1651. qemu_start_incoming_migration(uri, &local_err);
  1652. if (local_err) {
  1653. error_propagate(errp, local_err);
  1654. return;
  1655. }
  1656. once = false;
  1657. }
  1658. void qmp_migrate_recover(const char *uri, Error **errp)
  1659. {
  1660. MigrationIncomingState *mis = migration_incoming_get_current();
  1661. if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
  1662. error_setg(errp, "Migrate recover can only be run "
  1663. "when postcopy is paused.");
  1664. return;
  1665. }
  1666. if (atomic_cmpxchg(&mis->postcopy_recover_triggered,
  1667. false, true) == true) {
  1668. error_setg(errp, "Migrate recovery is triggered already");
  1669. return;
  1670. }
  1671. /*
  1672. * Note that this call will never start a real migration; it will
  1673. * only re-setup the migration stream and poke existing migration
  1674. * to continue using that newly established channel.
  1675. */
  1676. qemu_start_incoming_migration(uri, errp);
  1677. }
  1678. void qmp_migrate_pause(Error **errp)
  1679. {
  1680. MigrationState *ms = migrate_get_current();
  1681. MigrationIncomingState *mis = migration_incoming_get_current();
  1682. int ret;
  1683. if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
  1684. /* Source side, during postcopy */
  1685. qemu_mutex_lock(&ms->qemu_file_lock);
  1686. ret = qemu_file_shutdown(ms->to_dst_file);
  1687. qemu_mutex_unlock(&ms->qemu_file_lock);
  1688. if (ret) {
  1689. error_setg(errp, "Failed to pause source migration");
  1690. }
  1691. return;
  1692. }
  1693. if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
  1694. ret = qemu_file_shutdown(mis->from_src_file);
  1695. if (ret) {
  1696. error_setg(errp, "Failed to pause destination migration");
  1697. }
  1698. return;
  1699. }
  1700. error_setg(errp, "migrate-pause is currently only supported "
  1701. "during postcopy-active state");
  1702. }
  1703. bool migration_is_blocked(Error **errp)
  1704. {
  1705. if (qemu_savevm_state_blocked(errp)) {
  1706. return true;
  1707. }
  1708. if (migration_blockers) {
  1709. error_propagate(errp, error_copy(migration_blockers->data));
  1710. return true;
  1711. }
  1712. return false;
  1713. }
  1714. /* Returns true if continue to migrate, or false if error detected */
  1715. static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
  1716. bool resume, Error **errp)
  1717. {
  1718. Error *local_err = NULL;
  1719. if (resume) {
  1720. if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) {
  1721. error_setg(errp, "Cannot resume if there is no "
  1722. "paused migration");
  1723. return false;
  1724. }
  1725. /*
  1726. * Postcopy recovery won't work well with release-ram
  1727. * capability since release-ram will drop the page buffer as
  1728. * long as the page is put into the send buffer. So if there
  1729. * is a network failure happened, any page buffers that have
  1730. * not yet reached the destination VM but have already been
  1731. * sent from the source VM will be lost forever. Let's refuse
  1732. * the client from resuming such a postcopy migration.
  1733. * Luckily release-ram was designed to only be used when src
  1734. * and destination VMs are on the same host, so it should be
  1735. * fine.
  1736. */
  1737. if (migrate_release_ram()) {
  1738. error_setg(errp, "Postcopy recovery cannot work "
  1739. "when release-ram capability is set");
  1740. return false;
  1741. }
  1742. /* This is a resume, skip init status */
  1743. return true;
  1744. }
  1745. if (migration_is_running(s->state)) {
  1746. error_setg(errp, QERR_MIGRATION_ACTIVE);
  1747. return false;
  1748. }
  1749. if (runstate_check(RUN_STATE_INMIGRATE)) {
  1750. error_setg(errp, "Guest is waiting for an incoming migration");
  1751. return false;
  1752. }
  1753. if (migration_is_blocked(errp)) {
  1754. return false;
  1755. }
  1756. if (blk || blk_inc) {
  1757. if (migrate_use_block() || migrate_use_block_incremental()) {
  1758. error_setg(errp, "Command options are incompatible with "
  1759. "current migration capabilities");
  1760. return false;
  1761. }
  1762. migrate_set_block_enabled(true, &local_err);
  1763. if (local_err) {
  1764. error_propagate(errp, local_err);
  1765. return false;
  1766. }
  1767. s->must_remove_block_options = true;
  1768. }
  1769. if (blk_inc) {
  1770. migrate_set_block_incremental(s, true);
  1771. }
  1772. migrate_init(s);
  1773. /*
  1774. * set ram_counters memory to zero for a
  1775. * new migration
  1776. */
  1777. memset(&ram_counters, 0, sizeof(ram_counters));
  1778. return true;
  1779. }
  1780. void qmp_migrate(const char *uri, bool has_blk, bool blk,
  1781. bool has_inc, bool inc, bool has_detach, bool detach,
  1782. bool has_resume, bool resume, Error **errp)
  1783. {
  1784. Error *local_err = NULL;
  1785. MigrationState *s = migrate_get_current();
  1786. const char *p;
  1787. if (!migrate_prepare(s, has_blk && blk, has_inc && inc,
  1788. has_resume && resume, errp)) {
  1789. /* Error detected, put into errp */
  1790. return;
  1791. }
  1792. if (strstart(uri, "tcp:", &p)) {
  1793. tcp_start_outgoing_migration(s, p, &local_err);
  1794. #ifdef CONFIG_RDMA
  1795. } else if (strstart(uri, "rdma:", &p)) {
  1796. rdma_start_outgoing_migration(s, p, &local_err);
  1797. #endif
  1798. } else if (strstart(uri, "exec:", &p)) {
  1799. exec_start_outgoing_migration(s, p, &local_err);
  1800. } else if (strstart(uri, "unix:", &p)) {
  1801. unix_start_outgoing_migration(s, p, &local_err);
  1802. } else if (strstart(uri, "fd:", &p)) {
  1803. fd_start_outgoing_migration(s, p, &local_err);
  1804. } else {
  1805. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri",
  1806. "a valid migration protocol");
  1807. migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
  1808. MIGRATION_STATUS_FAILED);
  1809. block_cleanup_parameters(s);
  1810. return;
  1811. }
  1812. if (local_err) {
  1813. migrate_fd_error(s, local_err);
  1814. error_propagate(errp, local_err);
  1815. return;
  1816. }
  1817. }
  1818. void qmp_migrate_cancel(Error **errp)
  1819. {
  1820. migrate_fd_cancel(migrate_get_current());
  1821. }
  1822. void qmp_migrate_continue(MigrationStatus state, Error **errp)
  1823. {
  1824. MigrationState *s = migrate_get_current();
  1825. if (s->state != state) {
  1826. error_setg(errp, "Migration not in expected state: %s",
  1827. MigrationStatus_str(s->state));
  1828. return;
  1829. }
  1830. qemu_sem_post(&s->pause_sem);
  1831. }
  1832. void qmp_migrate_set_cache_size(int64_t value, Error **errp)
  1833. {
  1834. MigrateSetParameters p = {
  1835. .has_xbzrle_cache_size = true,
  1836. .xbzrle_cache_size = value,
  1837. };
  1838. qmp_migrate_set_parameters(&p, errp);
  1839. }
  1840. int64_t qmp_query_migrate_cache_size(Error **errp)
  1841. {
  1842. return migrate_xbzrle_cache_size();
  1843. }
  1844. void qmp_migrate_set_speed(int64_t value, Error **errp)
  1845. {
  1846. MigrateSetParameters p = {
  1847. .has_max_bandwidth = true,
  1848. .max_bandwidth = value,
  1849. };
  1850. qmp_migrate_set_parameters(&p, errp);
  1851. }
  1852. void qmp_migrate_set_downtime(double value, Error **errp)
  1853. {
  1854. if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
  1855. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  1856. "downtime_limit",
  1857. "an integer in the range of 0 to "
  1858. stringify(MAX_MIGRATE_DOWNTIME_SECONDS)" seconds");
  1859. return;
  1860. }
  1861. value *= 1000; /* Convert to milliseconds */
  1862. MigrateSetParameters p = {
  1863. .has_downtime_limit = true,
  1864. .downtime_limit = (int64_t)value,
  1865. };
  1866. qmp_migrate_set_parameters(&p, errp);
  1867. }
  1868. bool migrate_release_ram(void)
  1869. {
  1870. MigrationState *s;
  1871. s = migrate_get_current();
  1872. return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM];
  1873. }
  1874. bool migrate_postcopy_ram(void)
  1875. {
  1876. MigrationState *s;
  1877. s = migrate_get_current();
  1878. return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM];
  1879. }
  1880. bool migrate_postcopy(void)
  1881. {
  1882. return migrate_postcopy_ram() || migrate_dirty_bitmaps();
  1883. }
  1884. bool migrate_auto_converge(void)
  1885. {
  1886. MigrationState *s;
  1887. s = migrate_get_current();
  1888. return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE];
  1889. }
  1890. bool migrate_zero_blocks(void)
  1891. {
  1892. MigrationState *s;
  1893. s = migrate_get_current();
  1894. return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS];
  1895. }
  1896. bool migrate_postcopy_blocktime(void)
  1897. {
  1898. MigrationState *s;
  1899. s = migrate_get_current();
  1900. return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME];
  1901. }
  1902. bool migrate_use_compression(void)
  1903. {
  1904. MigrationState *s;
  1905. s = migrate_get_current();
  1906. return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS];
  1907. }
  1908. int migrate_compress_level(void)
  1909. {
  1910. MigrationState *s;
  1911. s = migrate_get_current();
  1912. return s->parameters.compress_level;
  1913. }
  1914. int migrate_compress_threads(void)
  1915. {
  1916. MigrationState *s;
  1917. s = migrate_get_current();
  1918. return s->parameters.compress_threads;
  1919. }
  1920. int migrate_compress_wait_thread(void)
  1921. {
  1922. MigrationState *s;
  1923. s = migrate_get_current();
  1924. return s->parameters.compress_wait_thread;
  1925. }
  1926. int migrate_decompress_threads(void)
  1927. {
  1928. MigrationState *s;
  1929. s = migrate_get_current();
  1930. return s->parameters.decompress_threads;
  1931. }
  1932. bool migrate_dirty_bitmaps(void)
  1933. {
  1934. MigrationState *s;
  1935. s = migrate_get_current();
  1936. return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS];
  1937. }
  1938. bool migrate_ignore_shared(void)
  1939. {
  1940. MigrationState *s;
  1941. s = migrate_get_current();
  1942. return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED];
  1943. }
  1944. bool migrate_validate_uuid(void)
  1945. {
  1946. MigrationState *s;
  1947. s = migrate_get_current();
  1948. return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID];
  1949. }
  1950. bool migrate_use_events(void)
  1951. {
  1952. MigrationState *s;
  1953. s = migrate_get_current();
  1954. return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS];
  1955. }
  1956. bool migrate_use_multifd(void)
  1957. {
  1958. MigrationState *s;
  1959. s = migrate_get_current();
  1960. return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
  1961. }
  1962. bool migrate_pause_before_switchover(void)
  1963. {
  1964. MigrationState *s;
  1965. s = migrate_get_current();
  1966. return s->enabled_capabilities[
  1967. MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER];
  1968. }
  1969. int migrate_multifd_channels(void)
  1970. {
  1971. MigrationState *s;
  1972. s = migrate_get_current();
  1973. return s->parameters.multifd_channels;
  1974. }
  1975. MultiFDCompression migrate_multifd_compression(void)
  1976. {
  1977. MigrationState *s;
  1978. s = migrate_get_current();
  1979. return s->parameters.multifd_compression;
  1980. }
  1981. int migrate_multifd_zlib_level(void)
  1982. {
  1983. MigrationState *s;
  1984. s = migrate_get_current();
  1985. return s->parameters.multifd_zlib_level;
  1986. }
  1987. int migrate_multifd_zstd_level(void)
  1988. {
  1989. MigrationState *s;
  1990. s = migrate_get_current();
  1991. return s->parameters.multifd_zstd_level;
  1992. }
  1993. int migrate_use_xbzrle(void)
  1994. {
  1995. MigrationState *s;
  1996. s = migrate_get_current();
  1997. return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE];
  1998. }
  1999. int64_t migrate_xbzrle_cache_size(void)
  2000. {
  2001. MigrationState *s;
  2002. s = migrate_get_current();
  2003. return s->parameters.xbzrle_cache_size;
  2004. }
  2005. static int64_t migrate_max_postcopy_bandwidth(void)
  2006. {
  2007. MigrationState *s;
  2008. s = migrate_get_current();
  2009. return s->parameters.max_postcopy_bandwidth;
  2010. }
  2011. bool migrate_use_block(void)
  2012. {
  2013. MigrationState *s;
  2014. s = migrate_get_current();
  2015. return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK];
  2016. }
  2017. bool migrate_use_return_path(void)
  2018. {
  2019. MigrationState *s;
  2020. s = migrate_get_current();
  2021. return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH];
  2022. }
  2023. bool migrate_use_block_incremental(void)
  2024. {
  2025. MigrationState *s;
  2026. s = migrate_get_current();
  2027. return s->parameters.block_incremental;
  2028. }
  2029. /* migration thread support */
  2030. /*
  2031. * Something bad happened to the RP stream, mark an error
  2032. * The caller shall print or trace something to indicate why
  2033. */
  2034. static void mark_source_rp_bad(MigrationState *s)
  2035. {
  2036. s->rp_state.error = true;
  2037. }
  2038. static struct rp_cmd_args {
  2039. ssize_t len; /* -1 = variable */
  2040. const char *name;
  2041. } rp_cmd_args[] = {
  2042. [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" },
  2043. [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" },
  2044. [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" },
  2045. [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" },
  2046. [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" },
  2047. [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
  2048. [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" },
  2049. [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" },
  2050. };
  2051. /*
  2052. * Process a request for pages received on the return path,
  2053. * We're allowed to send more than requested (e.g. to round to our page size)
  2054. * and we don't need to send pages that have already been sent.
  2055. */
  2056. static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
  2057. ram_addr_t start, size_t len)
  2058. {
  2059. long our_host_ps = qemu_real_host_page_size;
  2060. trace_migrate_handle_rp_req_pages(rbname, start, len);
  2061. /*
  2062. * Since we currently insist on matching page sizes, just sanity check
  2063. * we're being asked for whole host pages.
  2064. */
  2065. if (start & (our_host_ps-1) ||
  2066. (len & (our_host_ps-1))) {
  2067. error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT
  2068. " len: %zd", __func__, start, len);
  2069. mark_source_rp_bad(ms);
  2070. return;
  2071. }
  2072. if (ram_save_queue_pages(rbname, start, len)) {
  2073. mark_source_rp_bad(ms);
  2074. }
  2075. }
  2076. /* Return true to retry, false to quit */
  2077. static bool postcopy_pause_return_path_thread(MigrationState *s)
  2078. {
  2079. trace_postcopy_pause_return_path();
  2080. qemu_sem_wait(&s->postcopy_pause_rp_sem);
  2081. trace_postcopy_pause_return_path_continued();
  2082. return true;
  2083. }
  2084. static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
  2085. {
  2086. RAMBlock *block = qemu_ram_block_by_name(block_name);
  2087. if (!block) {
  2088. error_report("%s: invalid block name '%s'", __func__, block_name);
  2089. return -EINVAL;
  2090. }
  2091. /* Fetch the received bitmap and refresh the dirty bitmap */
  2092. return ram_dirty_bitmap_reload(s, block);
  2093. }
  2094. static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value)
  2095. {
  2096. trace_source_return_path_thread_resume_ack(value);
  2097. if (value != MIGRATION_RESUME_ACK_VALUE) {
  2098. error_report("%s: illegal resume_ack value %"PRIu32,
  2099. __func__, value);
  2100. return -1;
  2101. }
  2102. /* Now both sides are active. */
  2103. migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
  2104. MIGRATION_STATUS_POSTCOPY_ACTIVE);
  2105. /* Notify send thread that time to continue send pages */
  2106. qemu_sem_post(&s->rp_state.rp_sem);
  2107. return 0;
  2108. }
  2109. /*
  2110. * Handles messages sent on the return path towards the source VM
  2111. *
  2112. */
  2113. static void *source_return_path_thread(void *opaque)
  2114. {
  2115. MigrationState *ms = opaque;
  2116. QEMUFile *rp = ms->rp_state.from_dst_file;
  2117. uint16_t header_len, header_type;
  2118. uint8_t buf[512];
  2119. uint32_t tmp32, sibling_error;
  2120. ram_addr_t start = 0; /* =0 to silence warning */
  2121. size_t len = 0, expected_len;
  2122. int res;
  2123. trace_source_return_path_thread_entry();
  2124. rcu_register_thread();
  2125. retry:
  2126. while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
  2127. migration_is_setup_or_active(ms->state)) {
  2128. trace_source_return_path_thread_loop_top();
  2129. header_type = qemu_get_be16(rp);
  2130. header_len = qemu_get_be16(rp);
  2131. if (qemu_file_get_error(rp)) {
  2132. mark_source_rp_bad(ms);
  2133. goto out;
  2134. }
  2135. if (header_type >= MIG_RP_MSG_MAX ||
  2136. header_type == MIG_RP_MSG_INVALID) {
  2137. error_report("RP: Received invalid message 0x%04x length 0x%04x",
  2138. header_type, header_len);
  2139. mark_source_rp_bad(ms);
  2140. goto out;
  2141. }
  2142. if ((rp_cmd_args[header_type].len != -1 &&
  2143. header_len != rp_cmd_args[header_type].len) ||
  2144. header_len > sizeof(buf)) {
  2145. error_report("RP: Received '%s' message (0x%04x) with"
  2146. "incorrect length %d expecting %zu",
  2147. rp_cmd_args[header_type].name, header_type, header_len,
  2148. (size_t)rp_cmd_args[header_type].len);
  2149. mark_source_rp_bad(ms);
  2150. goto out;
  2151. }
  2152. /* We know we've got a valid header by this point */
  2153. res = qemu_get_buffer(rp, buf, header_len);
  2154. if (res != header_len) {
  2155. error_report("RP: Failed reading data for message 0x%04x"
  2156. " read %d expected %d",
  2157. header_type, res, header_len);
  2158. mark_source_rp_bad(ms);
  2159. goto out;
  2160. }
  2161. /* OK, we have the message and the data */
  2162. switch (header_type) {
  2163. case MIG_RP_MSG_SHUT:
  2164. sibling_error = ldl_be_p(buf);
  2165. trace_source_return_path_thread_shut(sibling_error);
  2166. if (sibling_error) {
  2167. error_report("RP: Sibling indicated error %d", sibling_error);
  2168. mark_source_rp_bad(ms);
  2169. }
  2170. /*
  2171. * We'll let the main thread deal with closing the RP
  2172. * we could do a shutdown(2) on it, but we're the only user
  2173. * anyway, so there's nothing gained.
  2174. */
  2175. goto out;
  2176. case MIG_RP_MSG_PONG:
  2177. tmp32 = ldl_be_p(buf);
  2178. trace_source_return_path_thread_pong(tmp32);
  2179. break;
  2180. case MIG_RP_MSG_REQ_PAGES:
  2181. start = ldq_be_p(buf);
  2182. len = ldl_be_p(buf + 8);
  2183. migrate_handle_rp_req_pages(ms, NULL, start, len);
  2184. break;
  2185. case MIG_RP_MSG_REQ_PAGES_ID:
  2186. expected_len = 12 + 1; /* header + termination */
  2187. if (header_len >= expected_len) {
  2188. start = ldq_be_p(buf);
  2189. len = ldl_be_p(buf + 8);
  2190. /* Now we expect an idstr */
  2191. tmp32 = buf[12]; /* Length of the following idstr */
  2192. buf[13 + tmp32] = '\0';
  2193. expected_len += tmp32;
  2194. }
  2195. if (header_len != expected_len) {
  2196. error_report("RP: Req_Page_id with length %d expecting %zd",
  2197. header_len, expected_len);
  2198. mark_source_rp_bad(ms);
  2199. goto out;
  2200. }
  2201. migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len);
  2202. break;
  2203. case MIG_RP_MSG_RECV_BITMAP:
  2204. if (header_len < 1) {
  2205. error_report("%s: missing block name", __func__);
  2206. mark_source_rp_bad(ms);
  2207. goto out;
  2208. }
  2209. /* Format: len (1B) + idstr (<255B). This ends the idstr. */
  2210. buf[buf[0] + 1] = '\0';
  2211. if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) {
  2212. mark_source_rp_bad(ms);
  2213. goto out;
  2214. }
  2215. break;
  2216. case MIG_RP_MSG_RESUME_ACK:
  2217. tmp32 = ldl_be_p(buf);
  2218. if (migrate_handle_rp_resume_ack(ms, tmp32)) {
  2219. mark_source_rp_bad(ms);
  2220. goto out;
  2221. }
  2222. break;
  2223. default:
  2224. break;
  2225. }
  2226. }
  2227. out:
  2228. res = qemu_file_get_error(rp);
  2229. if (res) {
  2230. if (res == -EIO && migration_in_postcopy()) {
  2231. /*
  2232. * Maybe there is something we can do: it looks like a
  2233. * network down issue, and we pause for a recovery.
  2234. */
  2235. if (postcopy_pause_return_path_thread(ms)) {
  2236. /* Reload rp, reset the rest */
  2237. if (rp != ms->rp_state.from_dst_file) {
  2238. qemu_fclose(rp);
  2239. rp = ms->rp_state.from_dst_file;
  2240. }
  2241. ms->rp_state.error = false;
  2242. goto retry;
  2243. }
  2244. }
  2245. trace_source_return_path_thread_bad_end();
  2246. mark_source_rp_bad(ms);
  2247. }
  2248. trace_source_return_path_thread_end();
  2249. ms->rp_state.from_dst_file = NULL;
  2250. qemu_fclose(rp);
  2251. rcu_unregister_thread();
  2252. return NULL;
  2253. }
  2254. static int open_return_path_on_source(MigrationState *ms,
  2255. bool create_thread)
  2256. {
  2257. ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
  2258. if (!ms->rp_state.from_dst_file) {
  2259. return -1;
  2260. }
  2261. trace_open_return_path_on_source();
  2262. if (!create_thread) {
  2263. /* We're done */
  2264. return 0;
  2265. }
  2266. qemu_thread_create(&ms->rp_state.rp_thread, "return path",
  2267. source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
  2268. trace_open_return_path_on_source_continue();
  2269. return 0;
  2270. }
  2271. /* Returns 0 if the RP was ok, otherwise there was an error on the RP */
  2272. static int await_return_path_close_on_source(MigrationState *ms)
  2273. {
  2274. /*
  2275. * If this is a normal exit then the destination will send a SHUT and the
  2276. * rp_thread will exit, however if there's an error we need to cause
  2277. * it to exit.
  2278. */
  2279. if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
  2280. /*
  2281. * shutdown(2), if we have it, will cause it to unblock if it's stuck
  2282. * waiting for the destination.
  2283. */
  2284. qemu_file_shutdown(ms->rp_state.from_dst_file);
  2285. mark_source_rp_bad(ms);
  2286. }
  2287. trace_await_return_path_close_on_source_joining();
  2288. qemu_thread_join(&ms->rp_state.rp_thread);
  2289. trace_await_return_path_close_on_source_close();
  2290. return ms->rp_state.error;
  2291. }
  2292. /*
  2293. * Switch from normal iteration to postcopy
  2294. * Returns non-0 on error
  2295. */
  2296. static int postcopy_start(MigrationState *ms)
  2297. {
  2298. int ret;
  2299. QIOChannelBuffer *bioc;
  2300. QEMUFile *fb;
  2301. int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  2302. int64_t bandwidth = migrate_max_postcopy_bandwidth();
  2303. bool restart_block = false;
  2304. int cur_state = MIGRATION_STATUS_ACTIVE;
  2305. if (!migrate_pause_before_switchover()) {
  2306. migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE,
  2307. MIGRATION_STATUS_POSTCOPY_ACTIVE);
  2308. }
  2309. trace_postcopy_start();
  2310. qemu_mutex_lock_iothread();
  2311. trace_postcopy_start_set_run();
  2312. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
  2313. global_state_store();
  2314. ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
  2315. if (ret < 0) {
  2316. goto fail;
  2317. }
  2318. ret = migration_maybe_pause(ms, &cur_state,
  2319. MIGRATION_STATUS_POSTCOPY_ACTIVE);
  2320. if (ret < 0) {
  2321. goto fail;
  2322. }
  2323. ret = bdrv_inactivate_all();
  2324. if (ret < 0) {
  2325. goto fail;
  2326. }
  2327. restart_block = true;
  2328. /*
  2329. * Cause any non-postcopiable, but iterative devices to
  2330. * send out their final data.
  2331. */
  2332. qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false);
  2333. /*
  2334. * in Finish migrate and with the io-lock held everything should
  2335. * be quiet, but we've potentially still got dirty pages and we
  2336. * need to tell the destination to throw any pages it's already received
  2337. * that are dirty
  2338. */
  2339. if (migrate_postcopy_ram()) {
  2340. if (ram_postcopy_send_discard_bitmap(ms)) {
  2341. error_report("postcopy send discard bitmap failed");
  2342. goto fail;
  2343. }
  2344. }
  2345. /*
  2346. * send rest of state - note things that are doing postcopy
  2347. * will notice we're in POSTCOPY_ACTIVE and not actually
  2348. * wrap their state up here
  2349. */
  2350. /* 0 max-postcopy-bandwidth means unlimited */
  2351. if (!bandwidth) {
  2352. qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX);
  2353. } else {
  2354. qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO);
  2355. }
  2356. if (migrate_postcopy_ram()) {
  2357. /* Ping just for debugging, helps line traces up */
  2358. qemu_savevm_send_ping(ms->to_dst_file, 2);
  2359. }
  2360. /*
  2361. * While loading the device state we may trigger page transfer
  2362. * requests and the fd must be free to process those, and thus
  2363. * the destination must read the whole device state off the fd before
  2364. * it starts processing it. Unfortunately the ad-hoc migration format
  2365. * doesn't allow the destination to know the size to read without fully
  2366. * parsing it through each devices load-state code (especially the open
  2367. * coded devices that use get/put).
  2368. * So we wrap the device state up in a package with a length at the start;
  2369. * to do this we use a qemu_buf to hold the whole of the device state.
  2370. */
  2371. bioc = qio_channel_buffer_new(4096);
  2372. qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer");
  2373. fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
  2374. object_unref(OBJECT(bioc));
  2375. /*
  2376. * Make sure the receiver can get incoming pages before we send the rest
  2377. * of the state
  2378. */
  2379. qemu_savevm_send_postcopy_listen(fb);
  2380. qemu_savevm_state_complete_precopy(fb, false, false);
  2381. if (migrate_postcopy_ram()) {
  2382. qemu_savevm_send_ping(fb, 3);
  2383. }
  2384. qemu_savevm_send_postcopy_run(fb);
  2385. /* <><> end of stuff going into the package */
  2386. /* Last point of recovery; as soon as we send the package the destination
  2387. * can open devices and potentially start running.
  2388. * Lets just check again we've not got any errors.
  2389. */
  2390. ret = qemu_file_get_error(ms->to_dst_file);
  2391. if (ret) {
  2392. error_report("postcopy_start: Migration stream errored (pre package)");
  2393. goto fail_closefb;
  2394. }
  2395. restart_block = false;
  2396. /* Now send that blob */
  2397. if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
  2398. goto fail_closefb;
  2399. }
  2400. qemu_fclose(fb);
  2401. /* Send a notify to give a chance for anything that needs to happen
  2402. * at the transition to postcopy and after the device state; in particular
  2403. * spice needs to trigger a transition now
  2404. */
  2405. ms->postcopy_after_devices = true;
  2406. notifier_list_notify(&migration_state_notifiers, ms);
  2407. ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop;
  2408. qemu_mutex_unlock_iothread();
  2409. if (migrate_postcopy_ram()) {
  2410. /*
  2411. * Although this ping is just for debug, it could potentially be
  2412. * used for getting a better measurement of downtime at the source.
  2413. */
  2414. qemu_savevm_send_ping(ms->to_dst_file, 4);
  2415. }
  2416. if (migrate_release_ram()) {
  2417. ram_postcopy_migrated_memory_release(ms);
  2418. }
  2419. ret = qemu_file_get_error(ms->to_dst_file);
  2420. if (ret) {
  2421. error_report("postcopy_start: Migration stream errored");
  2422. migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
  2423. MIGRATION_STATUS_FAILED);
  2424. }
  2425. return ret;
  2426. fail_closefb:
  2427. qemu_fclose(fb);
  2428. fail:
  2429. migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
  2430. MIGRATION_STATUS_FAILED);
  2431. if (restart_block) {
  2432. /* A failure happened early enough that we know the destination hasn't
  2433. * accessed block devices, so we're safe to recover.
  2434. */
  2435. Error *local_err = NULL;
  2436. bdrv_invalidate_cache_all(&local_err);
  2437. if (local_err) {
  2438. error_report_err(local_err);
  2439. }
  2440. }
  2441. qemu_mutex_unlock_iothread();
  2442. return -1;
  2443. }
  2444. /**
  2445. * migration_maybe_pause: Pause if required to by
  2446. * migrate_pause_before_switchover called with the iothread locked
  2447. * Returns: 0 on success
  2448. */
  2449. static int migration_maybe_pause(MigrationState *s,
  2450. int *current_active_state,
  2451. int new_state)
  2452. {
  2453. if (!migrate_pause_before_switchover()) {
  2454. return 0;
  2455. }
  2456. /* Since leaving this state is not atomic with posting the semaphore
  2457. * it's possible that someone could have issued multiple migrate_continue
  2458. * and the semaphore is incorrectly positive at this point;
  2459. * the docs say it's undefined to reinit a semaphore that's already
  2460. * init'd, so use timedwait to eat up any existing posts.
  2461. */
  2462. while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) {
  2463. /* This block intentionally left blank */
  2464. }
  2465. /*
  2466. * If the migration is cancelled when it is in the completion phase,
  2467. * the migration state is set to MIGRATION_STATUS_CANCELLING.
  2468. * So we don't need to wait a semaphore, otherwise we would always
  2469. * wait for the 'pause_sem' semaphore.
  2470. */
  2471. if (s->state != MIGRATION_STATUS_CANCELLING) {
  2472. qemu_mutex_unlock_iothread();
  2473. migrate_set_state(&s->state, *current_active_state,
  2474. MIGRATION_STATUS_PRE_SWITCHOVER);
  2475. qemu_sem_wait(&s->pause_sem);
  2476. migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER,
  2477. new_state);
  2478. *current_active_state = new_state;
  2479. qemu_mutex_lock_iothread();
  2480. }
  2481. return s->state == new_state ? 0 : -EINVAL;
  2482. }
  2483. /**
  2484. * migration_completion: Used by migration_thread when there's not much left.
  2485. * The caller 'breaks' the loop when this returns.
  2486. *
  2487. * @s: Current migration state
  2488. */
  2489. static void migration_completion(MigrationState *s)
  2490. {
  2491. int ret;
  2492. int current_active_state = s->state;
  2493. if (s->state == MIGRATION_STATUS_ACTIVE) {
  2494. qemu_mutex_lock_iothread();
  2495. s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  2496. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL);
  2497. s->vm_was_running = runstate_is_running();
  2498. ret = global_state_store();
  2499. if (!ret) {
  2500. bool inactivate = !migrate_colo_enabled();
  2501. ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
  2502. if (ret >= 0) {
  2503. ret = migration_maybe_pause(s, &current_active_state,
  2504. MIGRATION_STATUS_DEVICE);
  2505. }
  2506. if (ret >= 0) {
  2507. qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX);
  2508. ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false,
  2509. inactivate);
  2510. }
  2511. if (inactivate && ret >= 0) {
  2512. s->block_inactive = true;
  2513. }
  2514. }
  2515. qemu_mutex_unlock_iothread();
  2516. if (ret < 0) {
  2517. goto fail;
  2518. }
  2519. } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
  2520. trace_migration_completion_postcopy_end();
  2521. qemu_savevm_state_complete_postcopy(s->to_dst_file);
  2522. trace_migration_completion_postcopy_end_after_complete();
  2523. }
  2524. /*
  2525. * If rp was opened we must clean up the thread before
  2526. * cleaning everything else up (since if there are no failures
  2527. * it will wait for the destination to send it's status in
  2528. * a SHUT command).
  2529. */
  2530. if (s->rp_state.from_dst_file) {
  2531. int rp_error;
  2532. trace_migration_return_path_end_before();
  2533. rp_error = await_return_path_close_on_source(s);
  2534. trace_migration_return_path_end_after(rp_error);
  2535. if (rp_error) {
  2536. goto fail_invalidate;
  2537. }
  2538. }
  2539. if (qemu_file_get_error(s->to_dst_file)) {
  2540. trace_migration_completion_file_err();
  2541. goto fail_invalidate;
  2542. }
  2543. if (!migrate_colo_enabled()) {
  2544. migrate_set_state(&s->state, current_active_state,
  2545. MIGRATION_STATUS_COMPLETED);
  2546. }
  2547. return;
  2548. fail_invalidate:
  2549. /* If not doing postcopy, vm_start() will be called: let's regain
  2550. * control on images.
  2551. */
  2552. if (s->state == MIGRATION_STATUS_ACTIVE ||
  2553. s->state == MIGRATION_STATUS_DEVICE) {
  2554. Error *local_err = NULL;
  2555. qemu_mutex_lock_iothread();
  2556. bdrv_invalidate_cache_all(&local_err);
  2557. if (local_err) {
  2558. error_report_err(local_err);
  2559. } else {
  2560. s->block_inactive = false;
  2561. }
  2562. qemu_mutex_unlock_iothread();
  2563. }
  2564. fail:
  2565. migrate_set_state(&s->state, current_active_state,
  2566. MIGRATION_STATUS_FAILED);
  2567. }
  2568. bool migrate_colo_enabled(void)
  2569. {
  2570. MigrationState *s = migrate_get_current();
  2571. return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO];
  2572. }
  2573. typedef enum MigThrError {
  2574. /* No error detected */
  2575. MIG_THR_ERR_NONE = 0,
  2576. /* Detected error, but resumed successfully */
  2577. MIG_THR_ERR_RECOVERED = 1,
  2578. /* Detected fatal error, need to exit */
  2579. MIG_THR_ERR_FATAL = 2,
  2580. } MigThrError;
  2581. static int postcopy_resume_handshake(MigrationState *s)
  2582. {
  2583. qemu_savevm_send_postcopy_resume(s->to_dst_file);
  2584. while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
  2585. qemu_sem_wait(&s->rp_state.rp_sem);
  2586. }
  2587. if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
  2588. return 0;
  2589. }
  2590. return -1;
  2591. }
  2592. /* Return zero if success, or <0 for error */
  2593. static int postcopy_do_resume(MigrationState *s)
  2594. {
  2595. int ret;
  2596. /*
  2597. * Call all the resume_prepare() hooks, so that modules can be
  2598. * ready for the migration resume.
  2599. */
  2600. ret = qemu_savevm_state_resume_prepare(s);
  2601. if (ret) {
  2602. error_report("%s: resume_prepare() failure detected: %d",
  2603. __func__, ret);
  2604. return ret;
  2605. }
  2606. /*
  2607. * Last handshake with destination on the resume (destination will
  2608. * switch to postcopy-active afterwards)
  2609. */
  2610. ret = postcopy_resume_handshake(s);
  2611. if (ret) {
  2612. error_report("%s: handshake failed: %d", __func__, ret);
  2613. return ret;
  2614. }
  2615. return 0;
  2616. }
  2617. /*
  2618. * We don't return until we are in a safe state to continue current
  2619. * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or
  2620. * MIG_THR_ERR_FATAL if unrecovery failure happened.
  2621. */
  2622. static MigThrError postcopy_pause(MigrationState *s)
  2623. {
  2624. assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE);
  2625. while (true) {
  2626. QEMUFile *file;
  2627. migrate_set_state(&s->state, s->state,
  2628. MIGRATION_STATUS_POSTCOPY_PAUSED);
  2629. /* Current channel is possibly broken. Release it. */
  2630. assert(s->to_dst_file);
  2631. qemu_mutex_lock(&s->qemu_file_lock);
  2632. file = s->to_dst_file;
  2633. s->to_dst_file = NULL;
  2634. qemu_mutex_unlock(&s->qemu_file_lock);
  2635. qemu_file_shutdown(file);
  2636. qemu_fclose(file);
  2637. error_report("Detected IO failure for postcopy. "
  2638. "Migration paused.");
  2639. /*
  2640. * We wait until things fixed up. Then someone will setup the
  2641. * status back for us.
  2642. */
  2643. while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
  2644. qemu_sem_wait(&s->postcopy_pause_sem);
  2645. }
  2646. if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
  2647. /* Woken up by a recover procedure. Give it a shot */
  2648. /*
  2649. * Firstly, let's wake up the return path now, with a new
  2650. * return path channel.
  2651. */
  2652. qemu_sem_post(&s->postcopy_pause_rp_sem);
  2653. /* Do the resume logic */
  2654. if (postcopy_do_resume(s) == 0) {
  2655. /* Let's continue! */
  2656. trace_postcopy_pause_continued();
  2657. return MIG_THR_ERR_RECOVERED;
  2658. } else {
  2659. /*
  2660. * Something wrong happened during the recovery, let's
  2661. * pause again. Pause is always better than throwing
  2662. * data away.
  2663. */
  2664. continue;
  2665. }
  2666. } else {
  2667. /* This is not right... Time to quit. */
  2668. return MIG_THR_ERR_FATAL;
  2669. }
  2670. }
  2671. }
  2672. static MigThrError migration_detect_error(MigrationState *s)
  2673. {
  2674. int ret;
  2675. int state = s->state;
  2676. Error *local_error = NULL;
  2677. if (state == MIGRATION_STATUS_CANCELLING ||
  2678. state == MIGRATION_STATUS_CANCELLED) {
  2679. /* End the migration, but don't set the state to failed */
  2680. return MIG_THR_ERR_FATAL;
  2681. }
  2682. /* Try to detect any file errors */
  2683. ret = qemu_file_get_error_obj(s->to_dst_file, &local_error);
  2684. if (!ret) {
  2685. /* Everything is fine */
  2686. assert(!local_error);
  2687. return MIG_THR_ERR_NONE;
  2688. }
  2689. if (local_error) {
  2690. migrate_set_error(s, local_error);
  2691. error_free(local_error);
  2692. }
  2693. if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) {
  2694. /*
  2695. * For postcopy, we allow the network to be down for a
  2696. * while. After that, it can be continued by a
  2697. * recovery phase.
  2698. */
  2699. return postcopy_pause(s);
  2700. } else {
  2701. /*
  2702. * For precopy (or postcopy with error outside IO), we fail
  2703. * with no time.
  2704. */
  2705. migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED);
  2706. trace_migration_thread_file_err();
  2707. /* Time to stop the migration, now. */
  2708. return MIG_THR_ERR_FATAL;
  2709. }
  2710. }
  2711. /* How many bytes have we transferred since the beginning of the migration */
  2712. static uint64_t migration_total_bytes(MigrationState *s)
  2713. {
  2714. return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes;
  2715. }
  2716. static void migration_calculate_complete(MigrationState *s)
  2717. {
  2718. uint64_t bytes = migration_total_bytes(s);
  2719. int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  2720. int64_t transfer_time;
  2721. s->total_time = end_time - s->start_time;
  2722. if (!s->downtime) {
  2723. /*
  2724. * It's still not set, so we are precopy migration. For
  2725. * postcopy, downtime is calculated during postcopy_start().
  2726. */
  2727. s->downtime = end_time - s->downtime_start;
  2728. }
  2729. transfer_time = s->total_time - s->setup_time;
  2730. if (transfer_time) {
  2731. s->mbps = ((double) bytes * 8.0) / transfer_time / 1000;
  2732. }
  2733. }
  2734. static void update_iteration_initial_status(MigrationState *s)
  2735. {
  2736. /*
  2737. * Update these three fields at the same time to avoid mismatch info lead
  2738. * wrong speed calculation.
  2739. */
  2740. s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  2741. s->iteration_initial_bytes = migration_total_bytes(s);
  2742. s->iteration_initial_pages = ram_get_total_transferred_pages();
  2743. }
  2744. static void migration_update_counters(MigrationState *s,
  2745. int64_t current_time)
  2746. {
  2747. uint64_t transferred, transferred_pages, time_spent;
  2748. uint64_t current_bytes; /* bytes transferred since the beginning */
  2749. double bandwidth;
  2750. if (current_time < s->iteration_start_time + BUFFER_DELAY) {
  2751. return;
  2752. }
  2753. current_bytes = migration_total_bytes(s);
  2754. transferred = current_bytes - s->iteration_initial_bytes;
  2755. time_spent = current_time - s->iteration_start_time;
  2756. bandwidth = (double)transferred / time_spent;
  2757. s->threshold_size = bandwidth * s->parameters.downtime_limit;
  2758. s->mbps = (((double) transferred * 8.0) /
  2759. ((double) time_spent / 1000.0)) / 1000.0 / 1000.0;
  2760. transferred_pages = ram_get_total_transferred_pages() -
  2761. s->iteration_initial_pages;
  2762. s->pages_per_second = (double) transferred_pages /
  2763. (((double) time_spent / 1000.0));
  2764. /*
  2765. * if we haven't sent anything, we don't want to
  2766. * recalculate. 10000 is a small enough number for our purposes
  2767. */
  2768. if (ram_counters.dirty_pages_rate && transferred > 10000) {
  2769. s->expected_downtime = ram_counters.remaining / bandwidth;
  2770. }
  2771. qemu_file_reset_rate_limit(s->to_dst_file);
  2772. update_iteration_initial_status(s);
  2773. trace_migrate_transferred(transferred, time_spent,
  2774. bandwidth, s->threshold_size);
  2775. }
  2776. /* Migration thread iteration status */
  2777. typedef enum {
  2778. MIG_ITERATE_RESUME, /* Resume current iteration */
  2779. MIG_ITERATE_SKIP, /* Skip current iteration */
  2780. MIG_ITERATE_BREAK, /* Break the loop */
  2781. } MigIterateState;
  2782. /*
  2783. * Return true if continue to the next iteration directly, false
  2784. * otherwise.
  2785. */
  2786. static MigIterateState migration_iteration_run(MigrationState *s)
  2787. {
  2788. uint64_t pending_size, pend_pre, pend_compat, pend_post;
  2789. bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE;
  2790. qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre,
  2791. &pend_compat, &pend_post);
  2792. pending_size = pend_pre + pend_compat + pend_post;
  2793. trace_migrate_pending(pending_size, s->threshold_size,
  2794. pend_pre, pend_compat, pend_post);
  2795. if (pending_size && pending_size >= s->threshold_size) {
  2796. /* Still a significant amount to transfer */
  2797. if (!in_postcopy && pend_pre <= s->threshold_size &&
  2798. atomic_read(&s->start_postcopy)) {
  2799. if (postcopy_start(s)) {
  2800. error_report("%s: postcopy failed to start", __func__);
  2801. }
  2802. return MIG_ITERATE_SKIP;
  2803. }
  2804. /* Just another iteration step */
  2805. qemu_savevm_state_iterate(s->to_dst_file, in_postcopy);
  2806. } else {
  2807. trace_migration_thread_low_pending(pending_size);
  2808. migration_completion(s);
  2809. return MIG_ITERATE_BREAK;
  2810. }
  2811. return MIG_ITERATE_RESUME;
  2812. }
  2813. static void migration_iteration_finish(MigrationState *s)
  2814. {
  2815. /* If we enabled cpu throttling for auto-converge, turn it off. */
  2816. cpu_throttle_stop();
  2817. qemu_mutex_lock_iothread();
  2818. switch (s->state) {
  2819. case MIGRATION_STATUS_COMPLETED:
  2820. migration_calculate_complete(s);
  2821. runstate_set(RUN_STATE_POSTMIGRATE);
  2822. break;
  2823. case MIGRATION_STATUS_ACTIVE:
  2824. /*
  2825. * We should really assert here, but since it's during
  2826. * migration, let's try to reduce the usage of assertions.
  2827. */
  2828. if (!migrate_colo_enabled()) {
  2829. error_report("%s: critical error: calling COLO code without "
  2830. "COLO enabled", __func__);
  2831. }
  2832. migrate_start_colo_process(s);
  2833. /*
  2834. * Fixme: we will run VM in COLO no matter its old running state.
  2835. * After exited COLO, we will keep running.
  2836. */
  2837. s->vm_was_running = true;
  2838. /* Fallthrough */
  2839. case MIGRATION_STATUS_FAILED:
  2840. case MIGRATION_STATUS_CANCELLED:
  2841. case MIGRATION_STATUS_CANCELLING:
  2842. if (s->vm_was_running) {
  2843. vm_start();
  2844. } else {
  2845. if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
  2846. runstate_set(RUN_STATE_POSTMIGRATE);
  2847. }
  2848. }
  2849. break;
  2850. default:
  2851. /* Should not reach here, but if so, forgive the VM. */
  2852. error_report("%s: Unknown ending state %d", __func__, s->state);
  2853. break;
  2854. }
  2855. migrate_fd_cleanup_schedule(s);
  2856. qemu_mutex_unlock_iothread();
  2857. }
  2858. void migration_make_urgent_request(void)
  2859. {
  2860. qemu_sem_post(&migrate_get_current()->rate_limit_sem);
  2861. }
  2862. void migration_consume_urgent_request(void)
  2863. {
  2864. qemu_sem_wait(&migrate_get_current()->rate_limit_sem);
  2865. }
  2866. /* Returns true if the rate limiting was broken by an urgent request */
  2867. bool migration_rate_limit(void)
  2868. {
  2869. int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  2870. MigrationState *s = migrate_get_current();
  2871. bool urgent = false;
  2872. migration_update_counters(s, now);
  2873. if (qemu_file_rate_limit(s->to_dst_file)) {
  2874. if (qemu_file_get_error(s->to_dst_file)) {
  2875. return false;
  2876. }
  2877. /*
  2878. * Wait for a delay to do rate limiting OR
  2879. * something urgent to post the semaphore.
  2880. */
  2881. int ms = s->iteration_start_time + BUFFER_DELAY - now;
  2882. trace_migration_rate_limit_pre(ms);
  2883. if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) {
  2884. /*
  2885. * We were woken by one or more urgent things but
  2886. * the timedwait will have consumed one of them.
  2887. * The service routine for the urgent wake will dec
  2888. * the semaphore itself for each item it consumes,
  2889. * so add this one we just eat back.
  2890. */
  2891. qemu_sem_post(&s->rate_limit_sem);
  2892. urgent = true;
  2893. }
  2894. trace_migration_rate_limit_post(urgent);
  2895. }
  2896. return urgent;
  2897. }
  2898. /*
  2899. * Master migration thread on the source VM.
  2900. * It drives the migration and pumps the data down the outgoing channel.
  2901. */
  2902. static void *migration_thread(void *opaque)
  2903. {
  2904. MigrationState *s = opaque;
  2905. int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST);
  2906. MigThrError thr_error;
  2907. bool urgent = false;
  2908. rcu_register_thread();
  2909. object_ref(OBJECT(s));
  2910. update_iteration_initial_status(s);
  2911. qemu_savevm_state_header(s->to_dst_file);
  2912. /*
  2913. * If we opened the return path, we need to make sure dst has it
  2914. * opened as well.
  2915. */
  2916. if (s->rp_state.from_dst_file) {
  2917. /* Now tell the dest that it should open its end so it can reply */
  2918. qemu_savevm_send_open_return_path(s->to_dst_file);
  2919. /* And do a ping that will make stuff easier to debug */
  2920. qemu_savevm_send_ping(s->to_dst_file, 1);
  2921. }
  2922. if (migrate_postcopy()) {
  2923. /*
  2924. * Tell the destination that we *might* want to do postcopy later;
  2925. * if the other end can't do postcopy it should fail now, nice and
  2926. * early.
  2927. */
  2928. qemu_savevm_send_postcopy_advise(s->to_dst_file);
  2929. }
  2930. if (migrate_colo_enabled()) {
  2931. /* Notify migration destination that we enable COLO */
  2932. qemu_savevm_send_colo_enable(s->to_dst_file);
  2933. }
  2934. qemu_savevm_state_setup(s->to_dst_file);
  2935. if (qemu_savevm_state_guest_unplug_pending()) {
  2936. migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
  2937. MIGRATION_STATUS_WAIT_UNPLUG);
  2938. while (s->state == MIGRATION_STATUS_WAIT_UNPLUG &&
  2939. qemu_savevm_state_guest_unplug_pending()) {
  2940. qemu_sem_timedwait(&s->wait_unplug_sem, 250);
  2941. }
  2942. migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG,
  2943. MIGRATION_STATUS_ACTIVE);
  2944. }
  2945. s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start;
  2946. migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
  2947. MIGRATION_STATUS_ACTIVE);
  2948. trace_migration_thread_setup_complete();
  2949. while (migration_is_active(s)) {
  2950. if (urgent || !qemu_file_rate_limit(s->to_dst_file)) {
  2951. MigIterateState iter_state = migration_iteration_run(s);
  2952. if (iter_state == MIG_ITERATE_SKIP) {
  2953. continue;
  2954. } else if (iter_state == MIG_ITERATE_BREAK) {
  2955. break;
  2956. }
  2957. }
  2958. /*
  2959. * Try to detect any kind of failures, and see whether we
  2960. * should stop the migration now.
  2961. */
  2962. thr_error = migration_detect_error(s);
  2963. if (thr_error == MIG_THR_ERR_FATAL) {
  2964. /* Stop migration */
  2965. break;
  2966. } else if (thr_error == MIG_THR_ERR_RECOVERED) {
  2967. /*
  2968. * Just recovered from a e.g. network failure, reset all
  2969. * the local variables. This is important to avoid
  2970. * breaking transferred_bytes and bandwidth calculation
  2971. */
  2972. update_iteration_initial_status(s);
  2973. }
  2974. urgent = migration_rate_limit();
  2975. }
  2976. trace_migration_thread_after_loop();
  2977. migration_iteration_finish(s);
  2978. object_unref(OBJECT(s));
  2979. rcu_unregister_thread();
  2980. return NULL;
  2981. }
  2982. void migrate_fd_connect(MigrationState *s, Error *error_in)
  2983. {
  2984. Error *local_err = NULL;
  2985. int64_t rate_limit;
  2986. bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED;
  2987. s->expected_downtime = s->parameters.downtime_limit;
  2988. if (resume) {
  2989. assert(s->cleanup_bh);
  2990. } else {
  2991. assert(!s->cleanup_bh);
  2992. s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s);
  2993. }
  2994. if (error_in) {
  2995. migrate_fd_error(s, error_in);
  2996. migrate_fd_cleanup(s);
  2997. return;
  2998. }
  2999. if (resume) {
  3000. /* This is a resumed migration */
  3001. rate_limit = s->parameters.max_postcopy_bandwidth /
  3002. XFER_LIMIT_RATIO;
  3003. } else {
  3004. /* This is a fresh new migration */
  3005. rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO;
  3006. /* Notify before starting migration thread */
  3007. notifier_list_notify(&migration_state_notifiers, s);
  3008. }
  3009. qemu_file_set_rate_limit(s->to_dst_file, rate_limit);
  3010. qemu_file_set_blocking(s->to_dst_file, true);
  3011. /*
  3012. * Open the return path. For postcopy, it is used exclusively. For
  3013. * precopy, only if user specified "return-path" capability would
  3014. * QEMU uses the return path.
  3015. */
  3016. if (migrate_postcopy_ram() || migrate_use_return_path()) {
  3017. if (open_return_path_on_source(s, !resume)) {
  3018. error_report("Unable to open return-path for postcopy");
  3019. migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
  3020. migrate_fd_cleanup(s);
  3021. return;
  3022. }
  3023. }
  3024. if (resume) {
  3025. /* Wakeup the main migration thread to do the recovery */
  3026. migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED,
  3027. MIGRATION_STATUS_POSTCOPY_RECOVER);
  3028. qemu_sem_post(&s->postcopy_pause_sem);
  3029. return;
  3030. }
  3031. if (multifd_save_setup(&local_err) != 0) {
  3032. error_report_err(local_err);
  3033. migrate_set_state(&s->state, MIGRATION_STATUS_SETUP,
  3034. MIGRATION_STATUS_FAILED);
  3035. migrate_fd_cleanup(s);
  3036. return;
  3037. }
  3038. qemu_thread_create(&s->thread, "live_migration", migration_thread, s,
  3039. QEMU_THREAD_JOINABLE);
  3040. s->migration_thread_running = true;
  3041. }
  3042. void migration_global_dump(Monitor *mon)
  3043. {
  3044. MigrationState *ms = migrate_get_current();
  3045. monitor_printf(mon, "globals:\n");
  3046. monitor_printf(mon, "store-global-state: %s\n",
  3047. ms->store_global_state ? "on" : "off");
  3048. monitor_printf(mon, "only-migratable: %s\n",
  3049. only_migratable ? "on" : "off");
  3050. monitor_printf(mon, "send-configuration: %s\n",
  3051. ms->send_configuration ? "on" : "off");
  3052. monitor_printf(mon, "send-section-footer: %s\n",
  3053. ms->send_section_footer ? "on" : "off");
  3054. monitor_printf(mon, "decompress-error-check: %s\n",
  3055. ms->decompress_error_check ? "on" : "off");
  3056. monitor_printf(mon, "clear-bitmap-shift: %u\n",
  3057. ms->clear_bitmap_shift);
  3058. }
  3059. #define DEFINE_PROP_MIG_CAP(name, x) \
  3060. DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false)
  3061. static Property migration_properties[] = {
  3062. DEFINE_PROP_BOOL("store-global-state", MigrationState,
  3063. store_global_state, true),
  3064. DEFINE_PROP_BOOL("send-configuration", MigrationState,
  3065. send_configuration, true),
  3066. DEFINE_PROP_BOOL("send-section-footer", MigrationState,
  3067. send_section_footer, true),
  3068. DEFINE_PROP_BOOL("decompress-error-check", MigrationState,
  3069. decompress_error_check, true),
  3070. DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState,
  3071. clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT),
  3072. /* Migration parameters */
  3073. DEFINE_PROP_UINT8("x-compress-level", MigrationState,
  3074. parameters.compress_level,
  3075. DEFAULT_MIGRATE_COMPRESS_LEVEL),
  3076. DEFINE_PROP_UINT8("x-compress-threads", MigrationState,
  3077. parameters.compress_threads,
  3078. DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT),
  3079. DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState,
  3080. parameters.compress_wait_thread, true),
  3081. DEFINE_PROP_UINT8("x-decompress-threads", MigrationState,
  3082. parameters.decompress_threads,
  3083. DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT),
  3084. DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState,
  3085. parameters.throttle_trigger_threshold,
  3086. DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD),
  3087. DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState,
  3088. parameters.cpu_throttle_initial,
  3089. DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL),
  3090. DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState,
  3091. parameters.cpu_throttle_increment,
  3092. DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT),
  3093. DEFINE_PROP_BOOL("x-cpu-throttle-tailslow", MigrationState,
  3094. parameters.cpu_throttle_tailslow, false),
  3095. DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState,
  3096. parameters.max_bandwidth, MAX_THROTTLE),
  3097. DEFINE_PROP_UINT64("x-downtime-limit", MigrationState,
  3098. parameters.downtime_limit,
  3099. DEFAULT_MIGRATE_SET_DOWNTIME),
  3100. DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState,
  3101. parameters.x_checkpoint_delay,
  3102. DEFAULT_MIGRATE_X_CHECKPOINT_DELAY),
  3103. DEFINE_PROP_UINT8("multifd-channels", MigrationState,
  3104. parameters.multifd_channels,
  3105. DEFAULT_MIGRATE_MULTIFD_CHANNELS),
  3106. DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState,
  3107. parameters.multifd_compression,
  3108. DEFAULT_MIGRATE_MULTIFD_COMPRESSION),
  3109. DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState,
  3110. parameters.multifd_zlib_level,
  3111. DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL),
  3112. DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState,
  3113. parameters.multifd_zstd_level,
  3114. DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL),
  3115. DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState,
  3116. parameters.xbzrle_cache_size,
  3117. DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE),
  3118. DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState,
  3119. parameters.max_postcopy_bandwidth,
  3120. DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH),
  3121. DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState,
  3122. parameters.max_cpu_throttle,
  3123. DEFAULT_MIGRATE_MAX_CPU_THROTTLE),
  3124. DEFINE_PROP_SIZE("announce-initial", MigrationState,
  3125. parameters.announce_initial,
  3126. DEFAULT_MIGRATE_ANNOUNCE_INITIAL),
  3127. DEFINE_PROP_SIZE("announce-max", MigrationState,
  3128. parameters.announce_max,
  3129. DEFAULT_MIGRATE_ANNOUNCE_MAX),
  3130. DEFINE_PROP_SIZE("announce-rounds", MigrationState,
  3131. parameters.announce_rounds,
  3132. DEFAULT_MIGRATE_ANNOUNCE_ROUNDS),
  3133. DEFINE_PROP_SIZE("announce-step", MigrationState,
  3134. parameters.announce_step,
  3135. DEFAULT_MIGRATE_ANNOUNCE_STEP),
  3136. /* Migration capabilities */
  3137. DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE),
  3138. DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL),
  3139. DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE),
  3140. DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS),
  3141. DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS),
  3142. DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS),
  3143. DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM),
  3144. DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO),
  3145. DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM),
  3146. DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK),
  3147. DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
  3148. DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
  3149. DEFINE_PROP_END_OF_LIST(),
  3150. };
  3151. static void migration_class_init(ObjectClass *klass, void *data)
  3152. {
  3153. DeviceClass *dc = DEVICE_CLASS(klass);
  3154. dc->user_creatable = false;
  3155. device_class_set_props(dc, migration_properties);
  3156. }
  3157. static void migration_instance_finalize(Object *obj)
  3158. {
  3159. MigrationState *ms = MIGRATION_OBJ(obj);
  3160. MigrationParameters *params = &ms->parameters;
  3161. qemu_mutex_destroy(&ms->error_mutex);
  3162. qemu_mutex_destroy(&ms->qemu_file_lock);
  3163. g_free(params->tls_hostname);
  3164. g_free(params->tls_creds);
  3165. qemu_sem_destroy(&ms->wait_unplug_sem);
  3166. qemu_sem_destroy(&ms->rate_limit_sem);
  3167. qemu_sem_destroy(&ms->pause_sem);
  3168. qemu_sem_destroy(&ms->postcopy_pause_sem);
  3169. qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
  3170. qemu_sem_destroy(&ms->rp_state.rp_sem);
  3171. error_free(ms->error);
  3172. }
  3173. static void migration_instance_init(Object *obj)
  3174. {
  3175. MigrationState *ms = MIGRATION_OBJ(obj);
  3176. MigrationParameters *params = &ms->parameters;
  3177. ms->state = MIGRATION_STATUS_NONE;
  3178. ms->mbps = -1;
  3179. ms->pages_per_second = -1;
  3180. qemu_sem_init(&ms->pause_sem, 0);
  3181. qemu_mutex_init(&ms->error_mutex);
  3182. params->tls_hostname = g_strdup("");
  3183. params->tls_creds = g_strdup("");
  3184. /* Set has_* up only for parameter checks */
  3185. params->has_compress_level = true;
  3186. params->has_compress_threads = true;
  3187. params->has_decompress_threads = true;
  3188. params->has_throttle_trigger_threshold = true;
  3189. params->has_cpu_throttle_initial = true;
  3190. params->has_cpu_throttle_increment = true;
  3191. params->has_cpu_throttle_tailslow = true;
  3192. params->has_max_bandwidth = true;
  3193. params->has_downtime_limit = true;
  3194. params->has_x_checkpoint_delay = true;
  3195. params->has_block_incremental = true;
  3196. params->has_multifd_channels = true;
  3197. params->has_multifd_compression = true;
  3198. params->has_multifd_zlib_level = true;
  3199. params->has_multifd_zstd_level = true;
  3200. params->has_xbzrle_cache_size = true;
  3201. params->has_max_postcopy_bandwidth = true;
  3202. params->has_max_cpu_throttle = true;
  3203. params->has_announce_initial = true;
  3204. params->has_announce_max = true;
  3205. params->has_announce_rounds = true;
  3206. params->has_announce_step = true;
  3207. qemu_sem_init(&ms->postcopy_pause_sem, 0);
  3208. qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
  3209. qemu_sem_init(&ms->rp_state.rp_sem, 0);
  3210. qemu_sem_init(&ms->rate_limit_sem, 0);
  3211. qemu_sem_init(&ms->wait_unplug_sem, 0);
  3212. qemu_mutex_init(&ms->qemu_file_lock);
  3213. }
  3214. /*
  3215. * Return true if check pass, false otherwise. Error will be put
  3216. * inside errp if provided.
  3217. */
  3218. static bool migration_object_check(MigrationState *ms, Error **errp)
  3219. {
  3220. MigrationCapabilityStatusList *head = NULL;
  3221. /* Assuming all off */
  3222. bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret;
  3223. int i;
  3224. if (!migrate_params_check(&ms->parameters, errp)) {
  3225. return false;
  3226. }
  3227. for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
  3228. if (ms->enabled_capabilities[i]) {
  3229. head = migrate_cap_add(head, i, true);
  3230. }
  3231. }
  3232. ret = migrate_caps_check(cap_list, head, errp);
  3233. /* It works with head == NULL */
  3234. qapi_free_MigrationCapabilityStatusList(head);
  3235. return ret;
  3236. }
  3237. static const TypeInfo migration_type = {
  3238. .name = TYPE_MIGRATION,
  3239. /*
  3240. * NOTE: TYPE_MIGRATION is not really a device, as the object is
  3241. * not created using qdev_new(), it is not attached to the qdev
  3242. * device tree, and it is never realized.
  3243. *
  3244. * TODO: Make this TYPE_OBJECT once QOM provides something like
  3245. * TYPE_DEVICE's "-global" properties.
  3246. */
  3247. .parent = TYPE_DEVICE,
  3248. .class_init = migration_class_init,
  3249. .class_size = sizeof(MigrationClass),
  3250. .instance_size = sizeof(MigrationState),
  3251. .instance_init = migration_instance_init,
  3252. .instance_finalize = migration_instance_finalize,
  3253. };
  3254. static void register_migration_types(void)
  3255. {
  3256. type_register_static(&migration_type);
  3257. }
  3258. type_init(register_migration_types);