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

The following examples show how to use org.onlab.packet.IpPrefix#prefixLength() . 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: FibInstaller.java    From onos with Apache License 2.0 6 votes vote down vote up
private ForwardingObjective.Builder generateRibForwardingObj(IpPrefix prefix,
                                                             Integer nextId) {
    TrafficSelector selector = buildIpSelectorFromIpPrefix(prefix).build();
    int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;

    ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder()
            .fromApp(fibAppId)
            .makePermanent()
            .withSelector(selector)
            .withPriority(priority)
            .withFlag(ForwardingObjective.Flag.SPECIFIC);

    if (nextId == null) {
        // Route withdraws are not specified with next hops. Generating
        // dummy treatment as there is no equivalent nextId info.
        fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build());
    } else {
        fwdBuilder.nextStep(nextId);
    }
    return fwdBuilder;
}
 
Example 4
Source File: FibInstallerTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new forwarding objective with the given parameters.
 *
 * @param prefix IP prefix
 * @param add whether to create an add objective or a remove objective
 * @return new forwarding objective
 */
private ForwardingObjective createForwardingObjective(IpPrefix prefix,
                                                      boolean add) {
    TrafficSelector selector = DefaultTrafficSelector.builder()
            .matchEthType(Ethernet.TYPE_IPV4)
            .matchIPDst(prefix)
            .build();

    int priority = prefix.prefixLength() * 5 + 100;
    ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder()
            .fromApp(APPID)
            .makePermanent()
            .withSelector(selector)
            .withPriority(priority)
            .withFlag(ForwardingObjective.Flag.SPECIFIC);

    if (add) {
        fwdBuilder.nextStep(NEXT_ID);
    } else {
        fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build());
    }

    return add ? fwdBuilder.add() : fwdBuilder.remove();
}
 
Example 5
Source File: Ofdpa2Pipeline.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method to build Ipv6 selector using the selector provided by
 * a forwarding objective.
 *
 * @param builderToUpdate the builder to update
 * @param fwd the selector to read
 * @return 0 if the update ends correctly. -1 if the matches
 * are not yet supported
 */
int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
                                ForwardingObjective fwd) {

    TrafficSelector selector = fwd.selector();

    IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
    if (ipv6Dst.isMulticast()) {
        if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
            log.warn("Multicast specific forwarding objective can only be /128");
            fail(fwd, ObjectiveError.BADPARAMS);
            return -1;
        }
        VlanId assignedVlan = readVlanFromSelector(fwd.meta());
        if (assignedVlan == null) {
            log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
            fail(fwd, ObjectiveError.BADPARAMS);
            return -1;
        }
        if (requireVlanExtensions()) {
            OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
            builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
        } else {
            builderToUpdate.matchVlanId(assignedVlan);
        }
        builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
        log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
                          + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
    } else {
       if (ipv6Dst.prefixLength() != 0) {
           builderToUpdate.matchIPv6Dst(ipv6Dst);
       }
    builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
    log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
                          + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
    }
    return 0;
}
 
Example 6
Source File: IpConcurrentRadixTree.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value associated with the closest parent address from a
 * given radix tree, or returns null if no such value is associated
 * with the address.
 *
 * @param prefix IP prefix
 * @param tree a radix tree
 * @return A value associated with the closest parent address, or
 * null if no value was associated with the address
 */
private V getValueForClosestParentAddress(IpPrefix prefix, RadixTree<V> tree) {

    while (prefix != null && prefix.prefixLength() > 0) {
        V value = tree.getValueForExactKey(getPrefixString(prefix));
        if (value != null) {
            return value;
        }
        prefix = getParentPrefix(prefix);
    }

    return null;
}
 
Example 7
Source File: DeviceConfiguration.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the host IP is in any of the subnet defined in the router with the
 * device ID given.
 *
 * @param deviceId device identification of the router
 * @param hostIp   host IP address to check
 * @return true if the given IP is within any of the subnet defined in the router,
 * false if no subnet is defined in the router or if the host is not
 * within any subnet defined in the router
 */
public boolean inSameSubnet(DeviceId deviceId, IpAddress hostIp) {
    Set<IpPrefix> subnets = getConfiguredSubnets(deviceId);
    if (subnets == null) {
        return false;
    }

    for (IpPrefix subnet: subnets) {
        // Exclude /0 since it is a special case used for default route
        if (subnet.prefixLength() != 0 && subnet.contains(hostIp)) {
            return true;
        }
    }
    return false;
}
 
Example 8
Source File: LispExpireMapDatabase.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the map record associated with the closest parent address from a
 * given expire map, or returns null if no such map record is associated
 * with the address.
 *
 * @param prefix IP prefix
 * @return a map record with the closest parent address, or null if no value
 * was associated with the address
 */
private LispProxyMapRecord getMapRecordForClosestParentAddress(IpPrefix prefix) {
    while (prefix != null && prefix.prefixLength() > 0) {
        LispProxyMapRecord record = map.get(getEidRecordFromIpPrefix(prefix));
        if (record != null) {
            return record;
        }
        prefix = getParentPrefix(prefix);
    }

    return null;
}
 
Example 9
Source File: OpenstackSwitchingDhcpHandlerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
private byte[] bytesDestinationDescriptor(IpPrefix ipPrefix) {
    ByteBuffer byteBuffer;
    int prefixLen = ipPrefix.prefixLength();

    // retrieve ipPrefix to the destination descriptor format
    // ex) 10.1.1.0/24 -> [10,1,1,0]
    String[] ipPrefixString = ipPrefix.getIp4Prefix().toString()
            .split("/")[0]
            .split("\\.");

    // retrieve destination descriptor and put this to bytebuffer according to RFC 3442
    // ex) 0.0.0.0/0 -> 0
    // ex) 10.0.0.0/8 -> 8.10
    // ex) 10.17.0.0/16 -> 16.10.17
    // ex) 10.27.129.0/24 -> 24.10.27.129
    // ex) 10.229.0.128/25 -> 25.10.229.0.128
    for (int i = 0; i <= 4; i++) {
        if (prefixLen == Math.min(prefixLen, i * 8)) {
            byteBuffer = ByteBuffer.allocate(i + 1);
            byteBuffer.put((byte) prefixLen);

            for (int j = 0; j < i; j++) {
                byteBuffer.put((byte) Integer.parseInt(ipPrefixString[j]));
            }
            return byteBuffer.array();
        }
    }

    return null;
}
 
Example 10
Source File: InterfaceIpAddress.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Compute the IP broadcast address.
 *
 * @param ipAddress base IP address
 * @param subnetAddress subnet specification
 * @return the IP broadcast address
 */
public static IpAddress computeBroadcastAddress(IpAddress ipAddress, IpPrefix subnetAddress) {
    if (ipAddress.isIp6()) {
        return null;
    } else {
        IpAddress maskedIP = IpAddress.makeMaskedAddress(ipAddress, subnetAddress.prefixLength());
        int ipB = maskedIP.getIp4Address().toInt() | ((1 << (32 - subnetAddress.prefixLength())) - 1);
        return IpAddress.valueOf(ipB);
    }
}
 
Example 11
Source File: RouteTools.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a binary string representation of an IP prefix.
 *
 * For each string, we put a extra "0" in the front. The purpose of
 * doing this is to store the default route inside InvertedRadixTree.
 *
 * @param ipPrefix the IP prefix to use
 * @return the binary string representation
 */
public static String createBinaryString(IpPrefix ipPrefix) {
    byte[] octets = ipPrefix.address().toOctets();
    StringBuilder result = new StringBuilder(ipPrefix.prefixLength());
    result.append("0");
    for (int i = 0; i < ipPrefix.prefixLength(); i++) {
        int byteOffset = i / Byte.SIZE;
        int bitOffset = i % Byte.SIZE;
        int mask = 1 << (Byte.SIZE - 1 - bitOffset);
        byte value = octets[byteOffset];
        boolean isSet = ((value & mask) != 0);
        result.append(isSet ? "1" : "0");
    }
    return result.toString();
}
 
Example 12
Source File: ArtemisDeaggregatorImpl.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Handles a artemis event.
 *
 * @param event the artemis event
 */
protected void handleArtemisEvent(ArtemisEvent event) {
    if (event.type().equals(ArtemisEvent.Type.HIJACK_ADDED)) {
        IpPrefix receivedPrefix = (IpPrefix) event.subject();

        log.info("Deaggregator received a prefix " + receivedPrefix.toString());

        // can only de-aggregate /23 subnets and higher
        int cidr = receivedPrefix.prefixLength();
        if (receivedPrefix.prefixLength() < 24) {
            byte[] octets = receivedPrefix.address().toOctets();
            int byteGroup = (cidr + 1) / 8,
                    bitPos = 8 - (cidr + 1) % 8;

            octets[byteGroup] = (byte) (octets[byteGroup] & ~(1 << bitPos));
            String low = IpPrefix.valueOf(IpAddress.Version.INET, octets, cidr + 1).toString();
            octets[byteGroup] = (byte) (octets[byteGroup] | (1 << bitPos));
            String high = IpPrefix.valueOf(IpAddress.Version.INET, octets, cidr + 1).toString();

            String[] prefixes = {low, high};
            bgpSpeakers.forEach(bgpSpeakers -> bgpSpeakers.announceSubPrefixes(prefixes));
        } else {
            log.warn("Initiating MOAS");

            artemisService.getConfig().ifPresent(config -> config.monitoredPrefixes().forEach(artemisPrefixes -> {
                        log.info("checking if {} > {}", artemisPrefixes.prefix(), receivedPrefix);
                        if (artemisPrefixes.prefix().contains(receivedPrefix)) {
                            artemisPrefixes.moas().forEach(moasAddress -> {
                                        log.info("Creating a client for {}", moasAddress);
                                        MoasClientController client = new MoasClientController(
                                                packetProcessor,
                                                moasAddress,
                                                config.moasInfo().getTunnelPoints().iterator().next()
                                                        .getLocalIp(),
                                                receivedPrefix);
                                        log.info("Running client");
                                        client.run();
                                        moasClientControllers.add(client);
                                    }
                            );
                        }
                    }
            ));
        }

    }
}
 
Example 13
Source File: ReactiveRoutingFib.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Generates MultiPointToSinglePointIntent for both source host and
 * destination host located in local SDN network.
 *
 * @param dstIpAddress the destination IP address
 * @param dstConnectPoint the destination host connect point
 * @param dstMacAddress the MAC address of destination host
 * @param srcConnectPoint the connect point where packet-in from
 * @return the generated MultiPointToSinglePointIntent
 */
private MultiPointToSinglePointIntent hostToHostIntentGenerator(
        IpAddress dstIpAddress,
        ConnectPoint dstConnectPoint,
        MacAddress dstMacAddress,
        ConnectPoint srcConnectPoint) {
    checkNotNull(dstIpAddress);
    checkNotNull(dstConnectPoint);
    checkNotNull(dstMacAddress);
    checkNotNull(srcConnectPoint);

    Set<FilteredConnectPoint> ingressPoints = new HashSet<>();
    ingressPoints.add(new FilteredConnectPoint(srcConnectPoint));
    IpPrefix dstIpPrefix = dstIpAddress.toIpPrefix();

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

    // Rewrite the destination MAC address
    TrafficTreatment.Builder treatment =
            DefaultTrafficTreatment.builder().setEthDst(dstMacAddress);

    Key key = Key.of(dstIpPrefix.toString(), appId);
    int priority = dstIpPrefix.prefixLength() * PRIORITY_MULTIPLIER
            + PRIORITY_OFFSET;
    MultiPointToSinglePointIntent intent =
            MultiPointToSinglePointIntent.builder()
                    .appId(appId)
                    .key(key)
                    .selector(selector.build())
                    .treatment(treatment.build())
                    .filteredIngressPoints(ingressPoints)
                    .filteredEgressPoint(new FilteredConnectPoint(dstConnectPoint))
                    .priority(priority)
                    .constraints(CONSTRAINTS)
                    .build();

    log.trace("Generates ConnectivityHostToHost = {} ", intent);
    return intent;
}
 
Example 14
Source File: VtnManager.java    From onos with Apache License 2.0 4 votes vote down vote up
private boolean downloadSnatRules(DeviceId deviceId, MacAddress srcMac,
                                  IpAddress srcIp, MacAddress dstMac,
                                  IpAddress dstIp, FloatingIp floatingIp) {
    TenantNetwork exNetwork = tenantNetworkService
            .getNetwork(floatingIp.networkId());
    IpAddress fixedIp = floatingIp.fixedIp();
    VirtualPortId vmPortId = floatingIp.portId();
    VirtualPort vmPort = virtualPortService.getPort(vmPortId);
    if (vmPort == null) {
        vmPort = VtnData.getPort(vPortStore, vmPortId);
    }
    Subnet subnet = getSubnetOfFloatingIP(floatingIp);
    IpPrefix ipPrefix = subnet.cidr();
    IpAddress gwIp = subnet.gatewayIp();
    if (ipPrefix == null) {
        return false;
    }
    int mask = ipPrefix.prefixLength();
    if (mask <= 0) {
        return false;
    }
    TenantRouter tenantRouter = TenantRouter
            .tenantRouter(floatingIp.tenantId(), floatingIp.routerId());
    SegmentationId l3vni = vtnRscService.getL3vni(tenantRouter);
    // if the same ip segment
    if (IpUtil.checkSameSegment(srcIp, dstIp, mask)) {
        snatService.programSnatSameSegmentRules(deviceId, l3vni, fixedIp,
                                                dstIp, dstMac, srcMac,
                                                srcIp,
                                                exNetwork.segmentationId(),
                                                Objective.Operation.ADD);
        if (dstIp.equals(gwIp)) {
            snatService
                    .programSnatDiffSegmentRules(deviceId, l3vni, fixedIp,
                                                 dstMac, srcMac, srcIp,
                                                 exNetwork.segmentationId(),
                                                 Objective.Operation.ADD);
        }
    }
    return true;
}
 
Example 15
Source File: VtnManager.java    From onos with Apache License 2.0 4 votes vote down vote up
private void upStreamPacketProcessor(IPv4 ipPacket, DeviceId deviceId) {
    IpAddress srcIp = IpAddress.valueOf(ipPacket.getSourceAddress());
    IpAddress dstIp = IpAddress.valueOf(ipPacket.getDestinationAddress());
    FloatingIp floatingIp = null;
    Collection<FloatingIp> floatingIps = floatingIpService
            .getFloatingIps();
    Set<FloatingIp> floatingIpSet = Sets.newHashSet(floatingIps)
            .stream().collect(Collectors.toSet());
    for (FloatingIp f : floatingIpSet) {
        IpAddress fixIp = f.fixedIp();
        if (fixIp != null && fixIp.equals(srcIp)) {
            floatingIp = f;
            break;
        }
    }
    if (floatingIp == null) {
        return;
    }
    Subnet subnet = getSubnetOfFloatingIP(floatingIp);
    IpAddress gwIp = subnet.gatewayIp();
    Port exportPort = exPortOfDevice.get(deviceId);
    MacAddress exPortMac = MacAddress.valueOf(exportPort.annotations()
            .value(AnnotationKeys.PORT_MAC));
    IpPrefix ipPrefix = subnet.cidr();
    if (ipPrefix == null) {
        return;
    }
    int mask = ipPrefix.prefixLength();
    if (mask <= 0) {
        return;
    }
    Ethernet ethernet = null;
    // if the same ip segment
    if (IpUtil.checkSameSegment(floatingIp.floatingIp(), dstIp, mask)) {
        ethernet = buildArpRequest(dstIp, floatingIp.floatingIp(),
                                   exPortMac);
    } else {
        ethernet = buildArpRequest(gwIp, floatingIp.floatingIp(),
                                   exPortMac);
    }
    if (ethernet != null) {
        sendPacketOut(deviceId, exportPort.number(), ethernet);
    }
}
 
Example 16
Source File: SoftRouterPipeline.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * SoftRouter has a single specific table - the FIB Table. It emulates
 * LPM matching of dstIP by using higher priority flows for longer prefixes.
 * Flows are forwarded using flow-actions
 *
 * @param fwd The forwarding objective of type simple
 * @return A collection of flow rules meant to be delivered to the flowrule
 *         subsystem. Typically the returned collection has a single flowrule.
 *         May return empty collection in case of failures.
 *
 */
private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
    log.debug("Processing specific forwarding objective to next:{}", fwd.nextId());
    TrafficSelector selector = fwd.selector();
    EthTypeCriterion ethType =
            (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
    // XXX currently supporting only the L3 unicast table
    if (ethType == null || (ethType.ethType().toShort() != TYPE_IPV4
            && ethType.ethType().toShort() != Ethernet.TYPE_IPV6)) {
        fail(fwd, ObjectiveError.UNSUPPORTED);
        return Collections.emptySet();
    }
    //We build the selector according the eth type.
    IpPrefix ipPrefix;
    TrafficSelector.Builder filteredSelector;
    if (ethType.ethType().toShort() == TYPE_IPV4) {
        ipPrefix = ((IPCriterion)
                selector.getCriterion(Criterion.Type.IPV4_DST)).ip();

        filteredSelector = DefaultTrafficSelector.builder()
                .matchEthType(TYPE_IPV4);
    } else {
        ipPrefix = ((IPCriterion)
                selector.getCriterion(Criterion.Type.IPV6_DST)).ip();

        filteredSelector = DefaultTrafficSelector.builder()
                .matchEthType(Ethernet.TYPE_IPV6);
    }
    // If the prefix is different from the default via.
    if (ipPrefix.prefixLength() != 0) {
        if (ethType.ethType().toShort() == TYPE_IPV4) {
            filteredSelector.matchIPDst(ipPrefix);
        } else {
            filteredSelector.matchIPv6Dst(ipPrefix);
        }
    }

    TrafficTreatment tt = null;
    if (fwd.nextId() != null) {
        NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
        if (next == null) {
            log.error("next-id {} does not exist in store", fwd.nextId());
            return Collections.emptySet();
        }
        tt = appKryo.deserialize(next.data());
        if (tt == null)  {
            log.error("Error in deserializing next-id {}", fwd.nextId());
            return Collections.emptySet();
        }
    }

    FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
            .fromApp(fwd.appId())
            .withPriority(fwd.priority())
            .forDevice(deviceId)
            .withSelector(filteredSelector.build());

    if (tt != null) {
        ruleBuilder.withTreatment(tt);
    }

    if (fwd.permanent()) {
        ruleBuilder.makePermanent();
    } else {
        ruleBuilder.makeTemporary(fwd.timeout());
    }

    ruleBuilder.forTable(FIB_TABLE);
    return Collections.singletonList(ruleBuilder.build());
}
 
Example 17
Source File: PIMAddrGroup.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Set the encoded source address.
 *
 * @param pfx address prefix
 */
public void setAddr(IpPrefix pfx) {
    this.addr = pfx.address();
    this.masklen = (byte) pfx.prefixLength();
    this.family = (byte) ((this.addr.isIp4()) ? PIM.ADDRESS_FAMILY_IP4 : PIM.ADDRESS_FAMILY_IP6);
}
 
Example 18
Source File: PIMAddrSource.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * PIM Encoded Source Address.
 *
 * @param spfx IPv4 or IPv6 address prefix
 */
public void setAddr(IpPrefix spfx) {
    this.addr = spfx.address();
    this.masklen = (byte) spfx.prefixLength();
    this.family = (byte) ((this.addr.isIp4()) ? PIM.ADDRESS_FAMILY_IP4 : PIM.ADDRESS_FAMILY_IP6);
}
 
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: OvsOfdpaPipeline.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Handles forwarding rules to the IP Unicast Routing.
 *
 * @param fwd the forwarding objective
 * @return A collection of flow rules, or an empty set
 */
protected Collection<FlowRule> processDoubleTaggedFwd(ForwardingObjective fwd) {
    // inner for UNICAST_ROUTING_TABLE_1, outer for UNICAST_ROUTING_TABLE
    TrafficSelector selector = fwd.selector();
    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
    TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
    TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();

    EthTypeCriterion ethType =
            (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);

    if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
        sBuilder.matchEthType(Ethernet.TYPE_IPV4);
        sBuilder.matchVlanId(VlanId.ANY);
        IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
        if (!ipv4Dst.isMulticast() && ipv4Dst.prefixLength() == 32) {
            sBuilder.matchIPDst(ipv4Dst);
            if (fwd.nextId() != null) {
                NextGroup next = getGroupForNextObjective(fwd.nextId());
                if (next != null) {
                    List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
                    // we only need the top level group's key to point the flow to it
                    Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
                    if (group == null) {
                        log.warn("Group with key:{} for next-id:{} not found in dev:{}",
                                 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
                        fail(fwd, ObjectiveError.GROUPMISSING);
                        return Collections.emptySet();
                    }
                    outerTtb.immediate().setVlanId(extractDummyVlanIdFromGroupId(group.id().id()));
                    //ACTSET_OUTPUT in OVS will match output action in write_action() set.
                    outerTtb.deferred().setOutput(extractOutputPortFromGroupId(group.id().id()));
                    outerTtb.transition(EGRESS_VLAN_FLOW_TABLE_IN_INGRESS);
                    innerTtb.deferred().group(group.id());
                    innerTtb.transition(ACL_TABLE);

                    FlowRule.Builder innerRuleBuilder = DefaultFlowRule.builder()
                            .fromApp(fwd.appId())
                            .withPriority(fwd.priority())
                            .forDevice(deviceId)
                            .withSelector(sBuilder.build())
                            .withTreatment(innerTtb.build())
                            .forTable(UNICAST_ROUTING_TABLE_1);
                    if (fwd.permanent()) {
                        innerRuleBuilder.makePermanent();
                    } else {
                        innerRuleBuilder.makeTemporary(fwd.timeout());
                    }
                    Collection<FlowRule> flowRuleCollection = new HashSet<>();
                    flowRuleCollection.add(innerRuleBuilder.build());

                    FlowRule.Builder outerRuleBuilder = DefaultFlowRule.builder()
                            .fromApp(fwd.appId())
                            .withPriority(fwd.priority())
                            .forDevice(deviceId)
                            .withSelector(sBuilder.build())
                            .withTreatment(outerTtb.build())
                            .forTable(UNICAST_ROUTING_TABLE);
                    if (fwd.permanent()) {
                        outerRuleBuilder.makePermanent();
                    } else {
                        outerRuleBuilder.makeTemporary(fwd.timeout());
                    }
                    flowRuleCollection.add(innerRuleBuilder.build());
                    flowRuleCollection.add(outerRuleBuilder.build());
                    return flowRuleCollection;
                } else {
                    log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
                             fwd.nextId(), deviceId, fwd.id());
                    fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
                    return Collections.emptySet();
                }
            } else {
                log.warn("NextId is not specified in fwd:{}", fwd.id());
                fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
                return Collections.emptySet();
            }
        }
    }
    return Collections.emptySet();
}