def add_port(self, port_name): Modlib.runshell([ LinuxBridge.iptool, "link", "set", port_name, "mtu", str(self.mtu) ]) Modlib.runshell([LinuxBridge.brctl, "addif", self.name, port_name]) self.ports.add(port_name)
def stp(self, val=True): """ Turn STP protocol on/off. """ if val: state = "on" else: state = "off" Modlib.runshell([LinuxBridge.brctl, "stp", self.name, state])
def add_patch_port(self, peer_patch_port): iface_opt = "options:peer={0}".format(peer_patch_port) Modlib.runshell([ OvsBridge.brctl, "--may-exist", "add-port", self.name, self._patch_port, "--", "set", "interface", self._patch_port, "type=patch", iface_opt ])
def add_port(self, port_name): Modlib.runshell([ OvsBridge.iptool, "link", "set", "dev", port_name, "mtu", str(self.mtu) ]) Modlib.runshell( [OvsBridge.brctl, "--may-exist", "add-port", self.name, port_name]) self.ports.add(port_name)
def add_sdn_ctrl(self, sdn_ctrl_cfg): if sdn_ctrl_cfg["ConnectionType"] == "tcp": ctrl_conn_str = ":".join([ sdn_ctrl_cfg["ConnectionType"], sdn_ctrl_cfg["HostName"], sdn_ctrl_cfg["Port"] ]) Modlib.runshell( [OvsBridge.brctl, "set-controller", self.name, ctrl_conn_str])
def del_port(self, port_name): p = Modlib.runshell([LinuxBridge.brctl, "show", self.name]) wlist = map(str.split, p.stdout.decode("utf-8").splitlines()[1:]) port_lines = filter(lambda x: len(x) == 4, wlist) ports = map(lambda x: x[-1], port_lines) for port in ports: if port == port_name: Modlib.runshell( [LinuxBridge.brctl, "delif", self.name, port_name]) if port_name in self.ports: self.ports.remove(port_name)
def req_handler_remove_tunnel(self, cbt): msg = cbt.request.params ctl = modlib.CTL_REMOVE_TUNNEL ctl["EVIO"]["TransactionId"] = cbt.tag req = ctl["EVIO"]["Request"] req["OverlayId"] = msg["OverlayId"] req["TunnelId"] = msg["TunnelId"] self.send_control(json.dumps(ctl)) if "TapName" in msg and msg["TapName"]: modlib.runshell( [self.iptool, "link", "del", "dev", msg["TapName"]])
def req_handler_manage_bridge(self, cbt): try: olid = cbt.request.params["OverlayId"] br = self._ovl_net[olid] port_name = cbt.request.params.get("TapName") tnlid = cbt.request.params["TunnelId"] if cbt.request.params["UpdateType"] == "LnkEvCreated": # block external system components from attempting to configure our # tunnel as a source of traffic #port_name = cbt.request.params["TapName"] Modlib.runshell([ "sysctl", "net.ipv6.conf.{}.disable_ipv6=1".format(port_name) ]) Modlib.runshell([OvsBridge.iptool, "addr", "flush", port_name]) elif cbt.request.params["UpdateType"] == "LnkEvConnected": #port_name = cbt.request.params["TapName"] self._tunnels[olid][port_name] = { "PeerId": cbt.request.params["PeerId"], "TunnelId": tnlid, "ConnectedTimestamp": cbt.request.params["ConnectedTimestamp"], "TapName": port_name, "MAC": Modlib.delim_mac_str(cbt.request.params["MAC"]), "PeerMac": Modlib.delim_mac_str(cbt.request.params["PeerMac"]) } self._tunnels[olid]["DSeq"] = self._tunnels[olid]["DSeq"] + 1 br.add_port(port_name) self.log("LOG_INFO", "Port %s added to bridge %s", port_name, str(br)) elif cbt.request.params["UpdateType"] == "LnkEvRemoved": self._tunnels[olid].pop(port_name, None) if br.bridge_type == OvsBridge.bridge_type: self._tunnels[olid][ "DSeq"] = self._tunnels[olid]["DSeq"] + 1 br.del_port(port_name) self.log("LOG_INFO", "Port %s removed from bridge %s", port_name, str(br)) except RuntimeError as err: self.register_cbt("Logger", "LOG_WARNING", str(err)) cbt.set_response(None, True) self.complete_cbt(cbt)
def __init__(self, name, ip_addr, prefix_len, mtu, cm, stp_enable, sdn_ctrl_cfg=None): """ Initialize an OpenvSwitch bridge object. """ super(OvsBridge, self).__init__(name, ip_addr, prefix_len, mtu, cm) if OvsBridge.brctl is None or OvsBridge.iptool is None: raise RuntimeError( "openvswitch-switch was not found" if not OvsBridge.brctl else "iproute2 was not found") self._patch_port = self.name[:3] + "-pp0" Modlib.runshell([OvsBridge.brctl, "--may-exist", "add-br", self.name]) if ip_addr and prefix_len: net = "{0}/{1}".format(ip_addr, prefix_len) Modlib.runshell( [OvsBridge.iptool, "addr", "flush", "dev", self.name]) Modlib.runshell( [OvsBridge.iptool, "addr", "add", net, "dev", self.name]) try: Modlib.runshell([ OvsBridge.brctl, "set", "int", self.name, "mtu_request=" + str(self.mtu) ]) except RuntimeError as e: self.cm.register_cbt( "Logger", "LOG_WARN", "The following error occurred while setting" " MTU for OVS bridge: {0}".format(e)) self.stp(stp_enable) Modlib.runshell( [OvsBridge.iptool, "link", "set", "dev", self.name, "up"]) if sdn_ctrl_cfg: self.add_sdn_ctrl(sdn_ctrl_cfg)
def add_port(self, port_name): self.name = port_name net = "{0}/{1}".format(self.ip_addr, self.prefix_len) Modlib.runshell([VNIC.iptool, "addr", "add", net, "dev", self.name]) Modlib.runshell( [VNIC.iptool, "link", "set", self.name, "mtu", str(self.mtu)]) Modlib.runshell([VNIC.iptool, "link", "set", "dev", self.name, "up"])
def __init__(self, name, ip_addr, prefix_len, mtu, cm, stp_enable): """ Initialize a Linux bridge object. """ super(LinuxBridge, self).__init__(name, ip_addr, prefix_len, mtu, cm) if LinuxBridge.brctl is None or LinuxBridge.iptool is None: raise RuntimeError("bridge-utils was not found" if not LinuxBridge. brctl else "iproute2 was not found") p = Modlib.runshell([LinuxBridge.brctl, "show"]) wlist = map(str.split, p.stdout.decode("utf-8").splitlines()[1:]) brwlist = filter(lambda x: len(x) != 1, wlist) brlist = map(lambda x: x[0], brwlist) for br in brlist: if br == name: return p = Modlib.runshell([LinuxBridge.brctl, "addbr", self.name]) net = "{0}/{1}".format(ip_addr, prefix_len) if ip_addr and prefix_len: Modlib.runshell( [LinuxBridge.iptool, "addr", "add", net, "dev", name]) self.stp(stp_enable) Modlib.runshell([LinuxBridge.iptool, "link", "set", "dev", name, "up"])
def del_br(self): # Set the device down and delete the bridge Modlib.runshell( [LinuxBridge.iptool, "link", "set", "dev", self.name, "down"]) Modlib.runshell([LinuxBridge.brctl, "delbr", self.name])
def set_port_prio(self, port, prio): """ Set port priority value. """ Modlib.runshell( [LinuxBridge.brctl, "setportprio", self.name, port, str(prio)])
def set_path_cost(self, port, cost): """ Set port path cost value for STP protocol. """ Modlib.runshell( [LinuxBridge.brctl, "setpathcost", self.name, port, str(cost)])
def set_bridge_prio(self, prio): """ Set bridge priority value. """ Modlib.runshell( [LinuxBridge.brctl, "setbridgeprio", self.name, str(prio)])
def del_sdn_ctrl(self): Modlib.runshell([OvsBridge.brctl, "del-controller", self.name])
def del_br(self): self.del_sdn_ctrl() Modlib.runshell([OvsBridge.brctl, "--if-exists", "del-br", self.name])
def del_port(self, port_name): Modlib.runshell( [OvsBridge.brctl, "--if-exists", "del-port", self.name, port_name]) if port_name in self.ports: self.ports.remove(port_name)
def stp(self, enable): Modlib.runshell([ OvsBridge.brctl, "set", "bridge", self.name, "stp_enable={0}".format("true" if enable else "false") ])