Esempio n. 1
0
def to_match_masked_int(value):
    if isinstance(value, str) and '/' in value:
        value = value.split('/')
        return (ofctl_utils.str_to_int(value[0]),
                ofctl_utils.str_to_int(value[1]))
    else:
        return ofctl_utils.str_to_int(value)
Esempio n. 2
0
def to_instructions(dp, insts):
    instructions = []
    ofp = dp.ofproto
    parser = dp.ofproto_parser

    for i in insts:
        inst_type = i.get('type')
        if inst_type in ['APPLY_ACTIONS', 'WRITE_ACTIONS']:
            dics = i.get('actions', [])
            actions = _get_actions(dp, dics)
            if actions:
                if inst_type == 'APPLY_ACTIONS':
                    instructions.append(
                        parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS,
                                                     actions))
                else:
                    instructions.append(
                        parser.OFPInstructionActions(ofp.OFPIT_WRITE_ACTIONS,
                                                     actions))
        elif inst_type == 'CLEAR_ACTIONS':
            instructions.append(
                parser.OFPInstructionActions(ofp.OFPIT_CLEAR_ACTIONS, []))
        elif inst_type == 'GOTO_TABLE':
            table_id = int(i.get('table_id'))
            instructions.append(parser.OFPInstructionGotoTable(table_id))
        elif inst_type == 'WRITE_METADATA':
            metadata = ofctl_utils.str_to_int(i.get('metadata'))
            metadata_mask = (ofctl_utils.str_to_int(i['metadata_mask'])
                             if 'metadata_mask' in i else parser.UINT64_MAX)
            instructions.append(
                parser.OFPInstructionWriteMetadata(metadata, metadata_mask))
        else:
            LOG.error('Unknown instruction type: %s', inst_type)

    return instructions
Esempio n. 3
0
def to_match_masked_int(value):
    if isinstance(value, str) and '/' in value:
        value = value.split('/')
        return (ofctl_utils.str_to_int(value[0]),
                ofctl_utils.str_to_int(value[1]))
    else:
        return ofctl_utils.str_to_int(value)
Esempio n. 4
0
def to_instructions(dp, insts):
    instructions = []
    ofp = dp.ofproto
    parser = dp.ofproto_parser

    for i in insts:
        inst_type = i.get("type")
        if inst_type in ["APPLY_ACTIONS", "WRITE_ACTIONS"]:
            dics = i.get("actions", [])
            actions = _get_actions(dp, dics)
            if actions:
                if inst_type == "APPLY_ACTIONS":
                    instructions.append(parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions))
                else:
                    instructions.append(parser.OFPInstructionActions(ofp.OFPIT_WRITE_ACTIONS, actions))
        elif inst_type == "CLEAR_ACTIONS":
            instructions.append(parser.OFPInstructionActions(ofp.OFPIT_CLEAR_ACTIONS, []))
        elif inst_type == "GOTO_TABLE":
            table_id = int(i.get("table_id"))
            instructions.append(parser.OFPInstructionGotoTable(table_id))
        elif inst_type == "WRITE_METADATA":
            metadata = ofctl_utils.str_to_int(i.get("metadata"))
            metadata_mask = ofctl_utils.str_to_int(i["metadata_mask"]) if "metadata_mask" in i else parser.UINT64_MAX
            instructions.append(parser.OFPInstructionWriteMetadata(metadata, metadata_mask))
        elif inst_type == "METER":
            meter_id = int(i.get("meter_id"))
            instructions.append(parser.OFPInstructionMeter(meter_id))
        else:
            LOG.error("Unknown instruction type: %s", inst_type)

    return instructions
Esempio n. 5
0
    def ct(cls, ofproto, action_str):
        str_to_port = {'ftp': 21, 'tftp': 69}
        flags = 0
        zone_src = ""
        zone_ofs_nbits = 0
        recirc_table = nicira_ext.NX_CT_RECIRC_NONE
        alg = 0
        ct_actions = []

        if len(action_str) > 2:
            if (not action_str.startswith('ct(') or action_str[-1] != ')'):
                raise ryu.exception.OFPInvalidActionString(
                    action_str=action_str)
            rest = tokenize_ofp_instruction_arg(action_str[len('ct('):-1])
        else:
            rest = []
        for arg in rest:
            if arg == 'commit':
                flags |= nicira_ext.NX_CT_F_COMMIT
                rest = rest[len('commit'):]
            elif arg == 'force':
                flags |= nicira_ext.NX_CT_F_FORCE
            elif arg.startswith('exec('):
                ct_actions = ofp_instruction_from_str(ofproto,
                                                      arg[len('exec('):-1])
            else:
                try:
                    k, v = arg.split('=', 1)
                    if k == 'table':
                        recirc_table = str_to_int(v)
                    elif k == 'zone':
                        m = re.search('\[(\d*)\.\.(\d*)\]', v)
                        if m:
                            zone_ofs_nbits = nicira_ext.ofs_nbits(
                                int(m.group(1)), int(m.group(2)))
                            zone_src = nxm_field_name_to_ryu(v[:m.start(0)])
                        else:
                            zone_ofs_nbits = str_to_int(v)
                    elif k == 'alg':
                        alg = str_to_port[arg[len('alg='):]]
                except Exception:
                    raise ryu.exception.OFPInvalidActionString(
                        action_str=action_str)
        return dict(
            NXActionCT={
                'flags': flags,
                'zone_src': zone_src,
                'zone_ofs_nbits': zone_ofs_nbits,
                'recirc_table': recirc_table,
                'alg': alg,
                'actions': ct_actions
            })
Esempio n. 6
0
    def ct(cls, ofproto, action_str):
        str_to_port = {'ftp': 21, 'tftp': 69}
        flags = 0
        zone_src = ""
        zone_ofs_nbits = 0
        recirc_table = nicira_ext.NX_CT_RECIRC_NONE
        alg = 0
        ct_actions = []

        if len(action_str) > 2:
            if (not action_str.startswith('ct(') or
                    action_str[-1] != ')'):
                raise ryu.exception.OFPInvalidActionString(
                    action_str=action_str)
            rest = tokenize_ofp_instruction_arg(action_str[len('ct('):-1])
        else:
            rest = []
        for arg in rest:
            if arg == 'commit':
                flags |= nicira_ext.NX_CT_F_COMMIT
                rest = rest[len('commit'):]
            elif arg == 'force':
                flags |= nicira_ext.NX_CT_F_FORCE
            elif arg.startswith('exec('):
                ct_actions = ofp_instruction_from_str(
                    ofproto, arg[len('exec('):-1])
            else:
                try:
                    k, v = arg.split('=', 1)
                    if k == 'table':
                        recirc_table = str_to_int(v)
                    elif k == 'zone':
                        m = re.search('\[(\d*)\.\.(\d*)\]', v)
                        if m:
                            zone_ofs_nbits = nicira_ext.ofs_nbits(
                                int(m.group(1)), int(m.group(2)))
                            zone_src = nxm_field_name_to_ryu(
                                v[:m.start(0)])
                        else:
                            zone_ofs_nbits = str_to_int(v)
                    elif k == 'alg':
                        alg = str_to_port[arg[len('alg='):]]
                except Exception:
                    raise ryu.exception.OFPInvalidActionString(
                        action_str=action_str)
        return dict(NXActionCT={'flags': flags,
                                'zone_src': zone_src,
                                'zone_ofs_nbits': zone_ofs_nbits,
                                'recirc_table': recirc_table,
                                'alg': alg,
                                'actions': ct_actions})
Esempio n. 7
0
def to_actions(dp, acts):
    inst = []
    actions = []
    ofp = dp.ofproto
    parser = dp.ofproto_parser

    for a in acts:
        action = to_action(dp, a)

        if action is not None:
            actions.append(action)
        else:
            action_type = a.get('type')
            if action_type == 'WRITE_ACTIONS':
                write_actions = []
                write_acts = a.get('actions')
                for act in write_acts:
                    action = to_action(dp, act)
                    if action is not None:
                        write_actions.append(action)
                    else:
                        LOG.error('Unknown action type: %s', action_type)
                if write_actions:
                    inst.append(
                        parser.OFPInstructionActions(ofp.OFPIT_WRITE_ACTIONS,
                                                     write_actions))
            elif action_type == 'CLEAR_ACTIONS':
                inst.append(
                    parser.OFPInstructionActions(ofp.OFPIT_CLEAR_ACTIONS, []))
            elif action_type == 'GOTO_TABLE':
                table_id = UTIL.ofp_table_from_user(a.get('table_id'))
                inst.append(parser.OFPInstructionGotoTable(table_id))
            elif action_type == 'WRITE_METADATA':
                metadata = ofctl_utils.str_to_int(a.get('metadata'))
                metadata_mask = (ofctl_utils.str_to_int(a['metadata_mask'])
                                 if 'metadata_mask' in a
                                 else parser.UINT64_MAX)
                inst.append(
                    parser.OFPInstructionWriteMetadata(
                        metadata, metadata_mask))
            elif action_type == 'METER':
                meter_id = UTIL.ofp_meter_from_user(a.get('meter_id'))
                inst.append(parser.OFPInstructionMeter(meter_id))
            else:
                LOG.error('Unknown action type: %s', action_type)

    if actions:
        inst.append(parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS,
                                                 actions))
    return inst
Esempio n. 8
0
 def set_field(cls, ofproto, action_str):
     try:
         assert action_str.startswith("set_field:")
         value, key = action_str[len("set_field:"):].split("->", 1)
         fieldarg = dict(field=ofp_ofctl_field_name_to_ryu(key))
         m = value.find('/')
         if m >= 0:
             fieldarg['value'] = str_to_int(value[:m])
             fieldarg['mask'] = str_to_int(value[m + 1:])
         else:
             fieldarg['value'] = str_to_int(value)
     except Exception:
         raise ryu.exception.OFPInvalidActionString(action_str=action_str)
     return dict(OFPActionSetField={'field': {'OXMTlv': fieldarg}})
Esempio n. 9
0
def to_actions(dp, acts):
    inst = []
    actions = []
    ofp = dp.ofproto
    parser = dp.ofproto_parser

    for a in acts:
        action = to_action(dp, a)

        if action is not None:
            actions.append(action)
        else:
            action_type = a.get('type')
            if action_type == 'WRITE_ACTIONS':
                write_actions = []
                write_acts = a.get('actions')
                for a in write_acts:
                    action = to_action(dp, a)
                    if action is not None:
                        write_actions.append(action)
                    else:
                        LOG.error('Unknown action type: %s', action_type)
                if write_actions:
                    inst.append(
                        parser.OFPInstructionActions(ofp.OFPIT_WRITE_ACTIONS,
                                                     write_actions))
            elif action_type == 'CLEAR_ACTIONS':
                inst.append(parser.OFPInstructionActions(
                            ofp.OFPIT_CLEAR_ACTIONS, []))
            elif action_type == 'GOTO_TABLE':
                table_id = UTIL.ofp_table_from_user(a.get('table_id'))
                inst.append(parser.OFPInstructionGotoTable(table_id))
            elif action_type == 'WRITE_METADATA':
                metadata = ofctl_utils.str_to_int(a.get('metadata'))
                metadata_mask = (ofctl_utils.str_to_int(a['metadata_mask'])
                                 if 'metadata_mask' in a
                                 else parser.UINT64_MAX)
                inst.append(
                    parser.OFPInstructionWriteMetadata(
                        metadata, metadata_mask))
            elif action_type == 'METER':
                meter_id = UTIL.ofp_meter_from_user(a.get('meter_id'))
                inst.append(parser.OFPInstructionMeter(meter_id))
            else:
                LOG.error('Unknown action type: %s', action_type)

    if actions:
        inst.append(parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS,
                                                 actions))
    return inst
Esempio n. 10
0
 def set_field(cls, ofproto, action_str):
     try:
         assert action_str.startswith("set_field:")
         value, key = action_str[len("set_field:"):].split("->", 1)
         fieldarg = dict(field=ofp_ofctl_field_name_to_ryu(key))
         m = value.find('/')
         if m >= 0:
             fieldarg['value'] = str_to_int(value[:m])
             fieldarg['mask'] = str_to_int(value[m + 1:])
         else:
             fieldarg['value'] = str_to_int(value)
     except Exception:
         raise ryu.exception.OFPInvalidActionString(action_str=action_str)
     return dict(OFPActionSetField={
         'field': {'OXMTlv': fieldarg}})
Esempio n. 11
0
 def output(cls, ofproto, action_str):
     if action_str == 'normal':
         port = ofproto.OFPP_NORMAL
     else:
         assert action_str.startswith('output:')
         port = str_to_int(action_str[len('output:'):])
     return dict(OFPActionOutput={'port': port})
Esempio n. 12
0
 def output(cls, ofproto, action_str):
     if action_str == 'normal':
         port = ofproto.OFPP_NORMAL
     else:
         assert action_str.startswith('output:')
         port = str_to_int(action_str[len('output:'):])
     return dict(OFPActionOutput={'port': port})
Esempio n. 13
0
 def resubmit(cls, ofproto, action_str):
     arg = action_str[len("resubmit"):]
     kwargs = {}
     try:
         if arg[0] == ':':
             kwargs['in_port'] = str_to_int(arg[1:])
         elif arg[0] == '(' and arg[-1] == ')':
             in_port, table_id = arg[1:-1].split(',')
             if in_port:
                 kwargs['in_port'] = str_to_int(in_port)
             if table_id:
                 kwargs['table_id'] = str_to_int(table_id)
         else:
             raise Exception
         return dict(NXActionResubmitTable=kwargs)
     except Exception:
         raise ryu.exception.OFPInvalidActionString(action_str=action_str)
Esempio n. 14
0
 def resubmit(cls, ofproto, action_str):
     arg = action_str[len("resubmit"):]
     kwargs = {}
     try:
         if arg[0] == ':':
             kwargs['in_port'] = str_to_int(arg[1:])
         elif arg[0] == '(' and arg[-1] == ')':
             in_port, table_id = arg[1:-1].split(',')
             if in_port:
                 kwargs['in_port'] = str_to_int(in_port)
             if table_id:
                 kwargs['table_id'] = str_to_int(table_id)
         else:
             raise Exception
         return dict(NXActionResubmitTable=kwargs)
     except Exception:
         raise ryu.exception.OFPInvalidActionString(
             action_str=action_str)
Esempio n. 15
0
    def add_mac_table(self, req, **kwargs):
        simple_switch = self.simple_switch_app
        hostname1 = str_to_int(kwargs['hostname1'])
        hostname2 = str_to_int(kwargs['hostname2'])
        route = Route.get(Route.hostname1 == hostname1)

        if hostname1 == route.hostname1 and hostname2 == route.hostname2:
            flow = Flow.filter(Flow.route_id == 1).execute()
            for f in flow:
                simple_switch.get_flow(f.in_port1, f.vlan1, f.out_port1,
                                       f.in_port2, f.vlan2, f.out_port2)

            for f in flow:
                if f.in_port1 != 4 and route.flg != 0:
                    simple_switch.set2_flow(f.in_port1, f.vlan1, f.out_port1,
                                            f.in_port2, f.vlan2, f.out_port2)
                elif f.in_port1 != 3 and route.flg != 1:
                    simple_switch.set1_flow(f.in_port1, f.vlan1, f.out_port1)

            simple_switch.flg_update(hostname1)
Esempio n. 16
0
def to_instructions(dp, insts):
    instructions = []
    ofp = dp.ofproto
    parser = dp.ofproto_parser

    for i in insts:
        inst_type = i.get('type')
        if inst_type in ['APPLY_ACTIONS', 'WRITE_ACTIONS']:
            dics = i.get('actions', [])
            actions = _get_actions(dp, dics)
            if actions:
                if inst_type == 'APPLY_ACTIONS':
                    instructions.append(
                        parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS,
                                                     actions))
                else:
                    instructions.append(
                        parser.OFPInstructionActions(ofp.OFPIT_WRITE_ACTIONS,
                                                     actions))
        elif inst_type == 'CLEAR_ACTIONS':
            instructions.append(
                parser.OFPInstructionActions(ofp.OFPIT_CLEAR_ACTIONS, []))
        elif inst_type == 'GOTO_TABLE':
            table_id = int(i.get('table_id'))
            instructions.append(parser.OFPInstructionGotoTable(table_id))
        elif inst_type == 'WRITE_METADATA':
            metadata = ofctl_utils.str_to_int(i.get('metadata'))
            metadata_mask = (ofctl_utils.str_to_int(i['metadata_mask'])
                             if 'metadata_mask' in i
                             else parser.UINT64_MAX)
            instructions.append(
                parser.OFPInstructionWriteMetadata(
                    metadata, metadata_mask))
        elif inst_type == 'METER':
            meter_id = int(i.get('meter_id'))
            instructions.append(parser.OFPInstructionMeter(meter_id))
        else:
            LOG.error('Unknown instruction type: %s', inst_type)

    return instructions
Esempio n. 17
0
 def conjunction(cls, ofproto, action_str):
     try:
         assert action_str.startswith('conjunction(')
         assert action_str[-1] == ')'
         args = action_str[len('conjunction('):-1].split(',')
         assert len(args) == 2
         id_ = str_to_int(args[0])
         clauses = list(map(str_to_int, args[1].split('/')))
         assert len(clauses) == 2
         return dict(NXActionConjunction={
             'clause': clauses[0] - 1,
             'n_clauses': clauses[1],
             'id': id_
         })
     except Exception:
         raise ryu.exception.OFPInvalidActionString(action_str=action_str)
Esempio n. 18
0
 def conjunction(cls, ofproto, action_str):
     try:
         assert action_str.startswith('conjunction(')
         assert action_str[-1] == ')'
         args = action_str[len('conjunction('):-1].split(',')
         assert len(args) == 2
         id_ = str_to_int(args[0])
         clauses = list(map(str_to_int, args[1].split('/')))
         assert len(clauses) == 2
         return dict(NXActionConjunction={
             'clause': clauses[0] - 1,
             'n_clauses': clauses[1],
             'id': id_})
     except Exception:
         raise ryu.exception.OFPInvalidActionString(
             action_str=action_str)
Esempio n. 19
0
 def goto_table(cls, ofproto, action_str):
     assert action_str.startswith('goto_table:')
     table_id = str_to_int(action_str[len('goto_table:'):])
     return dict(OFPInstructionGotoTable={'table_id': table_id})
Esempio n. 20
0
 def goto_table(cls, ofproto, action_str):
     assert action_str.startswith('goto_table:')
     table_id = str_to_int(action_str[len('goto_table:'):])
     return dict(OFPInstructionGotoTable={'table_id': table_id})
Esempio n. 21
0
 def _test_str_to_int(self, input_value, expected_value):
     output_value = ofctl_utils.str_to_int(input_value)
     self.assertEqual(expected_value, output_value)
Esempio n. 22
0
 def _test_str_to_int(self, input_value, expected_value):
     output_value = ofctl_utils.str_to_int(input_value)
     self.assertEqual(expected_value, output_value)