Skip to content

Commit

Permalink
F OpenNebula#2427: Add support to NICs with NETWORK_MODE=auto. This c…
Browse files Browse the repository at this point in the history
…ommits includes

logic in oned to delay NIC/context resolution till deployment time. Also
scheduler has been extended to use the match-making algorithm to networks.
Sunstone interface changes to select auto networks.
Co-authored-by: juanmont <jjmontiel@opennebula.systems>
  • Loading branch information
Ruben S. Montero authored and juanmont committed Oct 16, 2018
1 parent 2bf147f commit 1a5d158
Show file tree
Hide file tree
Showing 35 changed files with 1,798 additions and 172 deletions.
8 changes: 7 additions & 1 deletion include/RankScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ class RankScheduler : public Scheduler
{
public:

RankScheduler():Scheduler(),rp_host(0),rp_ds(0),rp_vm(0){};
RankScheduler():Scheduler(),rp_host(0),rp_ds(0),rp_nics(0), rp_vm(0){};

~RankScheduler()
{
delete rp_host;
delete rp_ds;
delete rp_nics;

delete rp_vm;
};
Expand All @@ -56,11 +57,16 @@ class RankScheduler : public Scheduler
rp_vm = new UserPriorityPolicy(vmpool, 1.0);

add_vm_policy(rp_vm);

rp_nics = new RankNetworkPolicy(vnetpool, conf.get_nics_policy(), 1.0);

add_nic_policy(rp_nics);
};

private:
RankPolicy * rp_host;
RankPolicy * rp_ds;
RankPolicy * rp_nics;

UserPriorityPolicy * rp_vm;
};
4 changes: 3 additions & 1 deletion include/RequestManagerVirtualMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class RequestManagerVirtualMachine: public Request
RequestAttributes& att);

VirtualMachine * get_vm(int id, RequestAttributes& att);

VirtualMachine * get_vm_ro(int id, RequestAttributes& att);
};

/* ------------------------------------------------------------------------- */
Expand Down Expand Up @@ -133,7 +135,7 @@ class VirtualMachineDeploy : public RequestManagerVirtualMachine
VirtualMachineDeploy():
RequestManagerVirtualMachine("one.vm.deploy",
"Deploys a virtual machine",
"A:siibi")
"A:siibis")
{
auth_op = Nebula::instance().get_vm_auth_op(History::DEPLOY_ACTION);
};
Expand Down
21 changes: 18 additions & 3 deletions include/VirtualMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,8 @@ class VirtualMachine : public PoolObjectSQL
* @param files space separated list of paths to be included in the CBD
* @param disk_id CONTEXT/DISK_ID attribute value
* @param password Password to encrypt the token, if it is set
* @param only_auto boolean to generate context only for vnets
* with NETWORK_MODE = auto
* @return -1 in case of error, 0 if the VM has no context, 1 on success
*/
int generate_context(string &files, int &disk_id, const string& password);
Expand Down Expand Up @@ -1616,6 +1618,14 @@ class VirtualMachine : public PoolObjectSQL
disks.clear_cloning_image_id(image_id, source);
}

/**
* Get network leases with NETWORK_MODE = auto for this Virtual Machine
* @pram tmpl with the scheduling results for the auto NICs
* @param estr description if any
* @return 0 if success
*/
int get_auto_network_leases(VirtualMachineTemplate * tmpl, string &estr);

private:

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -1948,9 +1958,12 @@ class VirtualMachine : public PoolObjectSQL
* netowrking updates.
* @param context attribute of the VM
* @param error string if any
* @param only_auto boolean to generate context only for vnets
* with NETWORK_MODE = auto
* @return 0 on success
*/
int generate_network_context(VectorAttribute * context, string& error);
int generate_network_context(VectorAttribute * context, string& error,
bool only_auto);

/**
* Deletes the NETWORK related CONTEXT section for the given nic, i.e.
Expand Down Expand Up @@ -1979,9 +1992,11 @@ class VirtualMachine : public PoolObjectSQL
* Parse the "CONTEXT" attribute of the template by substituting
* $VARIABLE, $VARIABLE[ATTR] and $VARIABLE[ATTR, ATTR = VALUE]
* @param error_str Returns the error reason, if any
* @param only_auto boolean to parse only the context for vnets
* with NETWORK_MODE = auto
* @return 0 on success
*/
int parse_context(string& error_str);
int parse_context(string& error_str, bool all_nics);

/**
* Parses the current contents of the context vector attribute, without
Expand All @@ -1998,7 +2013,7 @@ class VirtualMachine : public PoolObjectSQL
// Management helpers: NIC, DISK and VMGROUP
// -------------------------------------------------------------------------
/**
* Get all network leases for this Virtual Machine
* Get network leases (no auto NICs, NETWORK_MODE != auto) for this VM
* @return 0 if success
*/
int get_network_leases(string &error_str);
Expand Down
3 changes: 3 additions & 0 deletions include/VirtualMachineNic.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ class VirtualMachineNics : public VirtualMachineAttributeSet
VectorAttribute * nic_default, std::vector<VectorAttribute *>& sgs,
std::string& estr);

int get_auto_network_leases(int vm_id, int uid, VectorAttribute * nic_default,
vector<VectorAttribute*>& sgs, std::string& error_str);

/**
* Release all the network leases and SG associated to the set
* @param vmid of the VM
Expand Down
23 changes: 14 additions & 9 deletions src/cli/one_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ module OpenNebulaHelper
:description => "Networks to attach. To use a network owned by"<<
" other user use user[network]. Additional"<<
" attributes are supported like with the --disk"<<
" option.",
" option. Also you can use auto if you want that" <<
" OpenNebula select automatically the network",
:format => Array
},
{
Expand Down Expand Up @@ -1241,15 +1242,19 @@ def self.create_disk_net(objects, section, name)
user, object=*res

template<<"#{section.upcase}=[\n"
template<<" #{name.upcase}_UNAME=\"#{user}\",\n" if user
extra_attributes.each do |extra_attribute|
key, value = extra_attribute.split("=")
template<<" #{key.upcase}=\"#{value}\",\n"
end
if object.match(/^\d+$/)
template<<" #{name.upcase}_ID=#{object}\n"
if object.casecmp? "auto"
template<<" NETWORK_MODE=\"#{object}\"\n"
else
template<<" #{name.upcase}=\"#{object}\"\n"
template<<" #{name.upcase}_UNAME=\"#{user}\",\n" if user
extra_attributes.each do |extra_attribute|
key, value = extra_attribute.split("=")
template<<" #{key.upcase}=\"#{value}\",\n"
end
if object.match(/^\d+$/)
template<<" #{name.upcase}_ID=#{object}\n"
else
template<<" #{name.upcase}=\"#{object}\"\n"
end
end
template<<"]\n"
end if objects
Expand Down
10 changes: 8 additions & 2 deletions src/cli/onevm
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,21 @@ cmd=CommandParser::CmdParser.new(ARGV) do
EOT
command :deploy, deploy_desc, [:range,:vmid_list], :hostid, [:datastoreid,nil],
:options=>[ENFORCE] do
:options=>[ENFORCE, OneVMHelper::FILE] do
host_id = args[1]
verbose = "deploying in host #{host_id}"
enforce = options[:enforce].nil? ? false : options[:enforce]
ds_id = args[2].nil? ? -1 : args[2]
extra_template = nil
if options[:file]
extra_template = File.read(options[:file])
end
helper.perform_actions(args[0],options,verbose) do |vm|
vm.deploy(host_id, enforce, ds_id)
vm.deploy(host_id, enforce, ds_id, extra_template)
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,9 @@ public OneResponse info()
* default, set it to -1
* @return If an error occurs the error message contains the reason.
*/
public OneResponse deploy(int hostId, boolean enforce, int dsId)
public OneResponse deploy(int hostId, boolean enforce, int dsId, String extra_template)
{
return client.call(DEPLOY, id, hostId, enforce, dsId);
return client.call(DEPLOY, id, hostId, enforce, dsId, extra_template);
}

/**
Expand All @@ -659,7 +659,7 @@ public OneResponse deploy(int hostId, boolean enforce, int dsId)
*/
public OneResponse deploy(int hostId)
{
return deploy(hostId, false, -1);
return deploy(hostId, false, -1, "");
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/oca/ruby/opennebula/virtual_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,19 @@ def replace(opts = {})
#
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def deploy(host_id, enforce=false, ds_id=-1)
def deploy(host_id, enforce=false, ds_id=-1, extra_template="")
enforce ||= false
ds_id ||= -1
extra_template ||= ""

self.info

return call(VM_METHODS[:deploy],
@pe_id,
host_id.to_i,
enforce,
ds_id.to_i)
ds_id.to_i,
extra_template)
end

# Shutdowns an already deployed VM
Expand Down
89 changes: 84 additions & 5 deletions src/rm/RequestManagerVirtualMachine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,24 @@ VirtualMachine * RequestManagerVirtualMachine::get_vm(int id,
{
VirtualMachine * vm;

vm = static_cast<VirtualMachine *>(pool->get(id));
vm = static_cast<VirtualMachinePool *>(pool)->get(id);

if ( vm == 0 )
{
att.resp_id = id;
failure_response(NO_EXISTS, att);
return 0;
}

return vm;
}

VirtualMachine * RequestManagerVirtualMachine::get_vm_ro(int id,
RequestAttributes& att)
{
VirtualMachine * vm;

vm = static_cast<VirtualMachinePool *>(pool)->get_ro(id);

if ( vm == 0 )
{
Expand Down Expand Up @@ -735,6 +752,7 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList,
DatastorePool * dspool = nd.get_dspool();

VirtualMachine * vm;
VirtualMachineTemplate tmpl;

string hostname;
string vmm_mad;
Expand All @@ -745,13 +763,14 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList,
PoolObjectAuth * auth_ds_perms;

string tm_mad;
string error_str;

bool auth = false;
bool check_nic_auto = false;

// ------------------------------------------------------------------------
// Get request parameters and information about the target host
// ------------------------------------------------------------------------

int id = xmlrpc_c::value_int(paramList.getInt(1));
int hid = xmlrpc_c::value_int(paramList.getInt(2));
bool enforce = false;
Expand All @@ -767,6 +786,21 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList,
ds_id = xmlrpc_c::value_int(paramList.getInt(4));
}

if ( paramList.size() > 5 ) // Template with network scheduling results
{
std::string str_tmpl = xmlrpc_c::value_string(paramList.getString(5));

check_nic_auto = !str_tmpl.empty();

int rc = tmpl.parse_str_or_xml(str_tmpl, att.resp_msg);

if ( rc != 0 )
{
failure_response(INTERNAL, att);
return;
}
}

if (get_host_information(hid,
hostname,
vmm_mad,
Expand All @@ -778,11 +812,11 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList,
return;
}


// ------------------------------------------------------------------------
// Get information about the system DS to use (tm_mad & permissions)
// ------------------------------------------------------------------------

if ((vm = get_vm(id, att)) == 0)
if ((vm = get_vm_ro(id, att)) == 0)
{
return;
}
Expand All @@ -793,8 +827,13 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList,
vm->get_action() == History::UNDEPLOY_HARD_ACTION))
{
ds_id = vm->get_ds_id();

check_nic_auto = false;
}

int uid = vm->get_uid();
int gid = vm->get_gid();

vm->unlock();

if (is_public_cloud) // Set ds_id to -1 and tm_mad empty(). This is used by
Expand Down Expand Up @@ -867,8 +906,37 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList,
// ------------------------------------------------------------------------
// Authorize request
// ------------------------------------------------------------------------
if ( check_nic_auto ) //Authorize network schedule and quotas
{
RequestAttributes att_quota(uid, gid, att);

auth = vm_authorization(id, 0, 0, att, &host_perms, auth_ds_perms, 0, auth_op);
if (!att.is_admin())
{
string aname;

if (tmpl.check_restricted(aname))
{
att.resp_msg = "NIC includes a restricted attribute " + aname;

failure_response(AUTHORIZATION, att);
return;
}
}

if (!quota_authorization(&tmpl, Quotas::NETWORK, att_quota, att.resp_msg))
{
failure_response(AUTHORIZATION, att);
return;
}

auth = vm_authorization(id, 0, &tmpl, att, &host_perms, auth_ds_perms,0,
auth_op);
}
else
{
auth = vm_authorization(id, 0, 0, att, &host_perms, auth_ds_perms, 0,
auth_op);
}

if (auth == false)
{
Expand Down Expand Up @@ -907,6 +975,17 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList,
return;
}

if ( check_nic_auto && vm->get_auto_network_leases(&tmpl, error_str) != 0 )
{
att.resp_msg = error_str;
failure_response(ACTION, att);

vm->unlock();
return;
}

static_cast<VirtualMachinePool *>(pool)->update(vm);

// ------------------------------------------------------------------------
// Add deployment dependent attributes to VM
// - volatile disk (selected system DS driver)
Expand Down
Loading

0 comments on commit 1a5d158

Please sign in to comment.