1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
16 from vstf.agent.env.vswitch_plugins import model
17 from vstf.common.utils import check_and_kill, check_and_rmmod, check_call, check_output, \
18 get_eth_by_bdf, my_mkdir, call
20 LOG = logging.getLogger(__name__)
23 class OvsPlugin(model.VswitchPlugin):
26 self.daemons = ['ovs-vswitchd', 'ovsdb-server']
27 self.mods = ['openvswitch']
28 self.dirs = {'db': "/usr/local/etc/openvswitch"}
30 self.cmds.append("mkdir -p /usr/local/etc/openvswitch")
31 self.cmds.append("ovsdb-tool create /usr/local/etc/openvswitch/conf.db")
32 self.cmds.append("ovsdb-server --remote=punix:/usr/local/var/run/openvswitch/db.sock \
33 --remote=db:Open_vSwitch,Open_vSwitch,manager_options \
34 --private-key=db:Open_vSwitch,SSL,private_key \
35 --certificate=db:Open_vSwitch,SSL,certificate \
36 --bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert \
38 self.cmds.append("ovs-vsctl --no-wait init")
39 self.cmds.append("ovs-vswitchd --pidfile --detach")
40 self.initialized = False
43 if not self.initialized:
45 self.initialized = True
48 """clean for ovs. Rmmod openvswitch.ko, kill openvswitch daemon process.
51 for process in self.daemons:
52 check_and_kill(process)
55 for _, directory in self.dirs.items():
56 if os.path.isdir(directory):
57 LOG.info('rm -rf %s', directory)
58 shutil.rmtree(directory, ignore_errors=True)
59 self.initialized = False
62 def create_br(self, br_cfg):
63 """Create a bridge(virtual switch). Return True for success, return False for failure.
65 :param dict br_cfg: configuration for bridge creation like
72 "vlan_mode": "access",
81 name, uplinks = br_cfg['name'], br_cfg['uplinks']
83 check_call("ovs-vsctl add-br %s" % (name), shell=True)
84 if br_cfg['vtep']: # vxlan supports
85 local_ip, remote_ip = br_cfg['vtep']['local_ip'], br_cfg['vtep']['remote_ip']
86 assert len(uplinks) == 1
88 device = get_eth_by_bdf(uplink['bdf'])
91 check_call("ifconfig %s %s up" % (device, local_ip), shell=True)
92 check_call("ovs-vsctl add-port %s %s" % (name, vtep), shell=True)
93 check_call("ovs-vsctl set interface %s type=vxlan options:remote_ip=%s" % (vtep, remote_ip), shell=True)
94 for uplink in uplinks:
95 device = get_eth_by_bdf(uplink['bdf'])
96 vlan_mode = uplink['vlan_mode']
97 vlan_id = uplink['vlan_id']
98 check_call("ip link set dev %s up" % device, shell=True)
99 call("ethtool -A %s rx off tx off " % device, shell=True)
100 check_call("ovs-vsctl add-port %s %s" % (name, device), shell=True)
101 if vlan_mode == 'trunk':
102 check_call("ovs-vsctl set port %s trunks=%s" % (device, vlan_id), shell=True)
103 elif vlan_mode == 'access':
104 check_call("ovs-vsctl set port %s tag=%s" % (device, vlan_id), shell=True)
106 raise Exception("unreconized vlan_mode:%s" % vlan_mode)
109 def set_tap_vid(self, tap_cfg):
110 """set vlan id or vxlan id for tap device(virtual nic for vm).
111 return True for success, return False for failure.
113 :param dict tap_cfg: dictionary config for tap device like
115 "tap_name": "tap_in",
116 "vlan_mode": "access",
121 port, vlan_mode, vlan = tap_cfg['tap_name'], tap_cfg['vlan_mode'], tap_cfg['vlan_id']
122 assert vlan_mode in ('access', 'vxlan')
123 if int(vlan) > '4095':
125 self.__set_tap_vid(port, "vxlan", vlan)
128 self.__set_tap_vid(port, vlan_mode, vlan)
131 def set_fastlink(self, br_cfg):
132 """connect two ports directly, so that packets comes from any one port be forwarded to the other.
133 return True for success, return False for failure.
135 :param dict br_cfg: dictionary configuration for linking ports.
146 br_name = br_cfg['name']
147 for fast_cfg in br_cfg['fastlink']:
148 p1, p2 = fast_cfg['inport'], fast_cfg['outport']
149 self.__fastlink(br_name, p1, p2)
152 def _start_servers(self):
153 for _, directory in self.dirs.items():
155 for mod in self.mods:
156 check_call("modprobe %s" % mod, shell=True)
157 for cmd in self.cmds:
158 check_call(cmd, shell=True)
161 def __set_tap_vid(self, port, vlan_mode, vlan_id):
162 if vlan_mode == 'vxlan':
163 raise Exception("don't support vxlan setting right now.")
164 elif vlan_mode == 'trunk':
165 check_call("ovs-vsctl set port %s trunks=%s" % (port, vlan_id), shell=True)
167 check_call("ovs-vsctl set port %s tag=%s" % (port, vlan_id), shell=True)
169 def __fastlink(self, br, p1, p2):
170 LOG.info("_fastlink(%s,%s,%s)", br, p1, p2)
171 p1 = p1.replace(' ', '')
172 p2 = p2.replace(' ', '')
173 bdfs = check_output("lspci |grep Eth | awk '{print $1}'", shell=True).splitlines()
175 p1 = get_eth_by_bdf(p1)
177 p2 = get_eth_by_bdf(p2)
179 buf = check_output("ovs-ofctl show %s" % br, shell=True)
180 port_info = re.compile(r"[0-9]+\(.*\)", re.IGNORECASE | re.MULTILINE)
181 for s in port_info.findall(buf):
182 port_num, interface = s.replace('(', ' ').replace(')', ' ').split()
183 ovs_port[interface] = port_num
184 pn1, pn2 = ovs_port[p1], ovs_port[p2]
185 check_call("ovs-ofctl add-flow %s in_port=%s,priority=100,action=output:%s" % (br, pn1, pn2), shell=True)
186 check_call("ovs-ofctl add-flow %s in_port=%s,priority=100,action=output:%s" % (br, pn2, pn1), shell=True)