Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions ansible/roles/vm_set/library/vm_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
PTF_FP_IFACE_TEMPLATE = 'eth%d'
BACK_ROOT_END_IF_TEMPLATE = 'veth-bb-%s'
BACK_VM_END_IF_TEMPLATE = 'veth-bv-%s'
RETRIES = 3


class VMTopology(object):
Expand Down Expand Up @@ -405,7 +406,9 @@ def bind_phys_vlan(self, br_name, vlan_iface, injected_iface, vm_iface, disconne
if vlan_iface not in ports:
VMTopology.cmd('ovs-vsctl add-port %s %s' % (br_name, vlan_iface))

bindings = VMTopology.get_ovs_port_bindings(br_name)
bindings, error = VMTopology.get_ovs_port_bindings(br_name, vlan_iface)
if error:
raise Exception("Can't find vlan_iface_id")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please raise an Exception inside of the get_ovs_port_bindings method? I don't see any reason you want to have it here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I will change that part.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure will raise in function get_ovs_port_bindings.

vlan_iface_id = bindings[vlan_iface]
injected_iface_id = bindings[injected_iface]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please hide retry logic inside of get_ovs_port_bindings()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.) I can do that, but I will need to add an extra argument [=vlan_iface] to function get_ovs_port_bindings().
i.e get_ovs_port_bindings(br_name) ==> get_ovs_port_bindings(br_name, vlan_iface).

2.) Or I can write an wrapper on top on get_ovs_port_bindings(br_name) to check vlan_iface.

Kindly let me know which sounds better. Thanks a lot for review.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gentle Reminder for review.

vm_iface_id = bindings[vm_iface]
Expand Down Expand Up @@ -490,19 +493,33 @@ def get_ovs_br_ports(bridge):
out = VMTopology.cmd('ovs-vsctl list-ports %s' % bridge)
return set(out.split('\n'))


@staticmethod
def get_ovs_port_bindings(bridge):
out = VMTopology.cmd('ovs-ofctl show %s' % bridge)
lines = out.split('\n')
result = {}
for line in lines:
matched = re.match(r'^\s+(\S+)\((\S+)\):\s+addr:.+$', line)
if matched:
port_id = matched.group(1)
iface_name = matched.group(2)
result[iface_name] = port_id

return result
def get_ovs_port_bindings(bridge, vlan_iface = None):
# Vlan interface addition may take few secs to reflect in OVS Command,
# Let`s retry few times in that case.
retries = 0
vlan_iface_id = None
while retries < RETRIES:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd better to use for retries in xrange(RETRIES):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change this to range. range takes more space than xrange but it is supported in python3 as well.

out = VMTopology.cmd('ovs-ofctl show %s' % bridge)
lines = out.split('\n')
result = {}
for line in lines:
matched = re.match(r'^\s+(\S+)\((\S+)\):\s+addr:.+$', line)
if matched:
port_id = matched.group(1)
iface_name = matched.group(2)
result[iface_name] = port_id
if vlan_iface is None:
return result, False
# Check if we have vlan_iface populated
vlan_iface_id = result[vlan_iface]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can you be sure you have vlan_iface key in the result? I think you need to check that you have vlan_iface key in the result first

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure let me add a check for this.

if vlan_iface_id:
return result, False
time.sleep(1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make the sleep time increasing in some way?
1,2,3 seconds
or
1, 3, 5 seconds

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can use 2*retries +1.

retries += 1

return result, True

@staticmethod
def ifconfig(cmdline):
Expand Down