org.onosproject.net.pi.runtime.PiActionParam Java Examples

The following examples show how to use org.onosproject.net.pi.runtime.PiActionParam. 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: FabricInterpreterTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Map treatment to push_internal_vlan action.
 */
@Test
public void testFilteringTreatmentPermitWithInternalVlan() throws Exception {
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .pushVlan()
            .setVlanId(VLAN_100)
            .build();
    PiAction mappedAction = interpreter.mapTreatment(treatment,
                                                     FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN);
    PiActionParam param = new PiActionParam(FabricConstants.VLAN_ID,
                                            ImmutableByteSequence.copyFrom(VLAN_100.toShort()));
    PiAction expectedAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN)
            .withParameter(param)
            .build();

    assertEquals(expectedAction, mappedAction);
}
 
Example #2
Source File: FabricNextPipelinerTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Test program set mac, set vlan, and output rule for Simple table.
 */
@Test
public void testSimpleOutputWithVlanAndMacTranslation() throws FabricPipelinerException {
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .setEthSrc(ROUTER_MAC)
            .setEthDst(HOST_MAC)
            .setVlanId(VLAN_100)
            .setOutput(PORT_1)
            .build();
    PiAction piAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_NEXT_ROUTING_SIMPLE)
            .withParameter(new PiActionParam(
                    FabricConstants.SMAC, ROUTER_MAC.toBytes()))
            .withParameter(new PiActionParam(
                    FabricConstants.DMAC, HOST_MAC.toBytes()))
            .withParameter(new PiActionParam(
                    FabricConstants.PORT_NUM, PORT_1.toLong()))
            .build();
    testSimple(treatment, piAction);
}
 
Example #3
Source File: Ipv6RoutingComponent.java    From ngsdn-tutorial with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a flow rule for the L2 table mapping the given next hop MAC to
 * the given output port.
 * <p>
 * This is called by the routing policy methods below to establish L2-based
 * forwarding inside the fabric, e.g., when deviceId is a leaf switch and
 * nextHopMac is the one of a spine switch.
 *
 * @param deviceId   the device
 * @param nexthopMac the next hop (destination) mac
 * @param outPort    the output port
 */
private FlowRule createL2NextHopRule(DeviceId deviceId, MacAddress nexthopMac,
                                     PortNumber outPort) {

    final String tableId = "IngressPipeImpl.l2_exact_table";
    final PiCriterion match = PiCriterion.builder()
            .matchExact(PiMatchFieldId.of("hdr.ethernet.dst_addr"),
                        nexthopMac.toBytes())
            .build();


    final PiAction action = PiAction.builder()
            .withId(PiActionId.of("IngressPipeImpl.set_egress_port"))
            .withParameter(new PiActionParam(
                    PiActionParamId.of("port_num"),
                    outPort.toLong()))
            .build();

    return Utils.buildFlowRule(
            deviceId, appId, tableId, match, action);
}
 
Example #4
Source File: Ipv6RoutingComponent.java    From onos-p4-tutorial with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a flow rule for the L2 table mapping the given next hop MAC to
 * the given output port.
 * <p>
 * This is called by the routing policy methods below to establish L2-based
 * forwarding inside the fabric, e.g., when deviceId is a leaf switch and
 * nextHopMac is the one of a spine switch.
 *
 * @param deviceId   the device
 * @param nexthopMac the next hop (destination) mac
 * @param outPort    the output port
 */
private FlowRule createL2NextHopRule(DeviceId deviceId, MacAddress nexthopMac,
                                     PortNumber outPort) {

    // TODO EXERCISE 3
    // Modify P4Runtime entity names to match content of P4Info file (look
    // for the fully qualified name of tables, match fields, and actions.
    // ---- START SOLUTION ----
    final String tableId = "IngressPipeImpl.l2_exact_table";
    final PiCriterion match = PiCriterion.builder()
            .matchExact(PiMatchFieldId.of("hdr.ethernet.dst_addr"),
                        nexthopMac.toBytes())
            .build();


    final PiAction action = PiAction.builder()
            .withId(PiActionId.of("IngressPipeImpl.set_output_port"))
            .withParameter(new PiActionParam(
                    PiActionParamId.of("port_num"),
                    outPort.toLong()))
            .build();
    // ---- END SOLUTION ----

    return Utils.buildFlowRule(
            deviceId, appId, tableId, match, action);
}
 
Example #5
Source File: InstructionCodecTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the encoding of protocol-independent instructions.
 */
@Test
public void piInstructionEncodingTest() {
    PiActionId actionId = PiActionId.of("set_egress_port");
    PiActionParamId actionParamId = PiActionParamId.of("port");
    PiActionParam actionParam = new PiActionParam(actionParamId, ImmutableByteSequence.copyFrom(10));
    PiTableAction action = PiAction.builder().withId(actionId).withParameter(actionParam).build();
    final PiInstruction actionInstruction = Instructions.piTableAction(action);
    final ObjectNode actionInstructionJson =
            instructionCodec.encode(actionInstruction, context);
    assertThat(actionInstructionJson, matchesInstruction(actionInstruction));

    PiTableAction actionGroupId = PiActionProfileGroupId.of(10);
    final PiInstruction actionGroupIdInstruction = Instructions.piTableAction(actionGroupId);
    final ObjectNode actionGroupIdInstructionJson =
            instructionCodec.encode(actionGroupIdInstruction, context);
    assertThat(actionGroupIdInstructionJson, matchesInstruction(actionGroupIdInstruction));

    PiTableAction actionProfileMemberId = PiActionProfileMemberId.of(10);
    final PiInstruction actionProfileMemberIdInstruction = Instructions.piTableAction(actionProfileMemberId);
    final ObjectNode actionProfileMemberIdInstructionJson =
            instructionCodec.encode(actionProfileMemberIdInstruction, context);
    assertThat(actionProfileMemberIdInstructionJson, matchesInstruction(actionProfileMemberIdInstruction));
}
 
Example #6
Source File: FabricNextPipelinerTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Test program set mac and output rule for Simple table.
 */
@Test
public void testSimpleOutputWithMacTranslation() throws FabricPipelinerException {
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .setEthSrc(ROUTER_MAC)
            .setEthDst(HOST_MAC)
            .setOutput(PORT_1)
            .build();
    PiAction piAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_NEXT_ROUTING_SIMPLE)
            .withParameter(new PiActionParam(
                    FabricConstants.SMAC, ROUTER_MAC.toBytes()))
            .withParameter(new PiActionParam(
                    FabricConstants.DMAC, HOST_MAC.toBytes()))
            .withParameter(new PiActionParam(
                    FabricConstants.PORT_NUM, PORT_1.toLong()))
            .build();
    testSimple(treatment, piAction);
}
 
Example #7
Source File: ActionCodec.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
protected P4RuntimeOuterClass.Action encode(
        PiAction piAction, Object ignored, PiPipeconf pipeconf, P4InfoBrowser browser)
        throws CodecException, P4InfoBrowser.NotFoundException {
    final int actionId = browser.actions()
            .getByName(piAction.id().toString()).getPreamble().getId();
    final P4RuntimeOuterClass.Action.Builder actionMsgBuilder =
            P4RuntimeOuterClass.Action.newBuilder().setActionId(actionId);
    for (PiActionParam p : piAction.parameters()) {
        final P4InfoOuterClass.Action.Param paramInfo = browser.actionParams(actionId)
                .getByName(p.id().toString());
        final ByteString paramValue = ByteString.copyFrom(p.value().asReadOnlyBuffer());
        assertSize(format("param '%s' of action '%s'", p.id(), piAction.id()),
                   paramValue, paramInfo.getBitwidth());
        actionMsgBuilder.addParams(P4RuntimeOuterClass.Action.Param.newBuilder()
                                           .setParamId(paramInfo.getId())
                                           .setValue(paramValue)
                                           .build());
    }
    return actionMsgBuilder.build();
}
 
Example #8
Source File: ActionCodec.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
protected PiAction decode(
        P4RuntimeOuterClass.Action message, Object ignored,
        PiPipeconf pipeconf, P4InfoBrowser browser)
        throws P4InfoBrowser.NotFoundException {
    final P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo =
            browser.actionParams(message.getActionId());
    final String actionName = browser.actions()
            .getById(message.getActionId())
            .getPreamble().getName();
    final PiAction.Builder builder = PiAction.builder()
            .withId(PiActionId.of(actionName));
    for (P4RuntimeOuterClass.Action.Param p : message.getParamsList()) {
        final String paramName = paramInfo.getById(p.getParamId()).getName();
        final ImmutableByteSequence value = ImmutableByteSequence.copyFrom(
                p.getValue().toByteArray());
        builder.withParameter(new PiActionParam(PiActionParamId.of(paramName), value));
    }
    return builder.build();
}
 
Example #9
Source File: FabricTreatmentInterpreter.java    From onos with Apache License 2.0 6 votes vote down vote up
private PiAction mapNextVlanTreatment(TrafficTreatment treatment, PiTableId tableId)
        throws PiInterpreterException {
    final List<ModVlanIdInstruction> modVlanIdInst = l2InstructionsOrFail(treatment, VLAN_ID, tableId)
            .stream().map(i -> (ModVlanIdInstruction) i).collect(Collectors.toList());
    if (modVlanIdInst.size() == 1) {
        return PiAction.builder().withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN)
                .withParameter(new PiActionParam(
                        FabricConstants.VLAN_ID,
                        modVlanIdInst.get(0).vlanId().toShort()))
                .build();
    }
    if (modVlanIdInst.size() == 2 && capabilities.supportDoubleVlanTerm()) {
        return PiAction.builder()
                .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_DOUBLE_VLAN)
                .withParameter(new PiActionParam(
                        FabricConstants.INNER_VLAN_ID,
                        modVlanIdInst.get(0).vlanId().toShort()))
                .withParameter(new PiActionParam(
                        FabricConstants.OUTER_VLAN_ID,
                        modVlanIdInst.get(1).vlanId().toShort()))
                .build();
    }
    throw new PiInterpreterException("Too many VLAN instructions");
}
 
Example #10
Source File: Ipv6RoutingComponent.java    From onos-p4-tutorial with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a flow rule for the L2 table mapping the given next hop MAC to
 * the given output port.
 * <p>
 * This is called by the routing policy methods below to establish L2-based
 * forwarding inside the fabric, e.g., when deviceId is a leaf switch and
 * nextHopMac is the one of a spine switch.
 *
 * @param deviceId   the device
 * @param nexthopMac the next hop (destination) mac
 * @param outPort    the output port
 */
private FlowRule createL2NextHopRule(DeviceId deviceId, MacAddress nexthopMac,
                                     PortNumber outPort) {

    // TODO EXERCISE 3
    // Modify P4Runtime entity names to match content of P4Info file (look
    // for the fully qualified name of tables, match fields, and actions.
    // ---- START SOLUTION ----
    final String tableId = "MODIFY ME";
    final PiCriterion match = PiCriterion.builder()
            .matchExact(PiMatchFieldId.of("MODIFY ME"),
                        nexthopMac.toBytes())
            .build();


    final PiAction action = PiAction.builder()
            .withId(PiActionId.of("MODIFY ME"))
            .withParameter(new PiActionParam(
                    PiActionParamId.of("MODIFY ME"),
                    outPort.toLong()))
            .build();
    // ---- END SOLUTION ----

    return Utils.buildFlowRule(
            deviceId, appId, tableId, match, action);
}
 
Example #11
Source File: FabricInterpreterTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Map treatment for hashed table to routing v4 action.
 */
@Test
public void testNextTreatmentHashedRoutingV4() throws Exception {
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .setEthSrc(SRC_MAC)
            .setEthDst(DST_MAC)
            .setOutput(PORT_1)
            .build();
    PiAction mappedAction = interpreter.mapTreatment(
            treatment, FabricConstants.FABRIC_INGRESS_NEXT_HASHED);
    PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, SRC_MAC.toBytes());
    PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, DST_MAC.toBytes());
    PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong());
    PiAction expectedAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_NEXT_ROUTING_HASHED)
            .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam))
            .build();
    assertEquals(expectedAction, mappedAction);
}
 
Example #12
Source File: FabricInterpreterTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Map treatment for hashed table to routing v4 action.
 */
@Test
public void testNextTreatmentHashedRoutingMpls() throws Exception {
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .setEthSrc(SRC_MAC)
            .setEthDst(DST_MAC)
            .setOutput(PORT_1)
            .pushMpls()
            .setMpls(MPLS_10)
            .build();
    PiAction mappedAction = interpreter.mapTreatment(
            treatment, FabricConstants.FABRIC_INGRESS_NEXT_HASHED);
    PiActionParam ethSrcParam = new PiActionParam(FabricConstants.SMAC, SRC_MAC.toBytes());
    PiActionParam ethDstParam = new PiActionParam(FabricConstants.DMAC, DST_MAC.toBytes());
    PiActionParam portParam = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong());
    PiActionParam mplsParam = new PiActionParam(FabricConstants.LABEL, MPLS_10.toInt());
    PiAction expectedAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_HASHED)
            .withParameters(ImmutableList.of(ethSrcParam, ethDstParam, portParam, mplsParam))
            .build();
    assertEquals(expectedAction, mappedAction);
}
 
Example #13
Source File: FabricInterpreterTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Map treatment to set_vlan_output action.
 */
@Test
public void testNextTreatment3() throws Exception {
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .setVlanId(VLAN_100)
            .build();
    PiAction mappedAction = interpreter.mapTreatment(
            treatment, FabricConstants.FABRIC_INGRESS_NEXT_NEXT_VLAN);
    PiActionParam vlanParam = new PiActionParam(
            FabricConstants.VLAN_ID, VLAN_100.toShort());
    PiAction expectedAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN)
            .withParameter(vlanParam)
            .build();
    assertEquals(expectedAction, mappedAction);
}
 
Example #14
Source File: FabricBngProgrammable.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Build the flow rule for the table t_line_map of the BNG-U (common to both
 * upstream and downstream).
 */
private FlowRule buildTLineMapFlowRule(Attachment attachment)
        throws BngProgrammableException {
    PiCriterion criterion = PiCriterion.builder()
            .matchExact(FabricConstants.HDR_S_TAG,
                    attachment.sTag().toShort())
            .matchExact(FabricConstants.HDR_C_TAG,
                    attachment.cTag().toShort())
            .build();
    TrafficSelector trafficSelector = DefaultTrafficSelector.builder()
            .matchPi(criterion)
            .build();
    PiAction action = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_BNG_INGRESS_SET_LINE)
            .withParameter(new PiActionParam(FabricConstants.LINE_ID,
                    lineId(attachment)))
            .build();
    TrafficTreatment instTreatment = DefaultTrafficTreatment.builder()
            .piTableAction(action)
            .build();
    return buildFlowRule(trafficSelector,
            instTreatment,
            FabricConstants.FABRIC_INGRESS_BNG_INGRESS_T_LINE_MAP,
            attachment.appId());
}
 
Example #15
Source File: FabricForwardingPipelineTest.java    From onos with Apache License 2.0 6 votes vote down vote up
@Test
public void testMpls() throws FabricPipelinerException {
    TrafficSelector selector = DefaultTrafficSelector.builder()
            .matchEthType(Ethernet.MPLS_UNICAST)
            .matchMplsLabel(MPLS_10)
            .matchMplsBos(true)
            .build();
    TrafficSelector expectedSelector = DefaultTrafficSelector.builder()
            .matchMplsLabel(MPLS_10)
            .build();

    PiActionParam nextIdParam = new PiActionParam(FabricConstants.NEXT_ID, NEXT_ID_1);
    PiAction setNextIdAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_POP_MPLS_AND_NEXT)
            .withParameter(nextIdParam)
            .build();
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .piTableAction(setNextIdAction)
            .build();
    testSpecificForward(FabricConstants.FABRIC_INGRESS_FORWARDING_MPLS,
                        expectedSelector, selector, NEXT_ID_1, treatment);
}
 
Example #16
Source File: FabricTreatmentInterpreter.java    From onos with Apache License 2.0 6 votes vote down vote up
static PiAction mapFilteringTreatment(TrafficTreatment treatment, PiTableId tableId)
        throws PiInterpreterException {

    if (!tableId.equals(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)) {
        // Mapping for other tables of the filtering block must be handled
        // in the pipeliner.
        tableException(tableId);
    }

    // VLAN_POP action is equivalent to the permit action (VLANs pop is done anyway)
    if (isNoAction(treatment) || isFilteringPopAction(treatment)) {
        // Permit action if table is ingress_port_vlan;
        return nop(tableId);
    }

    final ModVlanIdInstruction setVlanInst = (ModVlanIdInstruction) l2InstructionOrFail(
            treatment, VLAN_ID, tableId);
    return PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN)
            .withParameter(new PiActionParam(
                    FabricConstants.VLAN_ID, setVlanInst.vlanId().toShort()))
            .build();
}
 
Example #17
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 #18
Source File: MyTunnelApp.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Generates and insert a flow rule to perform the tunnel INGRESS function
 * for the given switch, destination IP address and tunnel ID.
 *
 * @param switchId  switch ID
 * @param dstIpAddr IP address to forward inside the tunnel
 * @param tunId     tunnel ID
 */
private void insertTunnelIngressRule(DeviceId switchId,
                                     IpAddress dstIpAddr,
                                     int tunId) {


    PiTableId tunnelIngressTableId = PiTableId.of("c_ingress.t_tunnel_ingress");

    // Longest prefix match on IPv4 dest address.
    PiMatchFieldId ipDestMatchFieldId = PiMatchFieldId.of("hdr.ipv4.dst_addr");
    PiCriterion match = PiCriterion.builder()
            .matchLpm(ipDestMatchFieldId, dstIpAddr.toOctets(), 32)
            .build();

    PiActionParam tunIdParam = new PiActionParam(PiActionParamId.of("tun_id"), tunId);

    PiActionId ingressActionId = PiActionId.of("c_ingress.my_tunnel_ingress");
    PiAction action = PiAction.builder()
            .withId(ingressActionId)
            .withParameter(tunIdParam)
            .build();

    log.info("Inserting INGRESS rule on switch {}: table={}, match={}, action={}",
             switchId, tunnelIngressTableId, match, action);

    insertPiFlowRule(switchId, tunnelIngressTableId, match, action);
}
 
Example #19
Source File: FabricForwardingPipelineTest.java    From onos with Apache License 2.0 5 votes vote down vote up
private void testSpecificForward(PiTableId expectedTableId, TrafficSelector expectedSelector,
                                 TrafficSelector selector, Integer nextId) throws FabricPipelinerException {
    TrafficTreatment setNextIdTreatment;
    if (nextId == null) {
        // Ref: RoutingRulePopulator.java->revokeIpRuleForRouter

        setNextIdTreatment = DefaultTrafficTreatment.builder().
                piTableAction(PiAction.builder()
                                      .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_NOP_ROUTING_V4)
                                      .build())
                .build();
    } else {
        PiActionParam nextIdParam = new PiActionParam(FabricConstants.NEXT_ID, nextId);
        PiAction.Builder setNextIdAction = PiAction.builder()
                .withParameter(nextIdParam);

        if (expectedTableId.equals(FabricConstants.FABRIC_INGRESS_FORWARDING_BRIDGING)) {
            setNextIdAction.withId(FabricConstants.FABRIC_INGRESS_FORWARDING_SET_NEXT_ID_BRIDGING);
        } else if (expectedTableId.equals(FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4)) {
            setNextIdAction.withId(FabricConstants.FABRIC_INGRESS_FORWARDING_SET_NEXT_ID_ROUTING_V4);
        } else if (expectedTableId.equals(FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V6)) {
            setNextIdAction.withId(FabricConstants.FABRIC_INGRESS_FORWARDING_SET_NEXT_ID_ROUTING_V6);
        }

        setNextIdTreatment = DefaultTrafficTreatment.builder()
                .piTableAction(setNextIdAction.build())
                .build();
    }

    testSpecificForward(expectedTableId, expectedSelector, selector, nextId, setNextIdTreatment);

}
 
Example #20
Source File: NdpReplyComponent.java    From ngsdn-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * Build a flow rule for the NDP reply table on the given device, for the
 * given target IPv6 address and MAC address.
 *
 * @param deviceId          device ID where to install the flow rules
 * @param targetIpv6Address target IPv6 address
 * @param targetMac         target MAC address
 * @return flow rule object
 */
private FlowRule buildNdpReplyFlowRule(DeviceId deviceId,
                                       Ip6Address targetIpv6Address,
                                       MacAddress targetMac) {

    // ** TODO EXERCISE 4
    // Modify P4Runtime entity names to match content of P4Info file (look
    // for the fully qualified name of tables, match fields, and actions.
    // ---- START SOLUTION ----
    // Build match.
    final PiCriterion match = PiCriterion.builder()
            .matchExact(PiMatchFieldId.of("hdr.ndp.target_ipv6_addr"), targetIpv6Address.toOctets())
            .build();
    // Build action.
    final PiActionParam targetMacParam = new PiActionParam(
            PiActionParamId.of("target_mac"), targetMac.toBytes());
    final PiAction action = PiAction.builder()
            .withId(PiActionId.of("<PUT HERE NAME OF NDP REPLY ACTION>"))
            .withParameter(targetMacParam)
            .build();
    // Table ID.
    final String tableId = "<PUT HERE NAME OF NDP REPLY TABLE>";
    // ---- END SOLUTION ----

    // Build flow rule.
    final FlowRule rule = Utils.buildFlowRule(
            deviceId, appId, tableId, match, action);

    return rule;
}
 
Example #21
Source File: FabricNextPipelinerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Test program output rule for Simple table.
 */
@Test
public void testSimpleOutput() throws FabricPipelinerException {
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .setOutput(PORT_1)
            .build();
    PiAction piAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE)
            .withParameter(new PiActionParam(
                    FabricConstants.PORT_NUM, PORT_1.toLong()))
            .build();
    testSimple(treatment, piAction);
}
 
Example #22
Source File: FabricNextPipelinerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Test program set vlan and output rule for Simple table.
 */
@Test
public void testSimpleOutputWithVlanTranslation() throws FabricPipelinerException {
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .setVlanId(VLAN_100)
            .setOutput(PORT_1)
            .build();
    PiAction piAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE)
            .withParameter(new PiActionParam(
                    FabricConstants.PORT_NUM, PORT_1.toLong()))
            .build();
    testSimple(treatment, piAction);
}
 
Example #23
Source File: IntProgrammableImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public boolean init() {
    if (!setupBehaviour()) {
        return false;
    }

    PiActionParam transitIdParam = new PiActionParam(
            IntConstants.SWITCH_ID,
            ImmutableByteSequence.copyFrom(
                    Integer.parseInt(deviceId.toString().substring(
                            deviceId.toString().length() - 2))));
    TrafficSelector selector = DefaultTrafficSelector.builder()
            .matchPi(PiCriterion.builder().matchExact(
                    IntConstants.HDR_INT_IS_VALID, (byte) 0x01)
                     .build())
            .build();
    PiAction transitAction = PiAction.builder()
            .withId(IntConstants.EGRESS_PROCESS_INT_TRANSIT_INIT_METADATA)
            .withParameter(transitIdParam)
            .build();
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .piTableAction(transitAction)
            .build();

    FlowRule transitFlowRule = DefaultFlowRule.builder()
            .withSelector(selector)
            .withTreatment(treatment)
            .fromApp(appId)
            .withPriority(DEFAULT_PRIORITY)
            .makePermanent()
            .forDevice(deviceId)
            .forTable(IntConstants.EGRESS_PROCESS_INT_TRANSIT_TB_INT_INSERT)
            .build();

    flowRuleService.applyFlowRules(transitFlowRule);

    return true;
}
 
Example #24
Source File: FabricNextPipelinerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    super.doSetup();

    translatorHashed = new NextObjectiveTranslator(DEVICE_ID, capabilitiesHashed);
    translatorSimple = new NextObjectiveTranslator(DEVICE_ID, capabilitiesSimple);

    PiCriterion nextIdCriterion = PiCriterion.builder()
            .matchExact(FabricConstants.HDR_NEXT_ID, NEXT_ID_1)
            .build();
    TrafficSelector selector = DefaultTrafficSelector.builder()
            .matchPi(nextIdCriterion)
            .build();
    PiAction piAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN)
            .withParameter(new PiActionParam(FabricConstants.VLAN_ID, VLAN_100.toShort()))
            .build();
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .piTableAction(piAction)
            .build();
    vlanMetaFlowRule = DefaultFlowRule.builder()
            .withSelector(selector)
            .withTreatment(treatment)
            .forTable(FabricConstants.FABRIC_INGRESS_NEXT_NEXT_VLAN)
            .makePermanent()
            // FIXME: currently next objective doesn't support priority, ignore this
            .withPriority(0)
            .forDevice(DEVICE_ID)
            .fromApp(APP_ID)
            .build();
}
 
Example #25
Source File: InstructionCodecTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the decoding of protocol-independent instructions.
 */
@Test
public void piInstructionDecodingTest() throws IOException {

    Instruction actionInstruction = getInstruction("PiActionInstruction.json");
    Assert.assertThat(actionInstruction.type(), is(Instruction.Type.PROTOCOL_INDEPENDENT));
    PiTableAction action = ((PiInstruction) actionInstruction).action();
    Assert.assertThat(action.type(), is(PiTableAction.Type.ACTION));
    Assert.assertThat(((PiAction) action).id().id(), is("set_egress_port"));
    Assert.assertThat(((PiAction) action).parameters().size(), is(1));
    Collection<PiActionParam> actionParams = ((PiAction) action).parameters();
    PiActionParam actionParam = actionParams.iterator().next();
    Assert.assertThat(actionParam.id().id(), is("port"));
    Assert.assertThat(actionParam.value(), is(copyFrom((byte) 0x1)));

    Instruction actionGroupIdInstruction = getInstruction("PiActionProfileGroupIdInstruction.json");
    Assert.assertThat(actionInstruction.type(), is(Instruction.Type.PROTOCOL_INDEPENDENT));
    PiTableAction actionGroupId = ((PiInstruction) actionGroupIdInstruction).action();
    Assert.assertThat(actionGroupId.type(), is(PiTableAction.Type.ACTION_PROFILE_GROUP_ID));
    Assert.assertThat(((PiActionProfileGroupId) actionGroupId).id(), is(100));

    Instruction actionMemberIdInstruction = getInstruction("PiActionProfileMemberIdInstruction.json");
    Assert.assertThat(actionInstruction.type(), is(Instruction.Type.PROTOCOL_INDEPENDENT));
    PiTableAction actionMemberId = ((PiInstruction) actionMemberIdInstruction).action();
    Assert.assertThat(actionMemberId.type(), is(PiTableAction.Type.ACTION_PROFILE_MEMBER_ID));
    Assert.assertThat(((PiActionProfileMemberId) actionMemberId).id(), is(100));
}
 
Example #26
Source File: FabricFilteringPipelinerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
private FlowRule buildExpectedVlanInPortRule(PortNumber inPort,
                                             VlanId vlanId,
                                             VlanId innerVlanId,
                                             VlanId internalVlan,
                                             TableId tableId) {

    TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
            .matchInPort(inPort);
    PiAction piAction;
    selector.matchPi(buildPiCriterionVlan(vlanId, innerVlanId));
    if (!vlanValid(vlanId)) {
        piAction = PiAction.builder()
                .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN)
                .withParameter(new PiActionParam(
                        FabricConstants.VLAN_ID, internalVlan.toShort()))
                .build();
    } else {
        selector.matchVlanId(vlanId);
        if (vlanValid(innerVlanId)) {
            selector.matchInnerVlanId(innerVlanId);
        }
        piAction = PiAction.builder()
                .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT)
                .build();
    }

    return DefaultFlowRule.builder()
            .withPriority(PRIORITY)
            .withSelector(selector.build())
            .withTreatment(DefaultTrafficTreatment.builder()
                                   .piTableAction(piAction).build())
            .fromApp(APP_ID)
            .forDevice(DEVICE_ID)
            .makePermanent()
            .forTable(tableId)
            .build();
}
 
Example #27
Source File: PiGroupTranslatorImplTest.java    From onos with Apache License 2.0 5 votes vote down vote up
private static PiActionProfileMember outputMember(int portNum)
        throws ImmutableByteSequence.ByteSequenceTrimException {
    PiActionParam param = new PiActionParam(PORT, copyFrom(portNum).fit(PORT_BITWIDTH));
    PiAction piAction = PiAction.builder()
            .withId(INGRESS_WCMP_CONTROL_SET_EGRESS_PORT)
            .withParameter(param).build();
    return PiActionProfileMember.builder()
            .forActionProfile(INGRESS_WCMP_CONTROL_WCMP_SELECTOR)
            .withAction(piAction)
            .withId(PiActionProfileMemberId.of(BASE_MEM_ID + portNum))
            .build();
}
 
Example #28
Source File: EncodeInstructionCodecHelper.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Encode a protocol-independent instruction.
 *
 * @param result json node that the instruction attributes are added to
 */
private void encodePi(ObjectNode result) {
    PiInstruction piInstruction = (PiInstruction) instruction;
    result.put(InstructionCodec.SUBTYPE, piInstruction.action().type().name());
    switch (piInstruction.action().type()) {
        case ACTION:
            final PiAction piAction = (PiAction) piInstruction.action();
            result.put(InstructionCodec.PI_ACTION_ID, piAction.id().id());
            final ObjectNode jsonActionParams = context.mapper().createObjectNode();
            for (PiActionParam actionParam : piAction.parameters()) {
                jsonActionParams.put(actionParam.id().id(),
                                     HexString.toHexString(actionParam.value().asArray(), null));
            }
            result.set(InstructionCodec.PI_ACTION_PARAMS, jsonActionParams);
            break;
        case ACTION_PROFILE_GROUP_ID:
            final PiActionProfileGroupId groupId = (PiActionProfileGroupId) piInstruction.action();
            result.put(InstructionCodec.PI_ACTION_PROFILE_GROUP_ID, groupId.id());
            break;
        case ACTION_PROFILE_MEMBER_ID:
            final PiActionProfileMemberId memberId = (PiActionProfileMemberId) piInstruction.action();
            result.put(InstructionCodec.PI_ACTION_PROFILE_MEMBER_ID, memberId.id());
            break;
        default:
            throw new IllegalArgumentException("Cannot convert protocol-independent subtype of" +
                                                       piInstruction.action().type().name());
    }
}
 
Example #29
Source File: PiFlowRuleTranslatorImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
private static PiTableAction checkPiAction(PiAction piAction, PiTableModel table)
        throws PiTranslationException {
    // Table supports this action?
    PiActionModel actionModel = table.action(piAction.id()).orElseThrow(
            () -> new PiTranslationException(format("Not such action '%s' for table '%s'",
                                                    piAction.id(), table.id())));

    // Is the number of runtime parameters correct?
    if (actionModel.params().size() != piAction.parameters().size()) {
        throw new PiTranslationException(format(
                "Wrong number of runtime parameters for action '%s', expected %d but found %d",
                actionModel.id(), actionModel.params().size(), piAction.parameters().size()));
    }

    // Forge a new action instance with well-sized parameters.
    // The same comment as in typeCheckFieldMatch() about duplicating field match instances applies here.
    PiAction.Builder newActionBuilder = PiAction.builder().withId(piAction.id());
    for (PiActionParam param : piAction.parameters()) {
        PiActionParamModel paramModel = actionModel.param(param.id())
                .orElseThrow(() -> new PiTranslationException(format(
                        "Not such parameter '%s' for action '%s'", param.id(), actionModel)));
        try {
            newActionBuilder.withParameter(new PiActionParam(param.id(),
                                                             param.value().fit(paramModel.bitWidth())));
        } catch (ByteSequenceTrimException e) {
            throw new PiTranslationException(format(
                    "Size mismatch for parameter '%s' of action '%s': %s",
                    param.id(), piAction.id(), e.getMessage()));
        }
    }

    return newActionBuilder.build();
}
 
Example #30
Source File: FabricInterpreterTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Map treatment to output action.
 */
@Test
public void testNextTreatmentSimpleOutput() throws Exception {
    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .setOutput(PORT_1)
            .build();
    PiAction mappedAction = interpreter.mapTreatment(
            treatment, FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE);
    PiActionParam param = new PiActionParam(FabricConstants.PORT_NUM, PORT_1.toLong());
    PiAction expectedAction = PiAction.builder()
            .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE)
            .withParameter(param)
            .build();
    assertEquals(expectedAction, mappedAction);
}