Ejemplo n.º 1
0
Archivo: vps_ops.py Proyecto: 42qu/vps
    def change_qos(self, _xv):
        meta_path = self._meta_path(_xv.vps_id)
        if not os.path.exists(meta_path):
            xv = self.load_vps_meta(_xv.vps_id, is_trash=True)
            xv.vif_ext.bandwidth = _xv.vif_ext.bandwidth
            bandwidth = float(_xv.vif_ext.bandwidth or 0)
            vif_name = xv.vif_ext.ifname
            self.save_vps_meta(xv, is_trash=True)
        else:
            xv = self._load_vps_meta(meta_path)
            if not _xv.vif_ext or not xv.vif_ext:
                return
            xv.vif_ext.bandwidth = _xv.vif_ext.bandwidth
            bandwidth = float(_xv.vif_ext.bandwidth or 0)
            vif_name = xv.vif_ext.ifname
            self.save_vps_meta(xv)
            if conf.USE_OVS:
                if xv.is_running():
                    ovsops = OVSOps()
                    ovsops.unset_traffic_limit(vif_name)
                    ovsops.set_traffic_limit(vif_name, int(bandwidth * 1000))
                    self.loginfo(xv, "updated vif=%s bandwidth to %s Mbps" %
                                 (vif_name, bandwidth))
                    if not xv.wait_until_reachable(5):
                        raise Exception("ip unreachable!")
            else:
#               if xv.stop():
#                   self.loginfo(xv, "vps stopped")
#               else:
#                   xv.destroy()
#                   self.loginfo(xv, "vps cannot shutdown, destroyed it")
                self.create_xen_config(xv)
Ejemplo n.º 2
0
def add_rule(net):
    assert net
    ovsops = OVSOps()
    ofport = ovsops.find_ofport_by_name(conf.EXT_INF)
    assert ofport > 0
    call_cmd(
        "ovs-ofctl add-flow %s 'ip,in_port=%s,nw_dst=%s,priority=2000,action=normal'"
        % (conf.XEN_BRIDGE, ofport, net))
    call_cmd("ovs-ofctl add-flow %s 'ip,in_port=%s,priority=10,action=drop'" %
             (conf.XEN_BRIDGE, ofport))
Ejemplo n.º 3
0
def add_rule(net):
    assert net
    ovsops = OVSOps()
    ofport = ovsops.find_ofport_by_name(conf.EXT_INF)
    assert ofport > 0
    call_cmd(
        "ovs-ofctl add-flow %s 'ip,in_port=%s,nw_dst=%s,priority=2000,action=normal'" %
        (conf.XEN_BRIDGE, ofport, net))
    call_cmd("ovs-ofctl add-flow %s 'ip,in_port=%s,priority=10,action=drop'" %
             (conf.XEN_BRIDGE, ofport))
Ejemplo n.º 4
0
def main():
    if len(sys.argv) <= 1:
        usage()
        os._exit(0)

    bridge = sys.argv[1]
    cmd = "ovs-ofctl dump-flows %s" % (bridge)
    out = call_cmd(cmd)
    lines = out.split("\n")
    regx = re.compile(r'^.*in_port=(\d+).*$')
    ovsops = OVSOps()
    unused_ofport = dict()
    using_ofport = dict()
    print "of_port that are in used:"
    for line in lines:
        om = re.match(regx, line)
        if not om:
            continue
        of_port = int(om.group(1))
        if of_port < 0:
            continue
        if using_ofport.has_key(of_port) or unused_ofport.has_key(of_port):
            continue
        try:
            if_name = ovsops.ovsdb.find_one('name', 'Interface',
                                            {'ofport': of_port})
            print if_name, of_port
            using_ofport[of_port] = 1
        except LookupError, e:
            unused_ofport[of_port] = 1
Ejemplo n.º 5
0
def check_all():
    assert conf.XEN_CONFIG_DIR and os.path.isdir(conf.XEN_CONFIG_DIR)
    assert conf.VPS_METADATA_DIR and os.path.isdir(conf.VPS_METADATA_DIR)
    client = VPSMgr()
    ovsops = OVSOps()
    all_ids = client.vpsops.all_vpsid_from_config()
    print ""
    print "xen_config: %d, running: %d" % (len(all_ids), client.vpsops.running_count)
    for vps_id in all_ids:
        print "vps", vps_id
        _clear_filter(client, ovsops, vps_id)
Ejemplo n.º 6
0
 def change_qos(self, _xv):
     meta_path = self._meta_path(_xv.vps_id)
     if not os.path.exists(meta_path):
         xv = self.load_vps_meta(_xv.vps_id, is_trash=True)
         xv.vif_ext.bandwidth = _xv.vif_ext.bandwidth
         bandwidth = float(_xv.vif_ext.bandwidth or 0)
         vif_name = xv.vif_ext.ifname
         self.save_vps_meta(xv, is_trash=True)
     else:
         xv = self._load_vps_meta(meta_path)
         if not _xv.vif_ext or not xv.vif_ext:
             return
         xv.vif_ext.bandwidth = _xv.vif_ext.bandwidth
         bandwidth = float(_xv.vif_ext.bandwidth or 0)
         vif_name = xv.vif_ext.ifname
         self.save_vps_meta(xv)
         if conf.USE_OVS:
             if xv.is_running():
                 ovsops = OVSOps()
                 ovsops.unset_traffic_limit(vif_name)
                 ovsops.set_traffic_limit(vif_name, int(bandwidth * 1000))
                 self.loginfo(
                     xv, "updated vif=%s bandwidth to %s Mbps" %
                     (vif_name, bandwidth))
                 if not xv.wait_until_reachable(5):
                     raise Exception("ip unreachable!")
         else:
             #               if xv.stop():
             #                   self.loginfo(xv, "vps stopped")
             #               else:
             #                   xv.destroy()
             #                   self.loginfo(xv, "vps cannot shutdown, destroyed it")
             self.create_xen_config(xv)
Ejemplo n.º 7
0
def main():
    bridge = args[0]
    vif_name = args[1]
    logger = Log("vps_mgr", config=conf)
    try:
        ovsops = OVSOps()
        ofport = ovsops.find_ofport_by_name(vif_name)
        if ofport < 0:
            logger.error("vif %s ofport=%s, skip it" % (vif_name, ofport))
        else:
            ovsops.unset_mac_filter(bridge, ofport)
        # it's strange that if you unset traffic first, might find ofport==-1
        ovsops.unset_traffic_limit(vif_name)
        logger.debug("unset %s" % vif_name)
        return 0
    except Exception, e:
        logger.exception(e)
        print >> sys.stderr, str(e)
        return 0
Ejemplo n.º 8
0
def main():
    bridge = args[0]
    vif_name = args[1]
    logger = Log("vps_mgr", config=conf)
    try:
        ovsops = OVSOps()
        ofport = ovsops.find_ofport_by_name(vif_name)
        if ofport < 0:
            logger.error("vif %s ofport=%s, skip it" % (vif_name, ofport))
        else:
            ovsops.unset_mac_filter(bridge, ofport)
        # it's strange that if you unset traffic first, might find ofport==-1
        ovsops.unset_traffic_limit(vif_name)
        logger.debug("unset %s" % vif_name)
        return 0
    except Exception, e:
        logger.exception(e)
        print >> sys.stderr, str(e)
        return 0
Ejemplo n.º 9
0
def main():
    bridge = args[0]
    vif_name = args[1]

    logger = Log("vps_mgr", config=conf)
    vpsops = VPSOps(logger)
    logger.debug("set %s" % vif_name)
    try:
        ovsops = OVSOps()
        om = re.match(r'^\w+?(\d+)\w*?$', vif_name)
        if not om:
            print >> sys.stderr, "wrong vif format %s" % (vif_name)
            return 1
        vps_id = int(om.group(1))
        xv = vpsops.load_vps_meta(vps_id)
        vif = xv.vifs.get(vif_name)
        if not vif:
            logger.error("no vif %s in metadata of %s" % (vif_name, vps_id))
            return 1
        ofport = ovsops.find_ofport_by_name(vif_name)
        if ofport < 0:
            logger.error(
                "vif %s ofport=%s, fix it by delete the port from bridge " %
                (vif_name, ofport))
            ovsops.del_port_from_bridge(bridge, vif_name)
            ovsops.add_port_to_bridge(bridge, vif_name)
            ofport = ovsops.find_ofport_by_name(vif_name)
            if ofport < 0:
                logger.error("vif %s ofport=%s, impossible " %
                             (vif_name, ofport))
        if ofport >= 0:
            ovsops.set_mac_filter(bridge, ofport, vif.ip_dict.keys())
        ovsops.unset_traffic_limit(vif_name)
        bandwidth = float(vif.bandwidth or 0)
        ovsops.set_traffic_limit(vif_name, int(bandwidth * 1000))
        print "set vif %s bandwidth %sm/s" % (vif_name, vif.bandwidth)
        return 0
    except Exception, e:
        logger.exception(e)
        print >> sys.stderr, str(e)
        return 1
Ejemplo n.º 10
0
def main():
    bridge = args[0]
    vif_name = args[1]

    logger = Log("vps_mgr", config=conf)
    vpsops = VPSOps(logger)
    logger.debug("set %s" % vif_name)
    try:
        ovsops = OVSOps()
        om = re.match(r"^\w+?(\d+)\w*?$", vif_name)
        if not om:
            print >> sys.stderr, "wrong vif format %s" % (vif_name)
            return 1
        vps_id = int(om.group(1))
        xv = vpsops.load_vps_meta(vps_id)
        vif = xv.vifs.get(vif_name)
        if not vif:
            logger.error("no vif %s in metadata of %s" % (vif_name, vps_id))
            return 1
        ofport = ovsops.find_ofport_by_name(vif_name)
        if ofport < 0:
            logger.error("vif %s ofport=%s, fix it by delete the port from bridge " % (vif_name, ofport))
            ovsops.del_port_from_bridge(bridge, vif_name)
            ovsops.add_port_to_bridge(bridge, vif_name)
            ofport = ovsops.find_ofport_by_name(vif_name)
            if ofport < 0:
                logger.error("vif %s ofport=%s, impossible " % (vif_name, ofport))
        if ofport >= 0:
            ovsops.set_mac_filter(bridge, ofport, vif.ip_dict.keys())
        ovsops.unset_traffic_limit(vif_name)
        bandwidth = float(vif.bandwidth or 0)
        ovsops.set_traffic_limit(vif_name, int(bandwidth * 1000))
        print "set vif %s bandwidth %sm/s" % (vif_name, vif.bandwidth)
        return 0
    except Exception, e:
        logger.exception(e)
        print >> sys.stderr, str(e)
        return 1