Java Code Examples for org.onlab.packet.IpPrefix#isIp4()

The following examples show how to use org.onlab.packet.IpPrefix#isIp4() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleFabricRouting.java    From onos with Apache License 2.0 6 votes vote down vote up
private FlowRule generateInterceptFlowRule(boolean isDstLocalSubnet, DeviceId deviceId, IpPrefix prefix) {
    TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
    if (prefix.isIp4()) {
        selector.matchEthType(Ethernet.TYPE_IPV4);
        if (prefix.prefixLength() > 0) {
            selector.matchIPDst(prefix);
        }
    } else {
        selector.matchEthType(Ethernet.TYPE_IPV6);
        if (prefix.prefixLength() > 0) {
            selector.matchIPv6Dst(prefix);
        }
    }
    FlowRule rule = DefaultFlowRule.builder()
            .forDevice(deviceId)
            .withPriority(reactivePriority(false, isDstLocalSubnet, prefix.prefixLength()))
            .withSelector(selector.build())
            .withTreatment(DefaultTrafficTreatment.builder().punt().build())
            .fromApp(appId)
            .makePermanent()
            .forTable(0).build();
    return rule;
}
 
Example 2
Source File: SdnIpFib.java    From onos with Apache License 2.0 6 votes vote down vote up
private TrafficSelector.Builder buildIngressTrafficSelector(Interface intf, IpPrefix prefix) {
    TrafficSelector.Builder selector = buildTrafficSelector(intf);

    // Match the destination IP prefix at the first hop
    if (prefix.isIp4()) {
        selector.matchEthType(Ethernet.TYPE_IPV4);
        // if it is default route, then we do not need match destination
        // IP address
        if (prefix.prefixLength() != 0) {
            selector.matchIPDst(prefix);
        }
    } else {
        selector.matchEthType(Ethernet.TYPE_IPV6);
        // if it is default route, then we do not need match destination
        // IP address
        if (prefix.prefixLength() != 0) {
            selector.matchIPv6Dst(prefix);
        }
    }
    return selector;
}
 
Example 3
Source File: IpConcurrentRadixTree.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public V putIfAbsent(IpPrefix prefix, V value) {

    String prefixString = getPrefixString(prefix);

    if (prefix.isIp4()) {
        return ipv4Tree.put(prefixString, value);
    }

    if (prefix.isIp6()) {
        return ipv6Tree.put(prefixString, value);
    }

    return null;
}
 
Example 4
Source File: RoutingRulePopulator.java    From onos with Apache License 2.0 5 votes vote down vote up
private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
    TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
    // If the prefix is IPv4
    if (prefixToMatch.isIp4()) {
        selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
        selectorBuilder.matchIPDst(prefixToMatch.getIp4Prefix());
        return selectorBuilder;
    }
    // If the prefix is IPv6
    selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
    selectorBuilder.matchIPv6Dst(prefixToMatch.getIp6Prefix());
    return selectorBuilder;
}
 
Example 5
Source File: SimpleFabricRouting.java    From onos with Apache License 2.0 5 votes vote down vote up
private FlowRule generateLocalSubnetIpBctFlowRule(DeviceId deviceId, IpPrefix prefix, FabricNetwork fabricNetwork) {
    TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
    IpPrefix bctPrefix;
    if (prefix.isIp4()) {
        bctPrefix = Ip4Prefix.valueOf(prefix.getIp4Prefix().address().toInt() |
                                          ~Ip4Address.makeMaskPrefix(prefix.prefixLength()).toInt(),
                                      Ip4Address.BIT_LENGTH);
        selector.matchEthType(Ethernet.TYPE_IPV4);
        selector.matchIPDst(bctPrefix);
    } else {
        byte[] p = prefix.getIp6Prefix().address().toOctets();
        byte[] m = Ip6Address.makeMaskPrefix(prefix.prefixLength()).toOctets();
        for (int i = 0; i < p.length; i++) {
             p[i] |= ~m[i];
        }
        bctPrefix = Ip6Prefix.valueOf(p, Ip6Address.BIT_LENGTH);
        selector.matchEthType(Ethernet.TYPE_IPV6);
        selector.matchIPv6Dst(bctPrefix);
    }
    TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
    Set<ConnectPoint> newEgressPoints = new HashSet<>();
    for (Interface iface : fabricNetwork.interfaces()) {
        if (iface.connectPoint().deviceId().equals(deviceId)) {
            treatment.setOutput(iface.connectPoint().port());
        }
    }
    FlowRule rule = DefaultFlowRule.builder()
            .forDevice(deviceId)
            .withPriority(reactivePriority(true, true, bctPrefix.prefixLength()))
            .withSelector(selector.build())
            .withTreatment(treatment.build())
            .fromApp(appId)
            .makePermanent()
            .forTable(0).build();
    return rule;
}
 
Example 6
Source File: IntentSynchronizerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * MultiPointToSinglePointIntent builder.
 *
 * @param ipPrefix the ipPrefix to match
 * @param nextHopMacAddress to which the destination MAC address in packet
 * should be rewritten
 * @param egressPoint to which packets should be sent
 * @return the constructed MultiPointToSinglePointIntent
 */
private MultiPointToSinglePointIntent intentBuilder(IpPrefix ipPrefix,
        String nextHopMacAddress, ConnectPoint egressPoint) {

    TrafficSelector.Builder selectorBuilder =
            DefaultTrafficSelector.builder();
    if (ipPrefix.isIp4()) {
        selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
        selectorBuilder.matchIPDst(ipPrefix);
    } else {
        selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
        selectorBuilder.matchIPv6Dst(ipPrefix);
    }

    TrafficTreatment.Builder treatmentBuilder =
            DefaultTrafficTreatment.builder();
    treatmentBuilder.setEthDst(MacAddress.valueOf(nextHopMacAddress));

    Set<ConnectPoint> ingressPoints = new HashSet<>(connectPoints);
    ingressPoints.remove(egressPoint);

    MultiPointToSinglePointIntent intent =
            MultiPointToSinglePointIntent.builder()
                    .appId(APPID)
                    .key(Key.of(ipPrefix.toString(), APPID))
                    .selector(selectorBuilder.build())
                    .treatment(treatmentBuilder.build())
                    .ingressPoints(ingressPoints)
                    .egressPoint(egressPoint)
                    .build();
    return intent;
}
 
Example 7
Source File: BgpSession.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Removes a BGP route for a prefix. The prefix can be either IPv4 or IPv6.
 *
 * @param prefix the prefix to use
 * @return true if the route was found and removed, otherwise false
 */
boolean removeBgpRoute(IpPrefix prefix) {
    if (prefix.isIp4()) {
        return (bgpRibIn4.remove(prefix.getIp4Prefix()) != null);   // IPv4
    }
    return (bgpRibIn6.remove(prefix.getIp6Prefix()) != null);       // IPv6
}
 
Example 8
Source File: BgpSessionManager.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Finds a BGP route for a prefix. The prefix can be either IPv4 or IPv6.
 *
 * @param prefix the prefix to use
 * @return the BGP route if found, otherwise null
 */
BgpRouteEntry findBgpRoute(IpPrefix prefix) {
    if (prefix.isIp4()) {
        return bgpRoutes4.get(prefix.getIp4Prefix());               // IPv4
    }
    return bgpRoutes6.get(prefix.getIp6Prefix());                   // IPv6
}
 
Example 9
Source File: ControlPlaneRedirectManager.java    From onos with Apache License 2.0 5 votes vote down vote up
static TrafficSelector buildIPSrcSelector(IpPrefix srcIp,
                                          PortNumber inPort,
                                          MacAddress srcMac,
                                          MacAddress dstMac,
                                          VlanId vlanId) {
    TrafficSelector.Builder selector = buildBaseSelectorBuilder(inPort, srcMac, dstMac, vlanId);
    if (srcIp.isIp4()) {
        selector.matchEthType(TYPE_IPV4);
        selector.matchIPSrc(srcIp);
    } else {
        selector.matchEthType(TYPE_IPV6);
        selector.matchIPv6Src(srcIp);
    }
    return selector.build();
}
 
Example 10
Source File: ControlPlaneRedirectManager.java    From onos with Apache License 2.0 5 votes vote down vote up
static TrafficSelector buildIPDstSelector(IpPrefix dstIp,
                                          PortNumber inPort,
                                          MacAddress srcMac,
                                          MacAddress dstMac,
                                          VlanId vlanId) {
    TrafficSelector.Builder selector = buildBaseSelectorBuilder(inPort, srcMac, dstMac, vlanId);
    if (dstIp.isIp4()) {
        selector.matchEthType(TYPE_IPV4);
        selector.matchIPDst(dstIp);
    } else {
        selector.matchEthType(TYPE_IPV6);
        selector.matchIPv6Dst(dstIp);
    }
    return selector.build();
}
 
Example 11
Source File: FibInstaller.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Method to build IPv4 or IPv6 selector.
 *
 * @param prefixToMatch the prefix to match
 * @return the traffic selector builder
 */
private TrafficSelector.Builder buildIpSelectorFromIpPrefix(IpPrefix prefixToMatch) {
    TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
    // If the prefix is IPv4
    if (prefixToMatch.isIp4()) {
        selectorBuilder.matchEthType(Ethernet.TYPE_IPV4);
        selectorBuilder.matchIPDst(prefixToMatch);
        return selectorBuilder;
    }
    // If the prefix is IPv6
    selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
    selectorBuilder.matchIPv6Dst(prefixToMatch);
    return selectorBuilder;
}
 
Example 12
Source File: LispMapUtil.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Obtains the string formatted IP prefix.
 * For example, if the IP address is 10.1.1.1 and has 16 prefix length,
 * the resulting string is 10.1
 *
 * @param prefix IP prefix
 * @return string formatted IP prefix
 */
public static String getPrefixString(IpPrefix prefix) {
    String addressString = prefix.address().toString();
    StringBuilder sb = new StringBuilder();
    String delimiter = "";
    int numOfBlock = 0;

    if (prefix.isIp4()) {
        delimiter = IPV4_DELIMITER;
        numOfBlock = prefix.prefixLength() / IPV4_BLOCK_LENGTH;
    }

    if (prefix.isIp6()) {
        delimiter = IPV6_DELIMITER;
        numOfBlock = prefix.prefixLength() / IPV6_BLOCK_LENGTH;
    }

    String[] octets = StringUtils.split(addressString, delimiter);

    for (int i = 0; i < numOfBlock; i++) {
        sb.append(octets[i]);

        if (i < numOfBlock - 1) {
            sb.append(delimiter);
        }
    }

    return sb.toString();
}
 
Example 13
Source File: LispMapUtil.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Obtains the EID record from an IP prefix.
 *
 * @param prefix IP prefix
 * @return EID record
 */
public static LispEidRecord getEidRecordFromIpPrefix(IpPrefix prefix) {

    LispIpAddress eid = null;

    if (prefix.isIp4()) {
        eid = new LispIpv4Address(prefix.address());
    }

    if (prefix.isIp6()) {
        eid = new LispIpv6Address(prefix.address());
    }

    return new LispEidRecord((byte) prefix.prefixLength(), eid);
}
 
Example 14
Source File: IpConcurrentRadixTree.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public List<V> getValuesForAddressesStartingWith(IpPrefix prefix) {

    String prefixString = getPrefixString(prefix);

    if (prefix.isIp4()) {
        return Lists.newArrayList(ipv4Tree.getValuesForKeysStartingWith(prefixString));
    }

    if (prefix.isIp6()) {
        return Lists.newArrayList(ipv6Tree.getValuesForKeysStartingWith(prefixString));
    }

    return null;
}
 
Example 15
Source File: IpConcurrentRadixTree.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public V getValueForClosestParentAddress(IpPrefix prefix) {

    if (prefix.isIp4()) {
        return getValueForClosestParentAddress(prefix, ipv4Tree);
    }

    if (prefix.isIp6()) {
        return getValueForClosestParentAddress(prefix, ipv6Tree);
    }

    return null;
}
 
Example 16
Source File: IpConcurrentRadixTree.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public V getValueForExactAddress(IpPrefix prefix) {

    String prefixString = getPrefixString(prefix);

    if (prefix.isIp4()) {
        return ipv4Tree.getValueForExactKey(prefixString);
    }

    if (prefix.isIp6()) {
        return ipv6Tree.getValueForExactKey(prefixString);
    }

    return null;
}
 
Example 17
Source File: IpConcurrentRadixTree.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public boolean remove(IpPrefix prefix) {

    String prefixString = getPrefixString(prefix);

    if (prefix.isIp4()) {
        return ipv4Tree.remove(prefixString);
    }

    if (prefix.isIp6()) {
        return ipv6Tree.remove(prefixString);
    }

    return false;
}
 
Example 18
Source File: IpConcurrentRadixTree.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public V put(IpPrefix prefix, V value) {

    String prefixString = getPrefixString(prefix);

    if (prefix.isIp4()) {
        return ipv4Tree.put(prefixString, value);
    }
    if (prefix.isIp6()) {
        return ipv6Tree.put(prefixString, value);
    }

    return null;
}
 
Example 19
Source File: RoutingRulePopulator.java    From onos with Apache License 2.0 4 votes vote down vote up
private int getPriorityFromPrefix(IpPrefix prefix) {
    return (prefix.isIp4()) ?
            2000 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY :
            500 * prefix.prefixLength() + SegmentRoutingService.MIN_IP_PRIORITY;
}
 
Example 20
Source File: RoutingRulePopulator.java    From onos with Apache License 2.0 4 votes vote down vote up
private void updateDefaultRouteBlackhole(DeviceId deviceId, IpPrefix address, boolean install) {
    try {
        if (srManager.deviceConfiguration.isEdgeDevice(deviceId)) {

            TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
            if (address.isIp4()) {
                sbuilder.matchIPDst(address);
                sbuilder.matchEthType(EthType.EtherType.IPV4.ethType().toShort());
            } else {
                sbuilder.matchIPv6Dst(address);
                sbuilder.matchEthType(EthType.EtherType.IPV6.ethType().toShort());
            }

            TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
            tBuilder.wipeDeferred();

            ForwardingObjective.Builder fob = DefaultForwardingObjective.builder();
            fob.withFlag(Flag.SPECIFIC)
                    .withSelector(sbuilder.build())
                    .withTreatment(tBuilder.build())
                    .withPriority(getPriorityFromPrefix(address))
                    .fromApp(srManager.appId)
                    .makePermanent();

            log.debug("{} blackhole forwarding objectives for dev: {}",
                    install ? "Installing" : "Removing", deviceId);
            ObjectiveContext context = new DefaultObjectiveContext(
                    (objective) -> log.debug("Forward for {} {}", deviceId,
                            install ? "installed" : "removed"),
                    (objective, error) -> log.warn("Failed to {} forward for {}: {}",
                            install ? "install" : "remove", deviceId, error));
            if (install) {
                srManager.flowObjectiveService.forward(deviceId, fob.add(context));
            } else {
                srManager.flowObjectiveService.forward(deviceId, fob.remove(context));
            }
        }
    } catch (DeviceConfigNotFoundException e) {
        log.info("Not populating blackhole for un-configured device {}", deviceId);
    }

}