Пример #1
0
    def set_dynamic_bond(self, conf):
        nic_list = ""

        if conf.bond_mode != UpLinkManager_pb2.UpLinkConf.BALANCE_SLB and \
                conf.bond_mode != UpLinkManager_pb2.UpLinkConf.BALANCE_TCP:
            raise net_agentd_exception.NetAgentException(
                'The bond_mode is incorrect')

        if conf.bond_name is None:
            raise net_agentd_exception.NetAgentException(
                'The bond_name is incorrect')

        if conf.bond_mode == UpLinkManager_pb2.UpLinkConf.BALANCE_TCP:
            bond_mode = 'balance-tcp'
        if conf.bond_mode == UpLinkManager_pb2.UpLinkConf.BALANCE_SLB:
            bond_mode = 'balance-slb'

        for nic_name in conf.nic_names:
            nic_list = nic_list + ' ' + nic_name

        cmd_str = '%s add-bond %s %s %s lacp=active bond_mode=%s' % (
            util.OVS_VSCTL_CMD, self._vswitch_name, conf.bond_name, nic_list,
            bond_mode)
        cmd = envoy.run(cmd_str)
        if cmd.status_code == 0:
            logging.info('%s success' % cmd_str)
        else:
            raise net_agentd_exception.NetAgentException(
                '%s failed \n%s' % (cmd_str, cmd.std_err))
Пример #2
0
    def detach_rules(self, iface):
        """detach rules to iface
        :param:{"iface": which iface will be detach qos rules}
        """

        qos_rules = get_qos_rules(self._qos_name)

        for rule in qos_rules:
            if rule.direction == "in":
                option = ''
                if rule.average:
                    option = "ingress_policing_rate=0"
                if rule.burst:
                    option = option + ' ' + "ingress_policing_burst=0"

                cmd_str = '%s set Interface %s %s' % (util.OVS_VSCTL_CMD,
                                                      iface,
                                                      option)
                ovs_cmd = envoy.run(cmd_str)
                if ovs_cmd.status_code != 0:
                    raise net_agentd_exception.NetAgentException("%s failed:\n%s" % (cmd_str, ovs_cmd.std_err))

            if rule.direction == "out":
                cmd_str = '%s clear port %s qos' % (util.OVS_VSCTL_CMD,
                                                    iface)
                ovs_cmd = envoy.run(cmd_str)
                if ovs_cmd.status_code != 0:
                    raise net_agentd_exception.NetAgentException("%s failed:\n%s" % (cmd_str, ovs_cmd.std_err))
Пример #3
0
 def stop_nic(self):
     if self._class_net_path is not None:
         cmd_str = '%s link set %s down' % (util.IP_CMD, self._nic_name)
         command = envoy.run(cmd_str)
         if command.status_code != 0:
             raise net_agentd_exception.NetAgentException("%s failed to run" % cmd_str)
     else:
         raise net_agentd_exception.NetAgentException("%s doesnt exist" % self._nic_name)
Пример #4
0
    def attach_driver(self, new_driver_path, raw_device):
        if raw_device == "" or os.path.join(new_driver_path) is False:
            raise net_agentd_exception.NetAgentException("parameter error")

        try:
            new_driver_bind_path = os.path.join(new_driver_path, "bind")
            sys_util.echo(new_driver_bind_path, raw_device)
        except:
            raise net_agentd_exception.NetAgentException('changer driver %s failed' % new_driver_path)
Пример #5
0
 def detach_driver(self):
     raw_device = self.get_raw_device()
     if raw_device:
         try:
             driver_unbind_path = os.path.join(self._class_net_path, "device/driver/unbind")
             sys_util.echo(driver_unbind_path, raw_device)
         except:
             raise net_agentd_exception.NetAgentException(
                 '%s detach driver %s failed' % (raw_device, driver_unbind_path))
     else:
         raise net_agentd_exception.NetAgentException("%s doesnt exist" % raw_device)
Пример #6
0
def GetXmlElementByXpath(xml_tree, xpath):
    """ get Element from ElementTree by xpath
    :param request : xml tree
    :returns : etree._Element
    """
    try:
        return xml_tree.find(xpath)
    except OSError:
        logging.error(traceback.format_exc())
        raise net_agentd_exception.NetAgentException("OSError")
    except etree.LxmlError:
        logging.critical(traceback.format_exc())
        raise net_agentd_exception.NetAgentException("LxmlError")
Пример #7
0
    def get_mtu(self):
        mtu = ""

        if self._class_net_path is not None:
            try:
                path = os.path.join(self._class_net_path, "mtu")
                mtu = sys_util.cat(path).strip()
            except:
                raise net_agentd_exception.NetAgentException("%s get mtu failed" % self._nic_name)
        else:
            raise net_agentd_exception.NetAgentException("%s doesnt exist" % self._nic_name)

        return mtu
Пример #8
0
def ReadXml(xml_file):
    """ read xml file
    :param request : xml file path
    :returns : etree._ElementTree
    """
    try:
        return etree.parse(xml_file)
    except OSError:
        logging.error(traceback.format_exc())
        raise net_agentd_exception.NetAgentException("OSError")
    except etree.LxmlError:
        logging.critical(traceback.format_exc())
        raise net_agentd_exception.NetAgentException("LxmlError")
Пример #9
0
    def attach_rules(self, iface):
        """attach rules to iface
        :param:{"iface": which iface will be attach qos rules}
        """

        qos_rules = get_qos_rules(self._qos_name)

        for rule in qos_rules:
            if rule.direction == "in":
                option = ''
                if rule.average:
                    option = "ingress_policing_rate=%s" % rule.average
                if rule.burst:
                    option = option + ' ' + "ingress_policing_burst=%s" % rule.burst

                cmd_str = '%s set Interface %s %s' % (util.OVS_VSCTL_CMD,
                                                      iface,
                                                      option)
                ovs_cmd = envoy.run(cmd_str)
                if ovs_cmd.status_code != 0:
                    raise net_agentd_exception.NetAgentException("%s failed:\n%s" % (cmd_str, ovs_cmd.std_err))
            if rule.direction == "out":
                cmd_str = '%s set port %s qos=@%s -- \
                           --id=@%s create qos type=linux-htb other-config:max-rate=%s other-config:qosname=%s\
                           queues:%s=@%squeue -- \
                           --id=@%squeue create queue other-config:min-rate=%s other-config:qosname=%s' % (
                               util.OVS_VSCTL_CMD,
                               iface,
                               self._qos_name,
                               self._qos_name,
                               rule.average,
                               self._qos_name,
                               self.iface_to_ofport(iface),
                               iface,
                               iface,
                               rule.average,
                               self._qos_name)
                ovs_cmd = envoy.run(cmd_str)
                if ovs_cmd.status_code != 0:
                    raise net_agentd_exception.NetAgentException("%s failed:\n%s" % (cmd_str, ovs_cmd.std_err))

                cmd_str = '%s add-flow %s \"in_port=%s, actions=set_queue:%s, normal\"' % (
                    util.OVS_OFCTL_CMD,
                    self._vswitch_name,
                    self.iface_to_ofport(iface),
                    self.iface_to_ofport(iface))
                ovs_cmd = envoy.run(cmd_str)
                if ovs_cmd.status_code != 0:
                    raise net_agentd_exception.NetAgentException("%s failed:\n%s" % (cmd_str, ovs_cmd.std_err))
Пример #10
0
    def set_single_uplink_port(self, nic):
        nic_object = NicManager.NicHandle(nic)
        if nic_object.get_nic_type(
        ) != NicManager.NicType.PHYSICAL or nic_object.is_nic_free() is False:
            raise net_agentd_exception.NetAgentException(
                "nic is in use or not physical nic")

        cmd_str = '%s add-port %s %s' % (util.OVS_VSCTL_CMD,
                                         self._vswitch_name, nic)
        cmd = envoy.run(cmd_str)
        if cmd.status_code == 0:
            logging.info('%s success' % cmd_str)
        else:
            raise net_agentd_exception.NetAgentException(
                '%s failed \n%s' % (cmd_str, cmd.std_err))
Пример #11
0
    def set_mtu(self, mtu):
        min_mtu = 64
        max_mtu = 9216

        if int(mtu) < min_mtu or int(mtu) > max_mtu:
            raise net_agentd_exception.NetAgentException('The MTU range is incorrect')

        if self._class_net_path is not None:
            try:
                path = os.path.join(self._class_net_path, "mtu")
                sys_util.echo(path, mtu)
            except:
                raise net_agentd_exception.NetAgentException("%s set mtu failed" % self._nic_name)
        else:
            raise net_agentd_exception.NetAgentException("%s doesnt exist" % self._nic_name)
Пример #12
0
def get_filter_name(filter_name):
    name = ""

    path = os.path.join(util.NET_AGENT_CONF_DIR, filter_name + '.xml')
    if os.path.exists(path):
        xml_tree = util.ReadXml(path)
        name = util.GetXmlElementByXpath(xml_tree, 'filter_name').text
    else:
        raise net_agentd_exception.NetAgentException("%s doesnt exist" %
                                                     filter_name + '.xml')

    if name == "":
        raise net_agentd_exception.NetAgentException("%s filter_name is null" %
                                                     filter_name + '.xml')

    return name
Пример #13
0
def get_nic_list(nic_type):
    nic_list = []

    try:
        for nic_name in os.listdir(util.SYS_CLASS_NET):
            nic_object = NicHandle(nic_name)
            type = nic_object.get_nic_type()

            if nic_type == NicManager_pb2.NicListQueryRequest.ALL_PHY_NIC \
                    or nic_type == NicManager_pb2.NicListQueryRequest.FREE_PHY_NIC:

                is_free = nic_object.is_nic_free()

                if type != NicType.PHYSICAL:
                    continue

                if nic_type == NicManager_pb2.NicListQueryRequest.FREE_PHY_NIC:
                    if is_free is False:
                        continue

                nic_list.append(nic_name)

            if nic_type == NicManager_pb2.NicListQueryRequest.VIRTUAL_NIC:
                if type != NicType.VIRTUAL:
                    continue

                nic_list.append(nic_name)

    except:
        logging.error(traceback.format_exc())
        raise net_agentd_exception.NetAgentException("get_nic_list failed")

    return nic_list
Пример #14
0
def get_out_default_action(filter_name):
    action = ""

    path = os.path.join(util.NET_AGENT_CONF_DIR, filter_name + '.xml')
    if os.path.exists(path):
        xml_tree = util.ReadXml(path)
        action = util.GetXmlElementByXpath(xml_tree, 'in_default_action').text
    else:
        raise net_agentd_exception.NetAgentException("%s doesnt exist" %
                                                     filter_name + '.xml')

    if action != "accept" and action != "drop":
        raise net_agentd_exception.NetAgentException(
            "%s in_default_action is err" % filter_name + '.xml')

    return action
Пример #15
0
    def save_conf_file_item(self, key, value):
        try:
            with open(self._conf_file_path, mode='r', encoding='utf-8') as old_file:
                conf = old_file.readlines()
                old_file.close()
        except Exception:
            logging.error("failed to open file %s" % self._conf_file_path)
            return

        re_condition = r'^(%s).*\d+$' % key
        find = False
        for line in conf:
            if re.match(re_condition, line):
                if value == "":
                    del conf[conf.index(line)]
                else:
                    conf[conf.index(line)] = "%s=%s\n" % (key, value)
                find = True

        if find is False:
            conf.append("%s=%s\n" % (key, value))

        try:
            with open(self._conf_file_path, mode='w', encoding='utf-8') as new_file:
                for line in conf:
                    new_file.write(line)
            new_file.close()
        except Exception:
            raise net_agentd_exception.NetAgentException("failed to open file %s" % self._conf_file_path)
Пример #16
0
 def clear_ip(self):
     cmd_str = '%s addr flush dev %s' % (util.IP_CMD, self._nic_name)
     cmd = envoy.run(cmd_str)
     if cmd.status_code == 0:
         logging.info("%s clear ip success" % cmd_str)
     else:
         raise net_agentd_exception.NetAgentException("%s failed to run:\n%s" % (cmd_str, cmd.std_err))
Пример #17
0
def execute_remote_machine_cmd(ssh, cmd):
    stdin, stdout, stderr = ssh.exec_command(cmd)
    if stderr.read().decode() != "":
        raise net_agentd_exception.NetAgentException(
            "execute_remote_machine_cmd %s failed", cmd)
    else:
        return stdout.read().decode()
Пример #18
0
 def clear_conf_file(self):
     try:
         if(os.path.exists(self._conf_file_path)):
             with open(self._conf_file_path, mode='w', encoding='utf-8') as file:
                 file.truncate()
             file.close()
     except:
         raise net_agentd_exception.NetAgentException("cannot clear generate_conf_file %s" % self._conf_file_path)
Пример #19
0
def get_filter_type(filter_name):
    filter_type = ""

    path = os.path.join(util.NET_AGENT_CONF_DIR, filter_name + '.xml')
    if os.path.exists(path):
        xml_tree = util.ReadXml(path)
        filter_type = util.GetXmlElementByXpath(xml_tree, 'filter_type').text
        filter_type = int(filter_type)
    else:
        raise net_agentd_exception.NetAgentException("%s doesnt exist" %
                                                     filter_name + '.xml')

    if filter_type != AclManager_pb2.FILTER_BY_MAC and filter_type != AclManager_pb2.FILTER_BY_IP:
        raise net_agentd_exception.NetAgentException(
            "%s filter_type is error" % filter_name + '.xml')

    return filter_type
Пример #20
0
    def set_static_bond(self, conf):
        nic_list = ""

        if conf.bond_mode != UpLinkManager_pb2.UpLinkConf.BALANCE_SLB and \
                conf.bond_mode != UpLinkManager_pb2.UpLinkConf.BALANCE_TCP and \
                conf.bond_mode != UpLinkManager_pb2.UpLinkConf.ACTIVE_BACKUP:
            raise net_agentd_exception.NetAgentException(
                'The bond_mode range is incorrect')

        if conf.bond_mode == UpLinkManager_pb2.UpLinkConf.BALANCE_TCP:
            bond_mode = 'balance-tcp'
        if conf.bond_mode == UpLinkManager_pb2.UpLinkConf.BALANCE_SLB:
            bond_mode = 'balance-slb'
        if conf.bond_mode == UpLinkManager_pb2.UpLinkConf.ACTIVE_BACKUP:
            bond_mode = 'active-backup'

        for nic_name in conf.nic_names:
            nic_list = nic_list + ' ' + nic_name
        """
        :Because the difference between the active-backup mode and the other two mode is not clear,
        :so use two branches to solve this problem.
        """
        if conf.bond_mode == UpLinkManager_pb2.UpLinkConf.BALANCE_SLB or \
                conf.bond_mode == UpLinkManager_pb2.UpLinkConf.BALANCE_TCP:
            cmd_str = '%s add-bond %s %s %s lacp=off bond_mode=%s' % (
                util.OVS_VSCTL_CMD, self._vswitch_name, conf.bond_name,
                nic_list, bond_mode)

            cmd = envoy.run(cmd_str)
            if cmd.status_code == 0:
                logging.info('%s success' % cmd_str)
            else:
                raise net_agentd_exception.NetAgentException(
                    '%s failed \n%s' % (cmd_str, cmd.std_err))
        else:
            cmd_str = '%s add-bond %s %s %s lacp=off bond_mode=%s' % (
                util.OVS_VSCTL_CMD, self._vswitch_name, conf.bond_name,
                nic_list, bond_mode)

            cmd = envoy.run(cmd_str)
            if cmd.status_code == 0:
                logging.info('%s success' % cmd_str)
            else:
                raise net_agentd_exception.NetAgentException(
                    '%s failed \n%s' % (cmd_str, cmd.std_err))
Пример #21
0
    def generate_conf_file(self):
        try:
            with open(self._conf_file_path, mode='w', encoding='utf-8') as conf_file:
                conf_file.write("DEVICE=%s\n" % self._nic_name)
                conf_file.write("NAME=%s\n" % self._nic_name)
                conf_file.write("ONBOOT=yes\n")
                conf_file.write("BOOTPROTO=static\n")

                conf_file.close()
        except Exception:
            raise net_agentd_exception.NetAgentException("failed to generate_conf_file %s" % self._conf_file_path)
Пример #22
0
    def set_gateway(self, gateway):
        if re.match(r'((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}', gateway) is None:
            raise net_agentd_exception.NetAgentException("set_gateway: gateway error")

        cmd = envoy.run('%s route show' % util.IP_CMD)
        if cmd.status_code == 0:
            default_gateway_line_re = re.search('default via' + '.*', cmd.std_out)
            if default_gateway_line_re is not None:
                default_gateway_line = default_gateway_line_re.group()
                old_dev = re.search('dev .*', default_gateway_line)
                if old_dev != 'dev ' + self._nic_name:
                    logging.error("Disable setting up multiple default gateways")
                    return

        self.save_conf_file_item("GATEWAY", gateway)

        cmd = envoy.run('%s add default gw %s' % (util.ROUTE_CMD, gateway))
        if cmd.status_code == 0:
            logging.info("%s: setting default gateway success" % self._nic_name)
        else:
            raise net_agentd_exception.NetAgentException("%s: setting default gateway failed" % self._nic_name)
Пример #23
0
    def set_ip_netmask(self, ip_addr, netmask):
        if re.match(r'((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}', ip_addr) is None:
            raise net_agentd_exception.NetAgentException("set_ip_netmask: ip error")
        netmask_pattern = r'(((255\.){3}(255|254|252|248|240|224|192|128|0+))|' \
            r'((255\.){2}(255|254|252|248|240|224|192|128|0+)\.0)|' \
            r'((255\.)(255|254|252|248|240|224|192|128|0+)(\.0+){2})|' \
            r'((255|254|252|248|240|224|192|128|0+)(\.0+){3}))'
        if re.match(netmask_pattern, netmask) is None:
            raise net_agentd_exception.NetAgentException("set_ip_netmask: netmask error")

        self.save_conf_file_item("IPADDR", ip_addr)
        self.save_conf_file_item("NETMASK", netmask)

        self.clear_ip()

        cmd_str = '%s addr add %s/%s dev %s' % (util.IP_CMD, ip_addr, netmask, self._nic_name)
        cmd = envoy.run(cmd_str)
        if cmd.status_code == 0:
            logging.info("%s run success" % cmd_str)
        else:
            raise net_agentd_exception.NetAgentException("%s failed to run:\n%s" % (cmd_str, cmd.std_err))
Пример #24
0
    def destroy_vswitch(self):
        retval = False

        cmd_str = '%s del-br %s' % (util.OVS_VSCTL_CMD, self._vswitch_name)
        ovs_cmd = envoy.run(cmd_str)
        if ovs_cmd.status_code == 0:
            retval = True
        else:
            raise net_agentd_exception.NetAgentException(
                "%s failed to run:\n%s" % (cmd_str, ovs_cmd.std_err))

        return retval
Пример #25
0
    def get_interface_uuid(self, interface):
        uuid = ""

        cmd_str = '%s get interface %s _uuid' % (util.OVS_VSCTL_CMD, interface)
        cmd = envoy.run(cmd_str)
        if cmd.status_code == 0:
            uuid = cmd.std_out.strip('\n')
        else:
            raise net_agentd_exception.NetAgentException(
                '%s failed \n%s' % (cmd_str, cmd.std_err))

        return uuid
Пример #26
0
    def get_interface_name_by_uuid(self, uuid):
        name = ""

        cmd_str = '%s get interface %s name' % (util.OVS_VSCTL_CMD, uuid)
        cmd = envoy.run(cmd_str)
        if cmd.status_code == 0:
            name = cmd.std_out.strip('\n').strip("\"")
        else:
            raise net_agentd_exception.NetAgentException(
                '%s failed \n%s' % (cmd_str, cmd.std_err))

        return name
Пример #27
0
    def set_vlan(self, vlan_id):
        min_vlan = 1
        max_vlan = 4094

        if vlan_id == "":
            ovs_cmd = envoy.run('%s clear Port %s tag' %
                                (util.OVS_VSCTL_CMD, self._internal_nic))
            return

        if int(vlan_id) < min_vlan or int(vlan_id) > max_vlan:
            raise net_agentd_exception.NetAgentException(
                'The vlan id range is incorrect')

        cmd_str = '%s set Port %s tag=%s' % (util.OVS_VSCTL_CMD,
                                             self._internal_nic, vlan_id)
        ovs_cmd = envoy.run(cmd_str)
        if ovs_cmd.status_code == 0:
            logging.info("%s run success" % cmd_str)
        else:
            raise net_agentd_exception.NetAgentException(
                "%s failed to run:\n%s" % (cmd_str, ovs_cmd.std_err))
Пример #28
0
    def create_default_vswitch(cls):
        vswitch_conf = OvsManager_pb2.vSwitchConf()
        vswitch_conf.vswitch_name = cls.switch_name
        vswitch_conf.mode = OvsManager_pb2.vSwitchConf.VEB
        vswitch_conf.is_multicast = False

        response = cls.ovs_stub.AddvSwitch(
            OvsManager_pb2.vSwitchAddRequest(vswitch_conf=vswitch_conf))

        if response == ErrNo_pb2.SYS_FAIL:
            raise net_agentd_exception.NetAgentException(
                "create default vswitch failed")
Пример #29
0
    def get_raw_device(self):
        raw_device = ""

        if self._class_net_path is not None:
            raw_device_path = os.path.join(self._class_net_path, "device")
            real_raw_device_path = os.readlink(raw_device_path)
            raw_device_pattern = re.compile('[0-9a-zA-Z:.]{1,}')
            items = raw_device_pattern.findall(real_raw_device_path)
            raw_device = items[-1]
        else:
            raise net_agentd_exception.NetAgentException("%s doesnt exist" % self._nic_name)

        return raw_device
Пример #30
0
def check_qos_rules(rules):
    """Check the legality of the qos rules
    :param:{"rules": qos rules}
    """

    direction_list = []

    for rule in rules:
        direction = rule["direction"]
        if direction != "in" and direction != "out":
            raise net_agentd_exception.NetAgentException("direction %s error" % direction)
        if not rule["average"]:
            raise net_agentd_exception.NetAgentException("must have average")

        direction_list.append(direction)

    before_length = len(direction_list)
    direction_list = list(set(direction_list))
    after_length = len(direction_list)

    if before_length != after_length:
        raise net_agentd_exception.NetAgentException("rule have same direction")