Java Code Examples for org.onlab.packet.MacAddress#toBytes()

The following examples show how to use org.onlab.packet.MacAddress#toBytes() . 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: DhcpRelayManagerTest.java    From onos with Apache License 2.0 6 votes vote down vote up
private byte[] buildInterfaceId(MacAddress clientMac, short vlanId, ConnectPoint clientCp) {
    String inPortString = "-" + clientCp.toString() + ":";
    byte[] clientSoureMacBytes = clientMac.toBytes();
    byte[] inPortStringBytes = inPortString.getBytes();
    byte[] vlanIdBytes = new byte[2];
    vlanIdBytes[0] = (byte) ((vlanId >> 8) & 0xff);  // high-order byte first
    vlanIdBytes[1] = (byte) (vlanId & 0xff);
    byte[] interfaceIdBytes = new byte[clientSoureMacBytes.length +  inPortStringBytes.length + vlanIdBytes.length];

    System.arraycopy(clientSoureMacBytes, 0, interfaceIdBytes, 0, clientSoureMacBytes.length);
    System.arraycopy(inPortStringBytes, 0, interfaceIdBytes, clientSoureMacBytes.length, inPortStringBytes.length);
    System.arraycopy(vlanIdBytes, 0, interfaceIdBytes, clientSoureMacBytes.length + inPortStringBytes.length,
            vlanIdBytes.length);

    return interfaceIdBytes;
}
 
Example 2
Source File: TroubleshootUtils.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if the Mac Address is inside a range between the min MAC and the mask.
 * @param macAddress the MAC address to check
 * @param minAddr the min MAC address
 * @param maskAddr the mask
 * @return true if in range, false otherwise.
 */
static boolean compareMac(MacAddress macAddress, MacAddress minAddr, MacAddress maskAddr) {
    byte[] mac = macAddress.toBytes();
    byte[] min = minAddr.toBytes();
    byte[] mask = maskAddr.toBytes();
    boolean inRange = true;

    int i = 0;

    //if mask is 00 stop
    while (inRange && i < mask.length && (mask[i] & 0xFF) != 0) {
        int ibmac = mac[i] & 0xFF;
        int ibmin = min[i] & 0xFF;
        int ibmask = mask[i] & 0xFF;
        if (ibmask == 255) {
            inRange = ibmac == ibmin;
        } else if (ibmac < ibmin || ibmac >= ibmask) {
            inRange = false;
            break;
        }
        i++;
    }

    return inRange;
}
 
Example 3
Source File: PiCriterionTranslatorsTest.java    From onos with Apache License 2.0 6 votes vote down vote up
@Test
public void testEthCriterion() throws Exception {
    MacAddress value1 = MacAddress.valueOf(random.nextLong());
    MacAddress value2 = MacAddress.valueOf(random.nextLong());
    MacAddress mask = MacAddress.valueOf(random.nextLong());
    int bitWidth = value1.toBytes().length * 8;

    EthCriterion criterion = (EthCriterion) Criteria.matchEthDst(value1);
    PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);

    EthCriterion maskedCriterion = (EthCriterion) Criteria.matchEthDstMasked(value2, mask);
    PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) translateCriterion(maskedCriterion, fieldId, TERNARY,
            bitWidth);

    assertThat(exactMatch.value().asArray(), is(criterion.mac().toBytes()));
    assertThat(ternaryMatch.value().asArray(), is(maskedCriterion.mac().toBytes()));
    assertThat(ternaryMatch.mask().asArray(), is(maskedCriterion.mask().toBytes()));
}
 
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("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 5
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 6
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 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: PiCriterionTranslatorsTest.java    From onos with Apache License 2.0 5 votes vote down vote up
@Test
public void testIPv6NDLinkLayerAddressCriterion() throws Exception {
    MacAddress mac = MacAddress.valueOf(random.nextLong());
    int bitWidth = mac.toBytes().length * 8;

    IPv6NDLinkLayerAddressCriterion criterion = (IPv6NDLinkLayerAddressCriterion) Criteria
            .matchIPv6NDSourceLinkLayerAddress(mac);

    PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);

    assertThat(exactMatch.value().asArray(), is(criterion.mac().toBytes()));
}
 
Example 9
Source File: PiCriterionTranslatorsTest.java    From onos with Apache License 2.0 5 votes vote down vote up
@Test
public void testArpHaCriterionn() throws Exception {
    MacAddress mac = MacAddress.valueOf(random.nextLong());
    int bitWidth = mac.toBytes().length * 8;

    ArpHaCriterion criterion = (ArpHaCriterion) Criteria.matchArpTha(mac);

    PiExactFieldMatch exactMatch = (PiExactFieldMatch) translateCriterion(criterion, fieldId, EXACT, bitWidth);

    assertThat(exactMatch.value().asArray(), is(criterion.mac().toBytes()));
}