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

The following examples show how to use org.onosproject.net.pi.model.PiMatchFieldId#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: FieldMatchCodec.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public PiFieldMatch decode(
        P4RuntimeOuterClass.FieldMatch message, P4InfoOuterClass.Preamble tablePreamble,
        PiPipeconf pipeconf, P4InfoBrowser browser)
        throws CodecException, P4InfoBrowser.NotFoundException {

    String fieldMatchName = browser.matchFields(tablePreamble.getId())
            .getById(message.getFieldId()).getName();
    PiMatchFieldId headerFieldId = PiMatchFieldId.of(fieldMatchName);

    P4RuntimeOuterClass.FieldMatch.FieldMatchTypeCase typeCase = message.getFieldMatchTypeCase();

    switch (typeCase) {
        case EXACT:
            P4RuntimeOuterClass.FieldMatch.Exact exactFieldMatch = message.getExact();
            ImmutableByteSequence exactValue = copyFrom(exactFieldMatch.getValue().asReadOnlyByteBuffer());
            return new PiExactFieldMatch(headerFieldId, exactValue);
        case TERNARY:
            P4RuntimeOuterClass.FieldMatch.Ternary ternaryFieldMatch = message.getTernary();
            ImmutableByteSequence ternaryValue = copyFrom(ternaryFieldMatch.getValue().asReadOnlyByteBuffer());
            ImmutableByteSequence ternaryMask = copyFrom(ternaryFieldMatch.getMask().asReadOnlyByteBuffer());
            return new PiTernaryFieldMatch(headerFieldId, ternaryValue, ternaryMask);
        case LPM:
            P4RuntimeOuterClass.FieldMatch.LPM lpmFieldMatch = message.getLpm();
            ImmutableByteSequence lpmValue = copyFrom(lpmFieldMatch.getValue().asReadOnlyByteBuffer());
            int lpmPrefixLen = lpmFieldMatch.getPrefixLen();
            return new PiLpmFieldMatch(headerFieldId, lpmValue, lpmPrefixLen);
        case RANGE:
            P4RuntimeOuterClass.FieldMatch.Range rangeFieldMatch = message.getRange();
            ImmutableByteSequence rangeHighValue = copyFrom(rangeFieldMatch.getHigh().asReadOnlyByteBuffer());
            ImmutableByteSequence rangeLowValue = copyFrom(rangeFieldMatch.getLow().asReadOnlyByteBuffer());
            return new PiRangeFieldMatch(headerFieldId, rangeLowValue, rangeHighValue);
        default:
            throw new CodecException(format(
                    "Decoding of field match type '%s' not implemented", typeCase.name()));
    }
}
 
Example 2
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 3
Source File: PiMatchFieldIdTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the construction of a PiMatchFieldId object.
 */
@Test
public void testConstruction() {
    final String name = IPV4_HEADER_NAME  + DOT + DST_ADDR;
    final PiMatchFieldId piMatchFieldId = PiMatchFieldId.of(name);
    assertThat(piMatchFieldId, is(notNullValue()));
    assertThat(piMatchFieldId.id(), is(name));
}
 
Example 4
Source File: PiMatchFieldIdTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the construction of a PiMatchFieldId object with index.
 */
@Test
public void testConstructionWithIndex() {
    final String name = IPV4_HEADER_NAME + DOT + DST_ADDR + "[1]";
    final PiMatchFieldId piMatchFieldId = PiMatchFieldId.of(name);
    assertThat(piMatchFieldId, is(notNullValue()));
    assertThat(piMatchFieldId.id(), is(name));
}
 
Example 5
Source File: PiLpmFieldMatchTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the construction of a PiLpmFieldMatch object.
 */
@Test
public void testConstruction() {
    final ImmutableByteSequence value = copyFrom(0x0a01010a);
    int prefix = 24;
    final PiMatchFieldId piMatchField = PiMatchFieldId.of(IPV4_HEADER_NAME + DOT + DST_ADDR);
    PiLpmFieldMatch piLpmFieldMatch = new PiLpmFieldMatch(piMatchField, value, prefix);
    assertThat(piLpmFieldMatch, is(notNullValue()));
    assertThat(piLpmFieldMatch.value(), is(value));
    assertThat(piLpmFieldMatch.prefixLength(), is(prefix));
    assertThat(piLpmFieldMatch.type(), is(PiMatchType.LPM));
}
 
Example 6
Source File: PiRangeFieldMatchTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the construction of a PiRangeFieldMatch object.
 */
@Test
public void testConstruction() {
    final ImmutableByteSequence high = copyFrom(0x50);
    final ImmutableByteSequence low = copyFrom(0x00);
    final PiMatchFieldId piMatchField = PiMatchFieldId.of(VLAN_HEADER_NAME + DOT + VID);
    PiRangeFieldMatch piRangeFieldMatch = new PiRangeFieldMatch(piMatchField, low, high);
    assertThat(piRangeFieldMatch, is(notNullValue()));
    assertThat(piRangeFieldMatch.lowValue(), is(low));
    assertThat(piRangeFieldMatch.highValue(), is(high));
    assertThat(piRangeFieldMatch.type(), is(PiMatchType.RANGE));
}
 
Example 7
Source File: PiTernaryFieldMatchTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the construction of a PiTernaryFieldMatch object.
 */
@Test
public void testConstruction() {
    final ImmutableByteSequence value = copyFrom(0x0a01010a);
    final ImmutableByteSequence mask = copyFrom(0x00ffffff);
    final PiMatchFieldId piMatchField = PiMatchFieldId.of(IPV4_HEADER_NAME + DOT + DST_ADDR);
    PiTernaryFieldMatch piTernaryFieldMatch = new PiTernaryFieldMatch(piMatchField, value, mask);
    assertThat(piTernaryFieldMatch, is(notNullValue()));
    assertThat(piTernaryFieldMatch.value(), is(value));
    assertThat(piTernaryFieldMatch.mask(), is(mask));
    assertThat(piTernaryFieldMatch.type(), is(PiMatchType.TERNARY));
}
 
Example 8
Source File: PiTableEntryTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Tests creation of a DefaultFlowRule using a FlowRule constructor.
 */
@Test
public void testBuilder() {

    PiTableId piTableId = PiTableId.of("table10");
    long cookie = 0xfff0323;
    int priority = 100;
    double timeout = 1000;
    PiMatchFieldId piMatchFieldId = PiMatchFieldId.of(IPV4_HEADER_NAME + DOT + DST_ADDR);
    PiFieldMatch piFieldMatch = new PiExactFieldMatch(piMatchFieldId, ImmutableByteSequence.copyFrom(0x0a010101));
    PiAction piAction = PiAction.builder().withId(PiActionId.of(DROP)).build();
    final Map<PiMatchFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap();
    fieldMatches.put(piMatchFieldId, piFieldMatch);
    final PiTableEntry piTableEntry = PiTableEntry.builder()
            .forTable(piTableId)
            .withMatchKey(PiMatchKey.builder()
                                  .addFieldMatches(fieldMatches.values())
                                  .build())
            .withAction(piAction)
            .withCookie(cookie)
            .withPriority(priority)
            .withTimeout(timeout)
            .build();

    assertThat(piTableEntry.table(), is(piTableId));
    assertThat(piTableEntry.cookie(), is(cookie));
    assertThat("Priority must be set", piTableEntry.priority().isPresent());
    assertThat("Timeout must be set", piTableEntry.timeout().isPresent());
    assertThat(piTableEntry.priority().getAsInt(), is(priority));
    assertThat(piTableEntry.timeout().get(), is(timeout));
    assertThat("Incorrect match param value",
               CollectionUtils.isEqualCollection(piTableEntry.matchKey().fieldMatches(), fieldMatches.values()));
    assertThat(piTableEntry.action(), is(piAction));
}
 
Example 9
Source File: PiFieldMatchTest.java    From onos with Apache License 2.0 5 votes vote down vote up
@Test
public void basics() {
    final PiMatchFieldId piMatchField = PiMatchFieldId.of(ETH_HEADER_NAME + DOT + ETH_TYPE);
    PiFieldMatch piFieldMatch = new PiExactFieldMatch(piMatchField, copyFrom(0x0806));

    assertEquals(piFieldMatch.fieldId(), piMatchField);
    assertEquals(piFieldMatch.type(), PiMatchType.EXACT);
}
 
Example 10
Source File: PiExactFieldMatchTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the construction of a PiExactFieldMatch object.
 */
@Test
public void testConstruction() {
    final ImmutableByteSequence value = copyFrom(0x0806);
    final PiMatchFieldId piMatchField = PiMatchFieldId.of(ETH_HEADER_NAME + DOT + ETH_TYPE);
    PiExactFieldMatch piExactFieldMatch = new PiExactFieldMatch(piMatchField, value);
    assertThat(piExactFieldMatch, is(notNullValue()));
    assertThat(piExactFieldMatch.value(), is(value));
    assertThat(piExactFieldMatch.type(), is(PiMatchType.EXACT));
}
 
Example 11
Source File: CriterionCodecTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Tests protocol-independent type criterion encoding.
 */
@Test
public void matchPiTypeEncodingTest() {

    PiMatchFieldId ethMatchFieldId = PiMatchFieldId.of("ethernet_t.etherType");
    byte[] matchExactBytes1 = {0x08, 0x00};
    Criterion exactBytesCriterion = PiCriterion.builder().matchExact(ethMatchFieldId, matchExactBytes1).build();
    ObjectNode exactResult = criterionCodec.encode(exactBytesCriterion, context);
    assertThat(exactResult, matchesCriterion(exactBytesCriterion));

    PiMatchFieldId ipv4MatchFieldId = PiMatchFieldId.of("ipv4_t.dstAddr");
    int mask = 0x00ffffff;
    byte[] matchLpmBytes1 = {0x0a, 0x01, 0x01, 0x01};
    Criterion lpmBytesCriterion = PiCriterion.builder().matchLpm(ipv4MatchFieldId, matchLpmBytes1, mask).build();
    ObjectNode lpmResult = criterionCodec.encode(lpmBytesCriterion, context);
    assertThat(lpmResult, matchesCriterion(lpmBytesCriterion));

    byte[] matchTernaryBytes1 = {0x0a, 0x01, 0x01, 0x01};
    byte[] matchTernaryMaskBytes = {0x7f, 0x7f, 0x7f, 0x00};
    Criterion ternaryBytesCriterion = PiCriterion.builder().matchTernary(ipv4MatchFieldId, matchTernaryBytes1,
                                                                matchTernaryMaskBytes).build();
    ObjectNode ternaryResult = criterionCodec.encode(ternaryBytesCriterion, context);
    assertThat(ternaryResult, matchesCriterion(ternaryBytesCriterion));

    byte[] matchRangeBytes1 = {0x10};
    byte[] matchRangeHighBytes = {0x30};
    Criterion rangeBytesCriterion = PiCriterion.builder()
            .matchRange(ipv4MatchFieldId, matchRangeBytes1, matchRangeHighBytes).build();
    ObjectNode rangeResult = criterionCodec.encode(rangeBytesCriterion, context);
    assertThat(rangeResult, matchesCriterion(rangeBytesCriterion));
}
 
Example 12
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);
}