Пример #1
0
def redirect(tokeniser):
    data = tokeniser()
    if data.count(':') == 1:
        prefix, suffix = data.split(':', 1)
        if prefix.count('.'):
            raise ValueError(
                'this format has been deprecaded as it does not make sense and it is not supported by other vendors'
            )

        asn = int(prefix)
        route_target = int(suffix)

        if asn >= pow(2, 32):
            raise ValueError('asn is a 32 bits number, value too large %s' %
                             asn)
        if asn >= pow(2, 16):
            if route_target >= pow(2, 16):
                raise ValueError(
                    'asn is a 32 bits number, route target can only be 16 bit %s'
                    % route_target)
            return NoNextHop, ExtendedCommunities().add(
                TrafficRedirectASN4(asn, route_target))
        if route_target >= pow(2, 32):
            raise ValueError(
                'route target is a 32 bits number, value too large %s' %
                route_target)
        return NoNextHop, ExtendedCommunities().add(
            TrafficRedirect(asn, route_target))
    else:
        return IP.create(data), ExtendedCommunities().add(
            TrafficNextHop(False))
Пример #2
0
def redirect(tokeniser):
    data = tokeniser()
    count = data.count(':')
    if count == 0:
        return IP.create(data), ExtendedCommunities().add(
            TrafficNextHopSimpson(False))
    if count == 1:
        prefix, suffix = data.split(':', 1)
        if prefix.count('.'):
            raise ValueError(
                'this format has been deprecated as it does not make sense and it is not supported by other vendors'
            )

        asn = int(prefix)
        route_target = int(suffix)

        if asn >= pow(2, 32):
            raise ValueError('asn is a 32 bits number, value too large %s' %
                             asn)
        if asn >= pow(2, 16):
            if route_target >= pow(2, 16):
                raise ValueError(
                    'asn is a 32 bits number, route target can only be 16 bit %s'
                    % route_target)
            return NoNextHop, ExtendedCommunities().add(
                TrafficRedirectASN4(asn, route_target))
        if route_target >= pow(2, 32):
            raise ValueError(
                'route target is a 32 bits number, value too large %s' %
                route_target)
        return NoNextHop, ExtendedCommunities().add(
            TrafficRedirect(asn, route_target))
    else:
        explicit_v6 = ']:' in data

        # ipv4
        if not explicit_v6 and data.count(':') == 1:
            return IP.create(data), ExtendedCommunities().add(
                TrafficNextHopSimpson(False))

        # ipv6 using []: notation
        if explicit_v6:
            ip, asn = data.split(']:')
            ip = ip.replace('[', '', 1)
            # FIXME: should this be 2^16 ??
            if asn >= pow(2, 32):
                raise ValueError(
                    'asn is a 32 bits number, value too large %s' % asn)
            return IP.create(ip), ExtendedCommunities().add(
                TrafficRedirectIPv6(ip, asn))

        raise ValueError(
            'it looks like you tried to use an IPv6 but did not enclose it in []'
        )
Пример #3
0
def extended_community(tokeniser):
    communities = ExtendedCommunities()

    value = tokeniser()
    if value == "[":
        while True:
            value = tokeniser()
            if value == "]":
                break
            communities.add(_extended_community(value))
    else:
        communities.add(_extended_community(value))

    return communities
Пример #4
0
def interface_set (tokeniser):
	communities = ExtendedCommunities()

	value = tokeniser()
	if value == '[':
		while True:
			value = tokeniser()
			if value == ']':
				break
			communities.add(_interface_set(value))
	else:
		communities.add(_interface_set(value))

	return communities
Пример #5
0
def rate_limit(tokeniser):
    # README: We are setting the ASN as zero as that what Juniper (and Arbor) did when we created a local flow route
    speed = int(tokeniser())
    if speed < 9600 and speed != 0:
        log.warning("rate-limiting flow under 9600 bytes per seconds may not work", 'configuration')
    if speed > 1000000000000:
        speed = 1000000000000
        log.warning("rate-limiting changed for 1 000 000 000 000 bytes from %s" % speed, 'configuration')
    return ExtendedCommunities().add(TrafficRate(ASN(0), speed))
Пример #6
0
def action(tokeniser):
    value = tokeniser()

    sample = 'sample' in value
    terminal = 'terminal' in value

    if not sample and not terminal:
        raise ValueError('invalid flow action')

    return ExtendedCommunities().add(TrafficAction(sample, terminal))
Пример #7
0
def mark(tokeniser):
    value = tokeniser()

    if not value.isdigit():
        raise ValueError('dscp is not a number')

    dscp_value = int(value)

    if dscp_value < 0 or dscp_value > 0b111111:
        raise ValueError('dscp is not a valid number')

    return ExtendedCommunities().add(TrafficMark(dscp_value))
Пример #8
0
    def extended_community(self, scope, name, command, tokens):
        attributes = scope[-1]['announce'][-1].attributes
        if Attribute.CODE.EXTENDED_COMMUNITY in attributes:
            extended_communities = attributes[
                Attribute.CODE.EXTENDED_COMMUNITY]
        else:
            extended_communities = ExtendedCommunities()
            attributes.add(extended_communities)

        extended_community = tokens.pop(0)
        try:
            if extended_community == '[':
                while True:
                    try:
                        extended_community = tokens.pop(0)
                    except IndexError:
                        return self.error.set(self.syntax)
                    if extended_community == ']':
                        break
                    extended_communities.add(
                        self._parse_extended_community(scope,
                                                       extended_community))
            else:
                extended_communities.add(
                    self._parse_extended_community(scope, extended_community))
        except ValueError:
            return self.error.set(self.syntax)
        return True
Пример #9
0
	def extended_community (self, scope, name, command, tokens):
		attributes = scope[-1]['announce'][-1].attributes
		if Attribute.CODE.EXTENDED_COMMUNITY in attributes:
			extended_communities = attributes[Attribute.CODE.EXTENDED_COMMUNITY]
		else:
			extended_communities = ExtendedCommunities()
			attributes.add(extended_communities)

		extended_community = tokens.pop(0)
		try:
			if extended_community == '[':
				while True:
					try:
						extended_community = tokens.pop(0)
					except IndexError:
						return self.error.set(self.syntax)
					if extended_community == ']':
						break
					extended_communities.add(self._parse_extended_community(scope,extended_community))
			else:
				extended_communities.add(self._parse_extended_community(scope,extended_community))
		except ValueError:
			return self.error.set(self.syntax)
		return True
Пример #10
0
def interface_set(tokeniser):
    communities = ExtendedCommunities()

    value = tokeniser()
    if value == '[':
        while True:
            value = tokeniser()
            if value == ']':
                break
            communities.add(_interface_set(value))
    else:
        communities.add(_interface_set(value))

    return communities
Пример #11
0
def extended_community(tokeniser):
    communities = ExtendedCommunities()

    value = tokeniser()
    if value == '[':
        while True:
            value = tokeniser()
            if value == ']':
                break
            communities.add(_extended_community(value))
    else:
        communities.add(_extended_community(value))

    return communities
Пример #12
0
def copy(tokeniser):
    return IP.create(tokeniser()), ExtendedCommunities().add(TrafficNextHopSimpson(True))
Пример #13
0
def redirect_next_hop_ietf(tokeniser):
    ip = IP.create(tokeniser())
    if ip.ipv4():
        return ExtendedCommunities().add(TrafficNextHopIPv4IETF(ip, False))
    else:
        return ExtendedCommunitiesIPv6().add(TrafficNextHopIPv6IETF(ip, False))
Пример #14
0
def redirect_next_hop(tokeniser):
    return ExtendedCommunities().add(TrafficNextHopSimpson(False))
Пример #15
0
def discard(tokeniser):
    # README: We are setting the ASN as zero as that what Juniper (and Arbor) did when we created a local flow route
    return ExtendedCommunities().add(TrafficRate(ASN(0), 0))