コード例 #1
0
ファイル: bridge_ctrl.py プロジェクト: ketor/easyOVS
def br_dump(bridge):
    """
    Dump the port information of a given bridges.
    """
    flows = br_getflows(bridge)
    debug('br_dump: len flows=%u\n' % len(flows))
    if flows:
        Flow.banner_output()
        for f in flows:
            f.fmt_output()
コード例 #2
0
ファイル: bridge_ctrl.py プロジェクト: MegaByte875/easyOVS
def br_dump(bridge_name):
    """
    Dump the port information of a given bridges.
    """
    flows = ovs_lib.OVSBridge(bridge_name).dump_flows()
    debug('br_dump: len flows=%u\n' % len(flows))
    if flows:
        Flow.banner_output()
        for f in flows:
            f.fmt_output()
コード例 #3
0
ファイル: bridge.py プロジェクト: yedan2010/easyOVS
 def dump_flows(self):
     """
     Dump out the flows of this bridge
     :return:
     """
     self.load_flows()
     debug('br_dump: len flows=%u\n' % len(self.flows))
     table = 0
     if self.flows:
         Flow.banner_output()
         for f in self.flows:
             if f.table != table:
                 output('\n')
                 table = f.table
             f.fmt_output()
コード例 #4
0
 def parse_flow(self, line):
     """
     Return a Flow or None, converted from a given line of original flow.
     """
     line = line.strip()
     table, packet, priority, match, actions = 0, 0, 0, '', ''
     if line.startswith('cookie='):
         table = get_num_after(line, 'table=')
         packet = get_num_after(line, 'n_packets=')
         if table is None or packet is None:
             return None
         for field in line.split():
             if field.startswith('priority='):  #priority of priority+match
                 priority = get_num_after(field, 'priority=')
                 if priority is None:
                     return None
                 match = field.replace('priority=%u' % priority,
                                       '').lstrip(',').strip()
                 if not match:
                     match = r'*'
             elif field.startswith('actions='):
                 actions = field.replace('actions=', '').rstrip('\n')
         if priority is None:  # There is no priority= field
             match = line.split()[len(line.split()) - 2]
         if len(match) >= 30:
             match.replace('vlan_tci', 'vlan')
             match = re.compile('0x0{1,}').sub('0x', match)
         return Flow(self.bridge, table, packet, priority, match, actions)
     else:
         return None
コード例 #5
0
ファイル: bridge.py プロジェクト: yedan2010/easyOVS
 def _parse_flow(self, line):
     """
     Return a Flow or None, converted from a given line of original flow.
     """
     line = line.strip()
     table, packet, priority, match, actions = 0, 0, 0, '', ''
     if line.startswith('cookie='):
         table = get_num_after(line, 'table=')
         packet = get_num_after(line, 'n_packets=')
         if table is None or packet is None:
             return None
         for field in line.split():
             if field.startswith('priority='):  # match starts with pri
                 priority = get_num_after(field, 'priority=')
                 if priority is None:
                     error('No priority field found in flow\n')
                     return None
                 match = \
                     field.replace('priority=%u'
                                   % priority, '').lstrip(',').strip()
                 if not match:
                     match = r'*'
                 port_no = get_num_after(field, 'in_port=')
                 if isinstance(port_no, int):
                     intf = self._get_port_intf(port_no)
                     if intf:
                         match = \
                             match.replace('in_port=%u'
                                           % port_no, 'in_port=%s' % intf)
             elif field.startswith('actions='):
                 actions = self._process_actions(field)
         if priority is None:  # There is no priority= field
             match = line.split()[len(line.split()) - 2]
         if len(match) >= 30:
             match.replace('vlan_tci', 'vlan')
             match = re.compile('0x0{1,}').sub('0x', match)
         return Flow(self.bridge, table, packet, priority, match, actions)
     else:
         return None
コード例 #6
0
ファイル: bridge.py プロジェクト: longsube/easyOVS
 def del_flow(self, flow_ids):
     """
     Return True or False to del a flow from given list.
     """
     if len(flow_ids) <= 0:
         return False
     if not self.flows:
         self.load_flows()
     del_flows = []
     fd = sys.stdin.fileno()
     old = termios.tcgetattr(fd)
     for flow_id in flow_ids:
         if isinstance(flow_id, str) and flow_id.isdigit():
             flow_id = int(flow_id)
         else:
             continue
         if flow_id >= len(self.flows):
             continue
         else:
             del_flow = self.flows[flow_id]
             Flow.banner_output()
             del_flow.fmt_output()
             output('Del the flow? [Y/n]: ')
             new = termios.tcgetattr(fd)
             new[3] = new[3] & ~termios.ICANON
             try:
                 termios.tcsetattr(fd, termios.TCSADRAIN, new)
                 while True:
                     in_ch = sys.stdin.read(1)
                     if in_ch == 'n' or in_ch == 'N':
                         output('\tCancel the deletion.\n')
                         break
                     elif in_ch == 'y' or in_ch == 'Y' or in_ch != '\n':
                         del_flows.append(del_flow)
                         output('\n')
                         break
                     else:
                         output('\nWrong, please input [Y/n]: ')
                         continue
             finally:
                 termios.tcsetattr(fd, termios.TCSADRAIN, old)
     if not del_flows:
         return False
     self.load_flows(True)
     flows_db_new = self.flows_db + '.new'
     f, f_new = open(self.flows_db, 'r'), open(flows_db_new, 'w')
     while True:
         lines = f.readlines(1000)
         if not lines:
             break
         for line in lines:
             flow = self.parse_flow(line)
             if flow not in del_flows:
                 f_new.write('%s' % line)
             else:
                 debug("Del the flow:\n")
                 #del_flow.fmt_output()
     f.close()
     f_new.close()
     replace_cmd = "ovs-ofctl replace-flows %s %s" % (self.bridge, flows_db_new)
     error = Popen(replace_cmd, stdout=PIPE, stderr=PIPE, shell=True).communicate()[1]
     if error:
         output(error)
         return False
     else:
         self.load_flows()
         return True
コード例 #7
0
 def del_flow(self, flow_ids):
     """
     Return True or False to del a flow from given list.
     """
     if len(flow_ids) <= 0:
         return False
     if not self.flows:
         self.load_flows()
     del_flows = []
     fd = sys.stdin.fileno()
     old = termios.tcgetattr(fd)
     for flow_id in flow_ids:
         if isinstance(flow_id, str) and flow_id.isdigit():
             flow_id = int(flow_id)
         else:
             continue
         if flow_id >= len(self.flows):
             continue
         else:
             del_flow = self.flows[flow_id]
             Flow.banner_output()
             del_flow.fmt_output()
             output('Del the flow? [Y/n]: ')
             new = termios.tcgetattr(fd)
             new[3] = new[3] & ~termios.ICANON
             try:
                 termios.tcsetattr(fd, termios.TCSADRAIN, new)
                 while True:
                     in_ch = sys.stdin.read(1)
                     if in_ch == 'n' or in_ch == 'N':
                         output('\tCancel the deletion.\n')
                         break
                     elif in_ch == 'y' or in_ch == 'Y' or in_ch != '\n':
                         del_flows.append(del_flow)
                         output('\n')
                         break
                     else:
                         output('\nWrong, please input [Y/n]: ')
                         continue
             finally:
                 termios.tcsetattr(fd, termios.TCSADRAIN, old)
     if not del_flows:
         return False
     self.load_flows(True)
     flows_db_new = self.flows_db + '.new'
     f, f_new = open(self.flows_db, 'r'), open(flows_db_new, 'w')
     while True:
         lines = f.readlines(1000)
         if not lines:
             break
         for line in lines:
             flow = self.parse_flow(line)
             if flow not in del_flows:
                 f_new.write('%s' % line)
             else:
                 debug("Del the flow:\n")
                 #del_flow.fmt_output()
     f.close()
     f_new.close()
     replace_cmd = "ovs-ofctl replace-flows %s %s" % (self.bridge,
                                                      flows_db_new)
     error = Popen(replace_cmd, stdout=PIPE, stderr=PIPE,
                   shell=True).communicate()[1]
     if error:
         output(error)
         return False
     else:
         self.load_flows()
         return True
コード例 #8
0
ファイル: bridge.py プロジェクト: lafenicecc/easyOVS
 def del_flow(self, flow_ids, forced=False):
     """
     Return True or False to del a flow from given list.
     """
     if len(flow_ids) <= 0:
         return False
     if not self.flows:
         self.load_flows()
     del_flows = []
     fd = sys.stdin.fileno()
     old = termios.tcgetattr(fd)
     for flow_id in flow_ids:
         if isinstance(flow_id, str) and flow_id.isdigit():
             flow_id = int(flow_id)
         else:
             continue
         if flow_id >= len(self.flows):
             continue
         else:
             del_flow = self.flows[flow_id]
             if forced:
                 del_flows.append(del_flow)
             else:
                 Flow.banner_output()
                 del_flow.fmt_output()
                 output('Del the flow? [Y/n]: ')
                 new = termios.tcgetattr(fd)
                 new[3] = new[3] & ~termios.ICANON
                 try:
                     termios.tcsetattr(fd, termios.TCSADRAIN, new)
                     while True:
                         in_ch = sys.stdin.read(1)
                         if in_ch == 'n' or in_ch == 'N':
                             output('\tCancel the deletion.\n')
                             break
                         elif in_ch == 'y' or in_ch == 'Y' or in_ch != '\n':
                             del_flows.append(del_flow)
                             output('\n')
                             break
                         else:
                             output('\nWrong, please input [Y/n]: ')
                             continue
                 finally:
                     termios.tcsetattr(fd, termios.TCSADRAIN, old)
     if not del_flows:
         return False
     self.load_flows(True)
     f = open(self.flows_db, 'r')
     while True:
         lines = f.readlines(1000)
         if not lines:
             break
         for line in lines:
             flow = self._parse_flow(line)
             if flow in del_flows:
                 del_matches = line.replace(',', ' ').split()
                 del_matches = \
                     filter(lambda m: not (m.startswith("cookie=")
                            or m.startswith("actions=")), del_matches)
                 del_cmd = "ovs-ofctl --strict del-flows %s %s" \
                           % (self.bridge, ','.join(del_matches))
                 err = Popen(del_cmd, stdout=PIPE, stderr=PIPE,
                             shell=True).communicate()[1]
                 if err:
                     error("Error when delflow <%s> in bridge %s\n"
                           % (','.join(del_matches), self.bridge))
                     error(err)
                     return False
     f.close()
     self.load_flows()
     return True
コード例 #9
0
ファイル: bridge.py プロジェクト: yedan2010/easyOVS
 def del_flow(self, flow_ids, forced=False):
     """
     Return True or False to del a flow from given list.
     """
     if len(flow_ids) <= 0:
         return False
     if not self.flows:
         self.load_flows()
     del_flows = []
     fd = sys.stdin.fileno()
     old = termios.tcgetattr(fd)
     for flow_id in flow_ids:
         if isinstance(flow_id, str) and flow_id.isdigit():
             flow_id = int(flow_id)
         else:
             continue
         if flow_id >= len(self.flows):
             continue
         else:
             del_flow = self.flows[flow_id]
             if forced:
                 del_flows.append(del_flow)
             else:
                 Flow.banner_output()
                 del_flow.fmt_output()
                 output('Del the flow? [Y/n]: ')
                 new = termios.tcgetattr(fd)
                 new[3] = new[3] & ~termios.ICANON
                 try:
                     termios.tcsetattr(fd, termios.TCSADRAIN, new)
                     while True:
                         in_ch = sys.stdin.read(1)
                         if in_ch == 'n' or in_ch == 'N':
                             output('\tCancel the deletion.\n')
                             break
                         elif in_ch == 'y' or in_ch == 'Y' or in_ch != '\n':
                             del_flows.append(del_flow)
                             output('\n')
                             break
                         else:
                             output('\nWrong, please input [Y/n]: ')
                             continue
                 finally:
                     termios.tcsetattr(fd, termios.TCSADRAIN, old)
     if not del_flows:
         return False
     self.load_flows(True)
     f = open(self.flows_db, 'r')
     while True:
         lines = f.readlines(1000)
         if not lines:
             break
         for line in lines:
             flow = self._parse_flow(line)
             if flow in del_flows:
                 del_matches = line.replace(',', ' ').split()
                 del_matches = \
                     filter(lambda m: not (m.startswith("cookie=")
                            or m.startswith("actions=")), del_matches)
                 del_cmd = "ovs-ofctl --strict del-flows %s %s" \
                           % (self.bridge, ','.join(del_matches))
                 err = Popen(del_cmd, stdout=PIPE, stderr=PIPE,
                             shell=True).communicate()[1]
                 if err:
                     error("Error when delflow <%s> in bridge %s\n"
                           % (','.join(del_matches), self.bridge))
                     error(err)
                     return False
     f.close()
     self.load_flows()
     return True