Java Code Examples for org.onosproject.net.pi.model.PiActionParamId#of()

The following examples show how to use org.onosproject.net.pi.model.PiActionParamId#of() . 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: PiActionTest.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Checks the construction of a PiAction object with parameters.
 */
@Test
public void testMethodWithParameters() {
    PiActionId piActionId = PiActionId.of(MOD_NW_DST);
    Collection<PiActionParam> runtimeParams = Lists.newArrayList();
    PiActionParam piActionParam = new PiActionParam(PiActionParamId.of(DST_ADDR), copyFrom(0x0a010101));

    runtimeParams.add(piActionParam);
    final PiAction piAction = PiAction.builder().withId(piActionId)
            .withParameters(runtimeParams)
            .build();
    assertThat(piAction, is(notNullValue()));
    assertThat(piAction.id(), is(piActionId));
    assertThat(piAction.parameters(), is(runtimeParams));
    assertThat(piAction.type(), is(PiTableAction.Type.ACTION));
}
 
Example 2
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 3
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("IngressPipeImpl.ndp_ns_to_na"))
            .withParameter(targetMacParam)
            .build();
    // Table ID.
    final String tableId = "IngressPipeImpl.ndp_reply_table";
    // ---- END SOLUTION ----

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

    return rule;
}
 
Example 4
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 5
Source File: NdpReplyComponent.java    From onos-p4-tutorial with Apache License 2.0 5 votes vote down vote up
private FlowRule buildNdpReplyFlowRule(DeviceId deviceId,
                                       MacAddress deviceMac,
                                       Ip6Address targetIp) {
    PiCriterion match = PiCriterion.builder()
            .matchExact(PiMatchFieldId.of("hdr.ndp.target_addr"), targetIp.toOctets())
            .build();

    PiActionParam paramRouterMac = new PiActionParam(
            PiActionParamId.of("target_mac"), deviceMac.toBytes());
    PiAction action = PiAction.builder()
            .withId(PiActionId.of("IngressPipeImpl.ndp_ns_to_na"))
            .withParameter(paramRouterMac)
            .build();

    TrafficSelector selector = DefaultTrafficSelector.builder()
            .matchPi(match)
            .build();

    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .piTableAction(action)
            .build();

    return DefaultFlowRule.builder()
            .forDevice(deviceId)
            .forTable(PiTableId.of("IngressPipeImpl.ndp_reply_table"))
            .fromApp(appId)
            .makePermanent()
            .withSelector(selector)
            .withTreatment(treatment)
            .withPriority(DEFAULT_FLOW_RULE_PRIORITY)
            .build();
}
 
Example 6
Source File: Srv6Component.java    From onos-p4-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * Insert a SRv6 transit insert policy that will inject an SRv6 header for
 * packets destined to destIp.
 *
 * @param deviceId     device ID
 * @param destIp       target IP address for the SRv6 policy
 * @param prefixLength prefix length for the target IP
 * @param segmentList  list of SRv6 SIDs that make up the path
 */
public void insertSrv6InsertRule(DeviceId deviceId, Ip6Address destIp, int prefixLength,
                                 List<Ip6Address> segmentList) {
    if (segmentList.size() < 2 || segmentList.size() > 3) {
        throw new RuntimeException("List of " + segmentList.size() + " segments is not supported");
    }

    // TODO EXERCISE 4
    // Fill in the table ID for the SRv6 transit table.
    // ---- START SOLUTION ----
    String tableId = "IngressPipeImpl.srv6_transit";
    // ---- END SOLUTION ----

    // TODO EXERCISE 4
    // Modify match field, action id, and action parameter to match your P4Info.
    // ---- START SOLUTION ----
    PiCriterion match = PiCriterion.builder()
            .matchLpm(PiMatchFieldId.of("hdr.ipv6.dst_addr"), destIp.toOctets(), prefixLength)
            .build();

    List<PiActionParam> actionParams = Lists.newArrayList();

    for (int i = 0; i < segmentList.size(); i++) {
        PiActionParamId paramId = PiActionParamId.of("s" + (i + 1));
        PiActionParam param = new PiActionParam(paramId, segmentList.get(i).toOctets());
        actionParams.add(param);
    }

    PiAction action = PiAction.builder()
            .withId(PiActionId.of("IngressPipeImpl.srv6_t_insert_" + segmentList.size()))
            .withParameters(actionParams)
            .build();
    // ---- END SOLUTION ----

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

    flowRuleService.applyFlowRules(rule);
}
 
Example 7
Source File: NdpReplyComponent.java    From onos-p4-tutorial with Apache License 2.0 5 votes vote down vote up
private FlowRule buildNdpReplyFlowRule(DeviceId deviceId,
                                       MacAddress deviceMac,
                                       Ip6Address targetIp) {
    PiCriterion match = PiCriterion.builder()
            .matchExact(PiMatchFieldId.of("hdr.ndp.target_addr"), targetIp.toOctets())
            .build();

    PiActionParam paramRouterMac = new PiActionParam(
            PiActionParamId.of("target_mac"), deviceMac.toBytes());
    PiAction action = PiAction.builder()
            .withId(PiActionId.of("IngressPipeImpl.ndp_ns_to_na"))
            .withParameter(paramRouterMac)
            .build();

    TrafficSelector selector = DefaultTrafficSelector.builder()
            .matchPi(match)
            .build();

    TrafficTreatment treatment = DefaultTrafficTreatment.builder()
            .piTableAction(action)
            .build();

    return DefaultFlowRule.builder()
            .forDevice(deviceId)
            .forTable(PiTableId.of("IngressPipeImpl.ndp_reply_table"))
            .fromApp(appId)
            .makePermanent()
            .withSelector(selector)
            .withTreatment(treatment)
            .withPriority(DEFAULT_FLOW_RULE_PRIORITY)
            .build();
}
 
Example 8
Source File: Srv6Component.java    From onos-p4-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * Insert a SRv6 transit insert policy that will inject an SRv6 header for
 * packets destined to destIp.
 *
 * @param deviceId     device ID
 * @param destIp       target IP address for the SRv6 policy
 * @param prefixLength prefix length for the target IP
 * @param segmentList  list of SRv6 SIDs that make up the path
 */
public void insertSrv6InsertRule(DeviceId deviceId, Ip6Address destIp, int prefixLength,
                                 List<Ip6Address> segmentList) {
    if (segmentList.size() < 2 || segmentList.size() > 3) {
        throw new RuntimeException("List of " + segmentList.size() + " segments is not supported");
    }

    // TODO EXERCISE 4
    // Fill in the table ID for the SRv6 transit table.
    // ---- START SOLUTION ----
    String tableId = "MODIFY ME";
    // ---- END SOLUTION ----

    // TODO EXERCISE 4
    // Modify match field, action id, and action parameters to match your P4Info.
    // ---- START SOLUTION ----
    PiCriterion match = PiCriterion.builder()
            .matchLpm(PiMatchFieldId.of("MODIFY ME"), destIp.toOctets(), prefixLength)
            .build();

    List<PiActionParam> actionParams = Lists.newArrayList();

    for (int i = 0; i < segmentList.size(); i++) {
        PiActionParamId paramId = PiActionParamId.of("s" + (i + 1));
        PiActionParam param = new PiActionParam(paramId, segmentList.get(i).toOctets());
        actionParams.add(param);
    }

    PiAction action = PiAction.builder()
            .withId(PiActionId.of("IngressPipeImpl.srv6_t_insert_" + segmentList.size()))
            .withParameters(actionParams)
            .build();
    // ---- END SOLUTION ----

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

    flowRuleService.applyFlowRules(rule);
}
 
Example 9
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 10
Source File: PiActionParamIdTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the construction of a PiActionParamId object.
 */
@Test
public void testConstruction() {
    final String param = SRC_ADDR;
    final PiActionParamId actionParamId = PiActionParamId.of(param);
    assertThat(actionParamId, is(notNullValue()));
    assertThat(actionParamId.id(), is(param));
}
 
Example 11
Source File: PiActionTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the construction of a PiAction object with parameter.
 */
@Test
public void testMethodWithParameter() {
    PiActionId piActionId = PiActionId.of(MOD_NW_DST);
    PiActionParam piActionParam = new PiActionParam(PiActionParamId.of(DST_ADDR), copyFrom(0x0a010101));
    final PiAction piAction = PiAction.builder().withId(piActionId)
            .withParameter(piActionParam)
            .build();
    assertThat(piAction, is(notNullValue()));
    assertThat(piAction.id(), is(piActionId));
    assertThat(piAction.type(), is(PiTableAction.Type.ACTION));
}
 
Example 12
Source File: PiActionParamTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the construction of a PiActionParam object.
 */
@Test
public void testConstruction() {
    ImmutableByteSequence value = copyFrom(0x0b010102);
    final PiActionParamId piActionParamId = PiActionParamId.of(SRC_ADDR);
    final PiActionParam piActionParam = new PiActionParam(piActionParamId, value);
    assertThat(piActionParam, is(notNullValue()));
    assertThat(piActionParam.id(), is(piActionParamId));
    assertThat(piActionParam.value(), is(value));
}
 
Example 13
Source File: MyTunnelApp.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Generates and insert a flow rule to perform the tunnel FORWARD/EGRESS
 * function for the given switch, output port address and tunnel ID.
 *
 * @param switchId switch ID
 * @param outPort  output port where to forward tunneled packets
 * @param tunId    tunnel ID
 * @param isEgress if true, perform tunnel egress action, otherwise forward
 *                 packet as is to port
 */
private void insertTunnelForwardRule(DeviceId switchId,
                                     PortNumber outPort,
                                     int tunId,
                                     boolean isEgress) {

    PiTableId tunnelForwardTableId = PiTableId.of("c_ingress.t_tunnel_fwd");

    // Exact match on tun_id
    PiMatchFieldId tunIdMatchFieldId = PiMatchFieldId.of("hdr.my_tunnel.tun_id");
    PiCriterion match = PiCriterion.builder()
            .matchExact(tunIdMatchFieldId, tunId)
            .build();

    // Action depend on isEgress parameter.
    // if true, perform tunnel egress action on the given outPort, otherwise
    // simply forward packet as is (set_out_port action).
    PiActionParamId portParamId = PiActionParamId.of("port");
    PiActionParam portParam = new PiActionParam(portParamId, (short) outPort.toLong());

    final PiAction action;
    if (isEgress) {
        // Tunnel egress action.
        // Remove MyTunnel header and forward to outPort.
        PiActionId egressActionId = PiActionId.of("c_ingress.my_tunnel_egress");
        action = PiAction.builder()
                .withId(egressActionId)
                .withParameter(portParam)
                .build();
    } else {
        // Tunnel transit action.
        // Forward the packet as is to outPort.
        /*
         * TODO EXERCISE: create action object for the transit case.
         * Look at the t_tunnel_fwd table in the P4 program. Which of the 3
         * actions can be used to simply set the output port? Get the full
         * action name from the P4Info file, and use that when creating the
         * PiActionId object. When creating the PiAction object, remember to
         * add all action parameters as defined in the P4 program.
         *
         * Hint: the code will be similar to the case when isEgress is true.
         */
        action = null; // Replace null with your solution.
    }

    log.info("Inserting {} rule on switch {}: table={}, match={}, action={}",
             isEgress ? "EGRESS" : "TRANSIT",
             switchId, tunnelForwardTableId, match, action);

    insertPiFlowRule(switchId, tunnelForwardTableId, match, action);
}