From 23ebb7c2ee13a31f6d8139e1dca157cfce83bb35 Mon Sep 17 00:00:00 2001 From: Edouard Bourguignon Date: Fri, 13 Apr 2018 16:24:46 +0200 Subject: [PATCH] B #1865: Allow vlan range notation in ovs (#1879) --- src/vnm_mad/remotes/ovswitch/OpenvSwitch.rb | 27 +++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/vnm_mad/remotes/ovswitch/OpenvSwitch.rb b/src/vnm_mad/remotes/ovswitch/OpenvSwitch.rb index 4367ab37b7..5242a10934 100644 --- a/src/vnm_mad/remotes/ovswitch/OpenvSwitch.rb +++ b/src/vnm_mad/remotes/ovswitch/OpenvSwitch.rb @@ -179,7 +179,11 @@ class OpenvSwitchVLAN < VNMMAD::VNMDriver if range? range ovs_vsctl_cmd = "#{command(:ovs_vsctl)} set Port #{@nic[:tap]}" - cmd = "#{ovs_vsctl_cmd} trunks=#{range}" + # Open vSwitch 2.7.0+ allows range intervals (x-y), but + # we need to support even older versions. We expand the + # intervals into the list of values [x,x+1,...,y-1,y], + # which should work for all. + cmd = "#{ovs_vsctl_cmd} trunks=#{expand_range(range)}" run cmd cmd = "#{ovs_vsctl_cmd} vlan_mode=native-untagged" @@ -336,9 +340,28 @@ class OpenvSwitchVLAN < VNMMAD::VNMDriver end def range?(range) - !range.to_s.match(/^\d+(,\d+)*$/).nil? + !range.to_s.match(/^\d+([,-]\d+)*$/).nil? end + def expand_range(range) + items = [] + + range.split(',').each do |i| + l, r = i.split('-') + + if r.nil? + items << i + elsif r >= l + items.concat((l..r).to_a) + else + items.concat((r..l).to_a) + end + end + + items.uniq.join(',') + end + + private # Generate the name of the vlan device which will be added to the bridge. def get_vlan_dev_name