Java Code Examples for org.onosproject.net.flow.TrafficSelector#Builder

The following examples show how to use org.onosproject.net.flow.TrafficSelector#Builder . 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: OpenstackSecurityGroupHandler.java    From onos with Apache License 2.0 6 votes vote down vote up
private void buildMatchProto(TrafficSelector.Builder sBuilder, String protocol) {
    if (protocol != null) {
        switch (protocol.toUpperCase()) {
            case PROTO_ICMP:
                sBuilder.matchIPProtocol(IPv4.PROTOCOL_ICMP);
                break;
            case PROTO_TCP:
                sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP);
                break;
            case PROTO_UDP:
                sBuilder.matchIPProtocol(IPv4.PROTOCOL_UDP);
                break;
            default:
        }
    }
}
 
Example 2
Source File: OpenstackFlowRuleManager.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void connectTables(DeviceId deviceId, int fromTable, int toTable) {
    TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
    TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();

    treatment.transition(toTable);

    FlowRule flowRule = DefaultFlowRule.builder()
            .forDevice(deviceId)
            .withSelector(selector.build())
            .withTreatment(treatment.build())
            .withPriority(DROP_PRIORITY)
            .fromApp(appId)
            .makePermanent()
            .forTable(fromTable)
            .build();

    applyRule(flowRule, true);
}
 
Example 3
Source File: CorsaPipelineV3.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
protected Builder processVlanFiler(FilteringObjective filt, VlanIdCriterion vlan, PortCriterion port) {
    log.debug("adding rule for VLAN: {}", vlan.vlanId());
    TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
    TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
    selector.matchVlanId(vlan.vlanId());
    selector.matchInPort(port.port());
            /* Static treatment for VLAN_CIRCUIT_TABLE */
    treatment.setVlanPcp(MAX_VLAN_PCP);
    treatment.setQueue(0);
    treatment.meter(MeterId.meterId(defaultMeterId.id())); /* use default meter (Green) */
    treatment.transition(L3_IF_MAC_DA_TABLE);
    return DefaultFlowRule.builder()
            .withSelector(selector.build())
            .withTreatment(treatment.build())
            .withPriority(CONTROLLER_PRIORITY)
            .makePermanent()
            .forTable(VLAN_CIRCUIT_TABLE);
}
 
Example 4
Source File: RoutingRulePopulator.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a forwarding objective builder for egress forwarding rules.
 * <p>
 * The forwarding objective installs flow rules to egress pipeline to push
 * two vlan headers with given inner, outer vlan ids and outer tpid.
 *
 * @param portNumber port where the next hop attaches to
 * @param dummyVlanId vlan ID of the packet to match
 * @param innerVlan inner vlan ID of the next hop
 * @param outerVlan outer vlan ID of the next hop
 * @param outerTpid outer TPID of the next hop
 * @return forwarding objective builder
 */
private ForwardingObjective.Builder egressFwdObjBuilder(PortNumber portNumber, VlanId dummyVlanId,
                                                        VlanId innerVlan, VlanId outerVlan, EthType outerTpid) {
    TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
    sbuilder.matchVlanId(dummyVlanId);
    TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
    tbuilder.setOutput(portNumber).setVlanId(innerVlan);

    if (outerTpid.equals(EthType.EtherType.QINQ.ethType())) {
        tbuilder.pushVlan(outerTpid);
    } else {
        tbuilder.pushVlan();
    }

    tbuilder.setVlanId(outerVlan);
    return DefaultForwardingObjective.builder()
            .withSelector(sbuilder.build())
            .withTreatment(tbuilder.build())
            .fromApp(srManager.appId)
            .makePermanent()
            .withPriority(DEFAULT_PRIORITY)
            .withFlag(ForwardingObjective.Flag.EGRESS);
}
 
Example 5
Source File: OvsCorsaPipeline.java    From onos with Apache License 2.0 5 votes vote down vote up
private void processEtherTable(boolean install) {
    TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
    .matchEthType(Ethernet.TYPE_ARP);
    TrafficTreatment.Builder treatment = DefaultTrafficTreatment
            .builder()
            .punt();

    FlowRule rule = DefaultFlowRule.builder()
            .forDevice(deviceId)
            .withSelector(selector.build())
            .withTreatment(treatment.build())
            .withPriority(CONTROLLER_PRIORITY)
            .fromApp(appId)
            .makePermanent()
            .forTable(ETHER_TABLE).build();

    processFlowRule(true, rule, "Provisioned ether table");
    selector = DefaultTrafficSelector.builder()
            .matchEthType(Ethernet.TYPE_IPV4);
    treatment = DefaultTrafficTreatment.builder()
    .transition(COS_MAP_TABLE);

    rule = DefaultFlowRule.builder()
            .forDevice(deviceId)
            .withPriority(CONTROLLER_PRIORITY)
            .withSelector(selector.build())
            .withTreatment(treatment.build())
            .fromApp(appId)
            .makePermanent()
            .forTable(ETHER_TABLE).build();
    processFlowRule(true, rule, "Provisioned ether table");

    //Drop rule
    processTableMissDrop(true, VLAN_TABLE, "Provisioned ether table");

}
 
Example 6
Source File: LinkCollectionCompiler.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Update the selector builder using a L4 instruction.
 *
 * @param builder the builder to update
 * @param l4instruction the l4 instruction to use
 */
private void updateBuilder(TrafficSelector.Builder builder, L4ModificationInstruction l4instruction) {
    if (l4instruction instanceof ModTransportPortInstruction) {
        // TODO check IP proto
        ModTransportPortInstruction l4mod = (ModTransportPortInstruction) l4instruction;
        switch (l4mod.subtype()) {
            case TCP_SRC:
                builder.matchTcpSrc(l4mod.port());
                break;

            case TCP_DST:
                builder.matchTcpDst(l4mod.port());
                break;

            case UDP_SRC:
                builder.matchUdpSrc(l4mod.port());
                break;

            case UDP_DST:
                builder.matchUdpDst(l4mod.port());
                break;

            default:
                throw new IntentCompilationException(UNSUPPORTED_L4_SUBTYPE);
        }
    } else {
        throw new IntentCompilationException(UNSUPPORTED_L4);
    }
}
 
Example 7
Source File: OpenstackVtapManager.java    From onos with Apache License 2.0 5 votes vote down vote up
private void setOutputTableForTunnel(DeviceId deviceId, int tableId,
                                     PortNumber outPort, IpAddress serverIp,
                                     boolean install) {
    log.debug("setOutputTableForTunnel[{}]: deviceId={}, tableId={}, outPort={}, serverIp={}",
            install ? "add" : "remove", deviceId, tableId, outPort, serverIp);

    TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
    TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
            .setOutput(outPort);

    if (tunnelNicira) {
        ExtensionTreatment extensionTreatment = buildTunnelExtension(deviceId, serverIp);
        if (extensionTreatment == null) {
            return;
        }
        treatment.extension(extensionTreatment, deviceId);
    }

    FlowRule flowRule = DefaultFlowRule.builder()
            .forDevice(deviceId)
            .withSelector(selector.build())
            .withTreatment(treatment.build())
            .withPriority(PRIORITY_VTAP_OUTPUT_RULE)
            .makePermanent()
            .forTable(tableId)
            .fromApp(appId)
            .build();

    log.debug("setOutputTableForTunnel flowRule={}, install={}", flowRule, install);
    applyFlowRule(flowRule, install);
}
 
Example 8
Source File: RoutingRulePopulator.java    From onos with Apache License 2.0 5 votes vote down vote up
private ForwardingObjective.Builder arpFwdObjective(PortNumber port, boolean punt, int priority) {
    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
    sBuilder.matchEthType(TYPE_ARP);
    if (port != null) {
        sBuilder.matchInPort(port);
    }

    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
    if (punt) {
        tBuilder.punt();
    }
    return fwdObjBuilder(sBuilder.build(), tBuilder.build(), priority);
}
 
Example 9
Source File: ControlPlaneRedirectManager.java    From onos with Apache License 2.0 5 votes vote down vote up
static TrafficSelector buildArpSelector(PortNumber inPort,
                                        VlanId vlanId,
                                        Ip4Address arpSpa,
                                        MacAddress srcMac) {
    TrafficSelector.Builder selector = buildBaseSelectorBuilder(inPort, null, null, vlanId);
    selector.matchEthType(TYPE_ARP);
    if (arpSpa != null) {
        selector.matchArpSpa(arpSpa);
    }
    if (srcMac != null) {
        selector.matchEthSrc(srcMac);
    }
    return selector.build();
}
 
Example 10
Source File: FabricFilteringPipelinerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
private Collection<FlowRule> buildExpectedFwdClassifierRule(PortNumber inPort,
                                                            MacAddress dstMac,
                                                            MacAddress dstMacMask,
                                                            short ethType,
                                                            byte fwdClass) {
    PiActionParam classParam = new PiActionParam(FabricConstants.FWD_TYPE,
                                                 ImmutableByteSequence.copyFrom(fwdClass));
    PiAction fwdClassifierAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_FILTERING_SET_FORWARDING_TYPE)
            .withParameter(classParam)
            .build();
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .piTableAction(fwdClassifierAction)
            .build();

    TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
            .matchInPort(inPort);
    if (dstMacMask != null) {
        sbuilder.matchEthDstMasked(dstMac, dstMacMask);
    } else {
        sbuilder.matchEthDstMasked(dstMac, MacAddress.EXACT_MASK);
    }
    // Special case for MPLS UNICAST forwarding, need to build 2 rules for MPLS+IPv4 and MPLS+IPv6
    if (ethType == Ethernet.MPLS_UNICAST) {
        return buildExpectedFwdClassifierRulesMpls(fwdClassifierAction, treatment, sbuilder);
    }
    sbuilder.matchPi(PiCriterion.builder()
                             .matchExact(FabricConstants.HDR_IP_ETH_TYPE, ethType)
                             .build());
    TrafficSelector selector = sbuilder.build();
    return List.of(DefaultFlowRule.builder()
                           .withPriority(PRIORITY)
                           .withSelector(selector)
                           .withTreatment(treatment)
                           .fromApp(APP_ID)
                           .forDevice(DEVICE_ID)
                           .makePermanent()
                           .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER)
                           .build());
}
 
Example 11
Source File: ControlPlaneRedirectManager.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a next objective for forwarding to a port. Handles metadata for
 * some pipelines that require vlan information for egress port.
 *
 * @param deviceId the device on which the next objective is being created
 * @param portNumber the egress port
 * @param vlanId vlan information for egress port
 * @param popVlan if vlan tag should be popped or not
 * @param install true to create an add next objective, false to create a remove
 *            next objective
 * @return nextId of the next objective created
 */
private int modifyNextObjective(DeviceId deviceId, PortNumber portNumber,
                                VlanId vlanId, boolean popVlan, boolean install) {
    int nextId = flowObjectiveService.allocateNextId();
    NextObjective.Builder nextObjBuilder = DefaultNextObjective
            .builder().withId(nextId)
            .withType(NextObjective.Type.SIMPLE)
            .fromApp(appId);

    TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
    if (popVlan) {
        ttBuilder.popVlan();
    }
    ttBuilder.setOutput(portNumber);

    // setup metadata to pass to nextObjective - indicate the vlan on egress
    // if needed by the switch pipeline.
    TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
    metabuilder.matchVlanId(vlanId);

    nextObjBuilder.withMeta(metabuilder.build());
    nextObjBuilder.addTreatment(ttBuilder.build());
    log.debug("Submitted next objective {} in device {} for port/vlan {}/{}",
            nextId, deviceId, portNumber, vlanId);
    if (install) {
         flowObjectiveService.next(deviceId, nextObjBuilder.add());
    } else {
         flowObjectiveService.next(deviceId, nextObjBuilder.remove());
    }
    return nextId;
}
 
Example 12
Source File: K8sFlowRuleManager.java    From onos with Apache License 2.0 5 votes vote down vote up
private void setupJumpTable(K8sNode k8sNode) {
    DeviceId deviceId = k8sNode.intgBridge();

    TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
    TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();

    selector.matchEthDst(DEFAULT_GATEWAY_MAC);
    treatment.transition(ROUTING_TABLE);

    FlowRule flowRule = DefaultFlowRule.builder()
            .forDevice(deviceId)
            .withSelector(selector.build())
            .withTreatment(treatment.build())
            .withPriority(HIGH_PRIORITY)
            .fromApp(appId)
            .makePermanent()
            .forTable(GROUPING_TABLE)
            .build();

    applyRule(flowRule, true);

    selector = DefaultTrafficSelector.builder();
    treatment = DefaultTrafficTreatment.builder();

    treatment.transition(STAT_EGRESS_TABLE);

    flowRule = DefaultFlowRule.builder()
            .forDevice(deviceId)
            .withSelector(selector.build())
            .withTreatment(treatment.build())
            .withPriority(DROP_PRIORITY)
            .fromApp(appId)
            .makePermanent()
            .forTable(GROUPING_TABLE)
            .build();

    applyRule(flowRule, true);
}
 
Example 13
Source File: OpenstackRemoveAclCommand.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
protected void doExecute() {

    OpenstackFlowRuleService flowRuleService = get(OpenstackFlowRuleService.class);
    CoreService coreService = get(CoreService.class);

    ApplicationId appId = coreService.getAppId(OPENSTACK_NETWORKING_APP_ID);

    InstancePortService instancePortService = get(InstancePortService.class);

    IpAddress srcIpAddress = null;

    IpAddress dstIpAddress = null;

    try {
        srcIpAddress = IpAddress.valueOf(srcIpStr);

        dstIpAddress = IpAddress.valueOf(dstIpStr);
    } catch (IllegalArgumentException e) {
        log.error("IllegalArgumentException occurred because of {}", e);
        return;
    }

    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder()
            .matchEthType(Ethernet.TYPE_IPV4)
            .matchIPSrc(srcIpAddress.toIpPrefix())
            .matchIPDst(dstIpAddress.toIpPrefix());

    TrafficTreatment treatment = DefaultTrafficTreatment.builder().
            drop().build();

    if (srcPort != 0 || dstPort != 0) {
        sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP);
        if (srcPort != 0) {
            sBuilder.matchTcpSrc(TpPort.tpPort(srcPort));
        }

        if (dstPort != 0) {
            sBuilder.matchTcpDst(TpPort.tpPort(dstPort));
        }
    }

    log.info("Deny the packet from srcIp: {}, dstPort: {} to dstIp: {}, dstPort: {}",
            srcIpAddress.toString(),
            srcPort,
            dstIpAddress.toString(),
            dstPort);

    Optional<InstancePort> instancePort = instancePortService.instancePorts().stream()
            .filter(port -> port.ipAddress().toString().equals(dstIpStr))
            .findAny();

    if (!instancePort.isPresent()) {
        log.info("Instance port that matches with the given dst ip address isn't present {}");
        return;
    }

    flowRuleService.setRule(
            appId,
            instancePort.get().deviceId(),
            sBuilder.build(),
            treatment,
            PRIORITY_FORCED_ACL_RULE,
            DHCP_TABLE,
            false);
}
 
Example 14
Source File: SdnIpFibTest.java    From onos with Apache License 2.0 4 votes vote down vote up
private MultiPointToSinglePointIntent createIntentToThreeSrcOneTwoFour(IpPrefix ipPrefix) {
    // Build the expected treatment
    TrafficTreatment.Builder treatmentBuilder =
            DefaultTrafficTreatment.builder();
    treatmentBuilder.setEthDst(MAC3);

    // Build the expected egress FilteredConnectPoint
    FilteredConnectPoint egressFilteredCP = new FilteredConnectPoint(SW3_ETH1);

    // Build the expected selectors
    Set<FilteredConnectPoint> ingressFilteredCPs = Sets.newHashSet();

    // Build the expected ingress FilteredConnectPoint for sw1
    TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
    selector.matchVlanId(VLAN10);
    selector.matchEthType(Ethernet.TYPE_IPV4);
    selector.matchIPDst(ipPrefix);
    FilteredConnectPoint ingressFilteredCP =
            new FilteredConnectPoint(SW1_ETH1, selector.build());
    ingressFilteredCPs.add(ingressFilteredCP);

    // Build the expected ingress FilteredConnectPoint for sw2
    selector = DefaultTrafficSelector.builder();
    selector.matchVlanId(VLAN20);
    selector.matchEthType(Ethernet.TYPE_IPV4);
    selector.matchIPDst(ipPrefix);
    ingressFilteredCP = new FilteredConnectPoint(SW2_ETH1, selector.build());
    ingressFilteredCPs.add(ingressFilteredCP);

    // Build the expected ingress FilteredConnectPoint for sw4
    selector = DefaultTrafficSelector.builder();
    selector.matchEthType(Ethernet.TYPE_IPV4);
    selector.matchIPDst(ipPrefix);
    ingressFilteredCP = new FilteredConnectPoint(SW4_ETH1, selector.build());
    ingressFilteredCPs.add(ingressFilteredCP);

    // Build the expected intent
    MultiPointToSinglePointIntent intent =
            MultiPointToSinglePointIntent.builder()
                    .appId(APPID)
                    .key(Key.of(ipPrefix.toString(), APPID))
                    .filteredIngressPoints(ingressFilteredCPs)
                    .filteredEgressPoint(egressFilteredCP)
                    .treatment(treatmentBuilder.build())
                    .constraints(SdnIpFib.CONSTRAINTS)
                    .build();

    return intent;
}
 
Example 15
Source File: RoutingRulePopulator.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Populate or revoke a bridging rule on given deviceId that matches given vlanId,
 * and hostMAC connected to given port, and output to given port only when
 * vlan information is valid.
 *
 * @param deviceId device ID that host attaches to
 * @param portNum port number that host attaches to
 * @param hostMac mac address of the host connected to the switch port
 * @param vlanId Vlan ID configured on the switch port
 * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise
 * @param install true to populate the objective, false to revoke
 */
// TODO Refactor. There are a lot of duplications between this method, populateBridging,
//      revokeBridging and bridgingFwdObjBuilder.
void updateBridging(DeviceId deviceId, PortNumber portNum, MacAddress hostMac,
                    VlanId vlanId, boolean popVlan, boolean install) {
    // Create host selector
    TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
    sbuilder.matchEthDst(hostMac);

    // Create host meta
    TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder();

    sbuilder.matchVlanId(vlanId);
    mbuilder.matchVlanId(vlanId);

    // Create host treatment
    TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
    tbuilder.immediate().setOutput(portNum);

    if (popVlan) {
        tbuilder.immediate().popVlan();
    }

    int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum,
            tbuilder.build(), mbuilder.build(), install);
    if (portNextObjId != -1) {
        ForwardingObjective.Builder fob = DefaultForwardingObjective.builder()
                .withFlag(ForwardingObjective.Flag.SPECIFIC)
                .withSelector(sbuilder.build())
                .nextStep(portNextObjId)
                .withPriority(100)
                .fromApp(srManager.appId)
                .makePermanent();

        ObjectiveContext context = new DefaultObjectiveContext(
                (objective) -> log.debug("Brigding rule for {}/{} {}", hostMac, vlanId,
                        install ? "populated" : "revoked"),
                (objective, error) -> log.warn("Failed to {} bridging rule for {}/{}: {}",
                        install ? "populate" : "revoke", hostMac, vlanId, error));
        srManager.flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context));
    } else {
        log.warn("Failed to retrieve next objective for {}/{}", hostMac, vlanId);
    }
}
 
Example 16
Source File: LinkCollectionCompiler.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method which handles the proper generation of the ouput actions.
 *
 * @param outPorts the output ports
 * @param deviceId the current device
 * @param intent the intent to compile
 * @param outLabels the output labels
 * @param type the encapsulation type
 * @param preCondition the previous state
 * @param treatmentBuilder the builder to update with the ouput actions
 */
private void manageOutputPorts(Set<PortNumber> outPorts,
                               DeviceId deviceId,
                               LinkCollectionIntent intent,
                               Map<ConnectPoint, Identifier<?>> outLabels,
                               EncapsulationType type,
                               TrafficSelector.Builder preCondition,
                               TrafficTreatment.Builder treatmentBuilder) {
    /*
     * We need to order the actions. First the actions
     * related to the not-egress points. At the same time we collect
     * also the egress points.
     */
    List<FilteredConnectPoint> egressPoints = Lists.newArrayList();
    for (PortNumber outPort : outPorts) {
        Optional<FilteredConnectPoint> filteredEgressPoint =
                getFilteredConnectPointFromIntent(deviceId, outPort, intent);
        if (!filteredEgressPoint.isPresent()) {
            /*
             * We build a temporary selector for the encapsulation.
             */
            TrafficSelector.Builder encapBuilder = DefaultTrafficSelector.builder();
            /*
             * We retrieve the associated label to the output port.
             */
            ConnectPoint cp = new ConnectPoint(deviceId, outPort);
            Identifier<?> outLabel = outLabels.get(cp);
            /*
             * If there are not labels, we cannot handle.
             */
            if (outLabel == null) {
                throw new IntentCompilationException(String.format(NO_LABELS, cp));
            }
            /*
             * In the core we match using encapsulation.
             */
            updateSelectorFromEncapsulation(
                    encapBuilder,
                    type,
                    outLabel
            );
            /*
             * We generate the transition.
             */
            TrafficTreatment forwardingTreatment =
                    forwardingTreatment(preCondition.build(),
                                        encapBuilder.build(),
                                        getEthType(intent.selector()));
            /*
             * We add the instruction necessary to the transition.
             */
            forwardingTreatment.allInstructions().stream()
                    .filter(inst -> inst.type() != Instruction.Type.NOACTION)
                    .forEach(treatmentBuilder::add);
            /*
             * Finally we set the output action.
             */
            treatmentBuilder.setOutput(outPort);
            /*
             * The encapsulation modifies the packet. If we are optimizing
             * we have to update the state.
             */
            if (optimizeTreatments()) {
                preCondition = encapBuilder;
            }
        } else {
            egressPoints.add(filteredEgressPoint.get());
        }
    }
    /*
     * The idea is to order the egress points. Before we deal
     * with the egress points which looks like similar to the
     * selector derived from the previous state then the
     * the others.
     */
    TrafficSelector prevState = preCondition.build();
    if (optimizeTreatments()) {
        egressPoints = orderedEgressPoints(prevState, egressPoints);
    }
    /*
     * In this case, we have to transit to the final
     * state.
     */
    generateEgressActions(treatmentBuilder, egressPoints, prevState, intent);

}
 
Example 17
Source File: OpenstackRoutingSnatHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
private void setStatelessSnatDownstreamRules(InstancePort srcInstPort,
                                             String segmentId,
                                             Type networkType,
                                             IpAddress externalIp,
                                             ExternalPeerRouter externalPeerRouter,
                                             TpPort patPort,
                                             InboundPacket packetIn) {
    IPv4 iPacket = (IPv4) packetIn.parsed().getPayload();
    IpAddress internalIp = IpAddress.valueOf(iPacket.getSourceAddress());

    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder()
            .matchEthType(Ethernet.TYPE_IPV4)
            .matchIPProtocol(iPacket.getProtocol())
            .matchIPDst(IpPrefix.valueOf(externalIp.getIp4Address(), VM_PREFIX))
            .matchIPSrc(IpPrefix.valueOf(iPacket.getDestinationAddress(), VM_PREFIX));

    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder()
            .setEthDst(packetIn.parsed().getSourceMAC())
            .setIpDst(internalIp);

    if (!externalPeerRouter.vlanId().equals(VlanId.NONE)) {
        sBuilder.matchVlanId(externalPeerRouter.vlanId());
        tBuilder.popVlan();
    }

    switch (networkType) {
        case VXLAN:
        case GRE:
        case GENEVE:
            tBuilder.setTunnelId(Long.parseLong(segmentId));
            break;
        case VLAN:
            tBuilder.pushVlan()
                    .setVlanId(VlanId.vlanId(segmentId));
            break;
        default:
            final String error = String.format("%s %s",
                    ERR_UNSUPPORTED_NET_TYPE, networkType.toString());
            throw new IllegalStateException(error);
    }


    switch (iPacket.getProtocol()) {
        case IPv4.PROTOCOL_TCP:
            TCP tcpPacket = (TCP) iPacket.getPayload();
            sBuilder.matchTcpSrc(TpPort.tpPort(tcpPacket.getDestinationPort()))
                    .matchTcpDst(patPort);
            tBuilder.setTcpDst(TpPort.tpPort(tcpPacket.getSourcePort()));
            break;
        case IPv4.PROTOCOL_UDP:
            UDP udpPacket = (UDP) iPacket.getPayload();
            sBuilder.matchUdpSrc(TpPort.tpPort(udpPacket.getDestinationPort()))
                    .matchUdpDst(patPort);
            tBuilder.setUdpDst(TpPort.tpPort(udpPacket.getSourcePort()));
            break;
        default:
            break;
    }

    OpenstackNode srcNode = osNodeService.node(srcInstPort.deviceId());
    osNodeService.completeNodes(GATEWAY).forEach(gNode -> {
        TrafficTreatment treatment =
                getDownstreamTreatment(networkType, tBuilder, gNode, srcNode);
        osFlowRuleService.setRule(
                appId,
                gNode.intgBridge(),
                sBuilder.build(),
                treatment,
                PRIORITY_SNAT_RULE,
                GW_COMMON_TABLE,
                true);
    });
}
 
Example 18
Source File: OpenstackSwitchingIcmpHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
private void setGatewayIcmpReplyRule(OpenstackNode osNode,
                                     String segmentId,
                                     IpAddress gatewayIp,
                                     Type networkType,
                                     boolean install) {
    TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder()
            .matchEthType(Ethernet.TYPE_IPV4)
            .matchIPProtocol(IPv4.PROTOCOL_ICMP)
            .matchIcmpType(TYPE_ECHO_REQUEST)
            .matchIcmpCode(CODE_ECHO_REQEUST)
            .matchIPDst(gatewayIp.getIp4Address().toIpPrefix());

    if (segmentId != null) {
        switch (networkType) {
            case VXLAN:
            case GRE:
            case GENEVE:
                sBuilder.matchTunnelId(Long.parseLong(segmentId));
                break;
            case VLAN:
                sBuilder.matchVlanId(VlanId.vlanId(segmentId));
                break;
            default:
                break;
        }
    }

    Device device = deviceService.getDevice(osNode.intgBridge());
    TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder()
            .extension(buildMoveEthSrcToDstExtension(device), device.id())
            .extension(buildMoveIpSrcToDstExtension(device), device.id())
            .extension(buildLoadExtension(device, NXM_NX_IP_TTL, DEFAULT_TTL), device.id())
            .extension(buildLoadExtension(device, NXM_OF_ICMP_TYPE, TYPE_ECHO_REPLY), device.id())
            .setIpSrc(gatewayIp)
            .setEthSrc(DEFAULT_GATEWAY_MAC)
            .setOutput(PortNumber.IN_PORT);

    osFlowRuleService.setRule(
            appId,
            osNode.intgBridge(),
            sBuilder.build(),
            tBuilder.build(),
            PRIORITY_ICMP_RULE,
            ROUTING_TABLE,
            install);
}
 
Example 19
Source File: SpringOpenTTP.java    From onos with Apache License 2.0 4 votes vote down vote up
protected List<FlowRule> processEthDstFilter(EthCriterion ethCriterion,
                                   VlanIdCriterion vlanIdCriterion,
                                   FilteringObjective filt,
                                   VlanId assignedVlan,
                                   ApplicationId applicationId) {
    if (vlanIdCriterion == null) {
        return processEthDstOnlyFilter(ethCriterion, applicationId, filt.priority());
    }

    //handling untagged packets via assigned VLAN
    if (vlanIdCriterion.vlanId() == VlanId.NONE) {
        vlanIdCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
    }

    List<FlowRule> rules = new ArrayList<>();
    TrafficSelector.Builder selectorIp = DefaultTrafficSelector
            .builder();
    TrafficTreatment.Builder treatmentIp = DefaultTrafficTreatment
            .builder();
    selectorIp.matchEthDst(ethCriterion.mac());
    selectorIp.matchEthType(Ethernet.TYPE_IPV4);
    selectorIp.matchVlanId(vlanIdCriterion.vlanId());
    treatmentIp.popVlan();
    treatmentIp.transition(ipv4UnicastTableId);
    FlowRule ruleIp = DefaultFlowRule.builder().forDevice(deviceId)
            .withSelector(selectorIp.build())
            .withTreatment(treatmentIp.build())
            .withPriority(filt.priority()).fromApp(applicationId)
            .makePermanent().forTable(tmacTableId).build();
    log.debug("adding IP ETH rule for MAC: {}", ethCriterion.mac());
    rules.add(ruleIp);

    TrafficSelector.Builder selectorMpls = DefaultTrafficSelector
            .builder();
    TrafficTreatment.Builder treatmentMpls = DefaultTrafficTreatment
            .builder();
    selectorMpls.matchEthDst(ethCriterion.mac());
    selectorMpls.matchEthType(Ethernet.MPLS_UNICAST);
    selectorMpls.matchVlanId(vlanIdCriterion.vlanId());
    treatmentMpls.popVlan();
    treatmentMpls.transition(mplsTableId);
    FlowRule ruleMpls = DefaultFlowRule.builder()
            .forDevice(deviceId).withSelector(selectorMpls.build())
            .withTreatment(treatmentMpls.build())
            .withPriority(filt.priority()).fromApp(applicationId)
            .makePermanent().forTable(tmacTableId).build();
    log.debug("adding MPLS ETH rule for MAC: {}", ethCriterion.mac());
    rules.add(ruleMpls);

    return rules;
}
 
Example 20
Source File: LinkCollectionCompiler.java    From onos with Apache License 2.0 2 votes vote down vote up
/**
 * Update the selector builder using a L1 instruction.
 *
 * @param builder the builder to update
 * @param l1instruction the l1 instruction to use
 */
private void updateBuilder(TrafficSelector.Builder builder, L1ModificationInstruction l1instruction) {
    throw new IntentCompilationException(UNSUPPORTED_L1);
}