+/* ind means ''indirect'' that this is a reference on the whole dict_elm
+ * structure (i.e not on the surf_resource_private infos) */
+
+static void vm_ws_create(const char *name, void *ind_phys_workstation)
+{
+ workstation_CLM03_t sub_ws = surf_workstation_resource_priv(ind_phys_workstation);
+ const char *sub_ws_name = sub_ws->generic_resource.name;
+
+ /* The workstation_VM2013 struct inherits the workstation_CLM03 struct. We
+ * create a physical workstation resource, but specifying the size of
+ * s_workstation_VM2013_t and the vm workstation model object. */
+ workstation_CLM03_t ws = (workstation_CLM03_t) surf_resource_new(sizeof(s_workstation_VM2013_t),
+ surf_vm_workstation_model, name, NULL);
+
+ /* Currently, we assume a VM has no storage. */
+ ws->storage = NULL;
+
+ /* Currently, a VM uses the network resource of its physical host. In
+ * host_lib, this network resource object is refered from two different keys.
+ * When deregistering the reference that points the network resource object
+ * from the VM name, we have to make sure that the system does not call the
+ * free callback for the network resource object. The network resource object
+ * is still used by the physical machine. */
+ ws->net_elm = xbt_lib_get_or_null(host_lib, sub_ws_name, ROUTING_HOST_LEVEL);
+ xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, ws->net_elm);
+
+ /* The SURF_WKS_LEVEL at host_lib saves workstation_CLM03 objects. Please
+ * note workstation_VM2013 objects, inheriting the workstation_CLM03
+ * structure, are also saved there.
+ *
+ * If you want to get a workstation_VM2013 object from host_lib, see
+ * ws->generic_resouce.model->type first. If it is
+ * SURF_MODEL_TYPE_VM_WORKSTATION, you can cast ws to vm_ws. */
+ XBT_INFO("Create VM(%s)@PM(%s) with %ld mounted disks", name, sub_ws_name, xbt_dynar_length(ws->storage));
+ xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, ws);
+
+
+ /* We initialize the VM-specific members. */
+ workstation_VM2013_t vm_ws = (workstation_VM2013_t) ws;
+ vm_ws->sub_ws = sub_ws;
+ vm_ws->current_state = SURF_VM_STATE_CREATED;
+
+
+
+ // //// CPU RELATED STUFF ////
+ // Roughly, create a vcpu resource by using the values of the sub_cpu one.
+ cpu_Cas01_t sub_cpu = surf_cpu_resource_priv(ind_phys_workstation);
+
+ /* We can assume one core and cas01 cpu for the first step.
+ * Do xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu) if you get the resource. */
+ cpu_cas01_create_resource(name, // name
+ sub_cpu->power_peak, // host->power_peak,
+ 1, // host->power_scale,
+ NULL, // host->power_trace,
+ 1, // host->core_amount,
+ SURF_RESOURCE_ON, // host->initial_state,
+ NULL, // host->state_trace,
+ NULL, // host->properties,
+ surf_cpu_model_vm);
+
+
+
+ /* We create cpu_action corresponding to a VM process on the host operating system. */
+ /* FIXME: TODO: we have to peridocally input GUESTOS_NOISE to the system? how ? */
+ // vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, GUESTOS_NOISE);
+ vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, 0);
+
+
+ /* TODO:
+ * - check how network requests are scheduled between distinct processes competing for the same card.
+ */
+}
+
+/*
+ * Update the physical host of the given VM
+ */
+static void vm_ws_migrate(void *ind_vm, void *ind_dst_pm)
+{
+ /* ind_phys_workstation equals to smx_host_t */
+ workstation_VM2013_t ws_vm2013 = surf_workstation_resource_priv(ind_vm);
+ workstation_CLM03_t ws_clm03_dst = surf_workstation_resource_priv(ind_dst_pm);
+ const char *vm_name = ws_vm2013->ws.generic_resource.name;
+ const char *pm_name_src = ws_vm2013->sub_ws->generic_resource.name;
+ const char *pm_name_dst = ws_clm03_dst->generic_resource.name;
+
+ xbt_assert(ws_vm2013);
+ xbt_assert(ws_clm03_dst);
+
+ ws_vm2013->current_state = SURF_VM_STATE_MIGRATING;
+
+ /* do something */
+
+ /* update net_elm with that of the destination physical host */
+ void *old_net_elm = ws_vm2013->ws.net_elm;
+ void *new_net_elm = xbt_lib_get_or_null(host_lib, pm_name_dst, ROUTING_HOST_LEVEL);
+ xbt_assert(new_net_elm);
+
+ /* Unregister the current net_elm from host_lib. Do not call the free callback. */
+ xbt_lib_unset(host_lib, vm_name, ROUTING_HOST_LEVEL, 0);
+
+ /* Then, resister the new one. */
+ ws_vm2013->ws.net_elm = new_net_elm;
+ xbt_lib_set(host_lib, vm_name, ROUTING_HOST_LEVEL, ws_vm2013->ws.net_elm);
+
+ ws_vm2013->sub_ws = ws_clm03_dst;
+
+ XBT_DEBUG("migrate VM(%s): change net_elm (%p to %p)", vm_name, old_net_elm, new_net_elm);
+ XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
+
+ ws_vm2013->current_state = SURF_VM_STATE_RUNNING;
+}
+
+/*
+ * A physical host does not disapper in the current SimGrid code, but a VM may
+ * disapper during a simulation.
+ */
+static void vm_ws_destroy(void *ind_vm_workstation)
+{
+ /* ind_phys_workstation equals to smx_host_t */
+
+ /* Before clearing the entries in host_lib, we have to pick up resources. */
+ workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
+ cpu_Cas01_t cpu = surf_cpu_resource_priv(ind_vm_workstation);
+ const char *name = vm_ws->ws.generic_resource.name;
+ XBT_INFO("%s", name);
+
+ xbt_assert(vm_ws);
+ xbt_assert(vm_ws->ws.generic_resource.model == surf_vm_workstation_model);
+
+
+ /* We deregister objects from host_lib, without invoking the freeing callback
+ * of each level.
+ *
+ * Do not call xbt_lib_remove() here. It deletes all levels of the key,
+ * including MSG_HOST_LEVEL and others. We should unregister only what we know.
+ */
+ xbt_lib_unset(host_lib, name, SURF_CPU_LEVEL, 0);
+ xbt_lib_unset(host_lib, name, ROUTING_HOST_LEVEL, 0);
+ xbt_lib_unset(host_lib, name, SURF_WKS_LEVEL, 0);
+
+ /* TODO: comment out when VM stroage is implemented. */
+ // xbt_lib_unset(host_lib, name, SURF_STORAGE_LEVEL, 0);
+
+
+ /* Free the cpu_action of the VM. */
+ int ret = surf_cpu_model_pm->action_unref(vm_ws->cpu_action);
+ xbt_assert(ret == 1, "Bug: some resource still remains");
+
+ /* Free the cpu resource of the VM. If using power_trace, we will have to
+ * free other objects than lmm_constraint. */
+ surf_model_t cpu_model = cpu->generic_resource.model;
+ lmm_constraint_free(cpu_model->model_private->maxmin_system, cpu->constraint);
+ surf_resource_free(cpu);
+
+ /* Free the network resource of the VM. */
+ // Nothing has to be done, because net_elmts is just a pointer on the physical one
+
+ /* Free the storage resource of the VM. */
+ // Not relevant yet
+
+ /* Free the workstation resource of the VM. */
+ surf_resource_free(vm_ws);
+}
+
+static int vm_ws_get_state(void *ind_vm_ws)
+{
+ return ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state;
+}
+
+static void vm_ws_set_state(void *ind_vm_ws, int state)
+{
+ ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state = state;
+}
+
+static void vm_ws_suspend(void *ind_vm_ws)