Skip to content

Commit

Permalink
Bug:#1359633 Adding the default gateway to the front end netwrk in th…
Browse files Browse the repository at this point in the history
…e namespace

Change-Id: I8aa702a24db12dec6f540ae80d26a79675348280
  • Loading branch information
divakardhar committed Sep 23, 2014
1 parent 6d7b399 commit 29179cb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/vnsw/agent/oper/namespace_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ void NamespaceManager::StartNetNS(ServiceInstance *svc_instance,
if (props.service_type == ServiceInstance::LoadBalancer) {
cmd_str << " --cfg-file " << loadbalancer_config_path_ <<
props.pool_id << "/etc/haproxy/haproxy.cfg";
cmd_str << " --gw-ip " << props.gw_ip;
}

if (update) {
Expand Down Expand Up @@ -427,8 +428,11 @@ void NamespaceManager::StopNetNS(ServiceInstance *svc_instance,

const ServiceInstance::Properties &props = state->properties();
if (props.instance_id.is_nil() ||
props.vmi_inside.is_nil() ||
props.vmi_outside.is_nil()) {
props.vmi_inside.is_nil()) {
return;
}

if (props.interface_count == 2 && props.vmi_outside.is_nil()) {
return;
}

Expand Down
14 changes: 14 additions & 0 deletions src/vnsw/agent/oper/service_instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ static void FindAndSetInterfaces(
SubNetContainsIpv4(subnets.ipam_subnets[i],
properties->ip_addr_inside)) {
properties->ip_prefix_len_inside = prefix_len;
properties->gw_ip =
subnets.ipam_subnets[i].default_gateway;
} else if (netname == right_netname &&
SubNetContainsIpv4(subnets.ipam_subnets[i],
properties->ip_addr_outside)) {
Expand Down Expand Up @@ -303,6 +305,7 @@ void ServiceInstance::Properties::Clear() {
mac_addr_outside.empty();
ip_addr_inside.empty();
ip_addr_outside.empty();
gw_ip.empty();
ip_prefix_len_inside = -1;
ip_prefix_len_outside = -1;
interface_count = 0;
Expand Down Expand Up @@ -363,6 +366,11 @@ int ServiceInstance::Properties::CompareTo(const Properties &rhs) const {
return cmp;
}

cmp = compare(gw_ip, rhs.gw_ip);
if (cmp != 0) {
return cmp;
}

cmp = compare(pool_id, rhs.pool_id);
if (cmp == 0) {
if (!pool_id.is_nil())
Expand Down Expand Up @@ -410,6 +418,10 @@ std::string ServiceInstance::Properties::DiffString(
if (compare(pool_id, rhs.pool_id)) {
ss << " pool_id: -" << pool_id << " +" << rhs.pool_id;
}

if (compare(gw_ip, rhs.gw_ip)) {
ss << " gw_ip: -" << gw_ip << " +" << rhs.gw_ip;
}
return ss.str();
}

Expand All @@ -432,6 +444,8 @@ bool ServiceInstance::Properties::Usable() const {
}

if (service_type == LoadBalancer) {
if (gw_ip.empty())
return false;
return (!pool_id.is_nil());
}

Expand Down
1 change: 1 addition & 0 deletions src/vnsw/agent/oper/service_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class ServiceInstance : public AgentRefCount<ServiceInstance>,

std::string ip_addr_inside;
std::string ip_addr_outside;
std::string gw_ip;

int ip_prefix_len_inside;
int ip_prefix_len_outside;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class NetnsManager(object):
LBAAS_PROCESS = 'haproxy'

def __init__(self, vm_uuid, nic_left, nic_right, root_helper='sudo',
cfg_file=None, update=False):
cfg_file=None, update=False, gw_ip=None):
self.vm_uuid = vm_uuid
self.namespace = self.NETNS_PREFIX + self.vm_uuid
self.nic_left = nic_left
Expand All @@ -69,6 +69,7 @@ def __init__(self, vm_uuid, nic_left, nic_right, root_helper='sudo',
self.vrouter_client = ContrailVRouterApi()
self.cfg_file = cfg_file
self.update = update
self.gw_ip = gw_ip

def _get_tap_name(self, uuid_str):
return (self.TAP_PREFIX + uuid_str)[:self.DEV_NAME_LEN]
Expand Down Expand Up @@ -130,28 +131,37 @@ def set_lbaas(self):
pid = self._get_lbaas_pid()
if (self.update is False):
if pid is not None:
cmd = """kill -9 %(pid)s""" % {'pid':pid}
try:
s = subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError:
print "SIGKILL Error"
self.release_lbaas()

self.ip_ns.netns.execute([self.LBAAS_PROCESS, '-f', self.cfg_file, '-D',
'-p', pid_file])
self.ip_ns.netns.execute(['route', 'add', 'default', 'gw', self.gw_ip])
else:
if pid is not None:
self.ip_ns.netns.execute([self.LBAAS_PROCESS, '-f', self.cfg_file, '-D', '-p', pid_file, '-sf', pid])
else:
print "No old Haproxy process to Update"
try:
self.ip_ns.netns.execute(['route', 'add', 'default', 'gw', self.gw_ip])
except RuntimeError:
pass

def release_lbaas(self):
if not self.ip_ns.netns.exists(self.namespace):
raise ValueError('Need to create the network namespace before '
'relasing lbaas')
pid = self._get_lbaas_pid()
if pid is not None:
cmd = """kill -9 %(pid)s""" % {'pid':pid}
try:
s = subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError:
print "SIGKILL Error"
try:
self.ip_ns.netns.execute(['route', 'del', 'default'])
except RuntimeError:
pass


def destroy(self):
if not self.ip_ns.netns.exists(self.namespace):
Expand Down Expand Up @@ -309,7 +319,11 @@ def _parse_args(self, args_str):
create_parser.add_argument(
"--cfg-file",
default=None,
help=("config file for lbaas"))
help=("Config file for lbaas"))
create_parser.add_argument(
"--gw-ip",
default=None,
help=("Gateway IP for Virtual Network"))
create_parser.set_defaults(func=self.create)

destroy_parser = subparsers.add_parser('destroy')
Expand Down Expand Up @@ -365,7 +379,7 @@ def create(self):
netns_mgr = NetnsManager(netns_name, nic_left, nic_right,
root_helper=self.args.root_helper,
cfg_file=self.args.cfg_file,
update=self.args.update)
update=self.args.update, gw_ip=self.args.gw_ip)

if (self.args.update is False):
if netns_mgr.is_netns_already_exists():
Expand Down Expand Up @@ -397,10 +411,9 @@ def destroy(self):

netns_mgr = NetnsManager(netns_name, nic_left, nic_right,
root_helper=self.args.root_helper,
cfg_file=self.args.cfg_file)
cfg_file=self.args.cfg_file, gw_ip=None)

netns_mgr.unplug_namespace_interface()

if self.args.service_type == self.SOURCE_NAT:
netns_mgr.destroy()
elif self.args.service_type == self.LOAD_BALANCER:
Expand Down

0 comments on commit 29179cb

Please sign in to comment.