Java Code Examples for org.onosproject.net.Host#mac()

The following examples show how to use org.onosproject.net.Host#mac() . 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: OpenstackSwitchingHostProviderTest.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void addLocationToHost(HostId hostId, HostLocation location) {
    Host oldHost = hostMap.get(hostId);

    Set<HostLocation> newHostlocations = oldHost.locations();
    newHostlocations.add(location);

    Host newHost = new DefaultHost(oldHost.providerId(),
            oldHost.id(),
            oldHost.mac(),
            oldHost.vlan(),
            newHostlocations,
            oldHost.ipAddresses(),
            oldHost.innerVlan(),
            oldHost.tpid(),
            oldHost.configured(),
            oldHost.annotations());

    hostMap.put(hostId, newHost);
}
 
Example 2
Source File: BasicHostOperator.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a description of the given host.
 *
 * @param host the host
 * @return a description of the host
 */
public static HostDescription descriptionOf(Host host) {
    checkNotNull(host, "Must supply a non-null Host");
    return new DefaultHostDescription(host.mac(), host.vlan(), host.locations(),
                                      host.ipAddresses(), host.configured(),
                                      (SparseAnnotations) host.annotations());
}
 
Example 3
Source File: HostHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
private void processIntfVlanUpdatedEventInternal(Host host, DeviceId deviceId, PortNumber portNum,
                                                 VlanId vlanId, boolean popVlan, boolean install) {
    MacAddress mac = host.mac();
    VlanId hostVlanId = host.vlan();

    if (!install) {
        // Do not check the host validity. Just remove all rules corresponding to the vlan id
        // Revoke forwarding objective for bridging to the host
        srManager.defaultRoutingHandler.updateBridging(deviceId, portNum, mac, vlanId, popVlan, false);

        // Revoke forwarding objective and corresponding simple Next objective
        // for each Host and IP address connected to given port
        host.ipAddresses().forEach(ipAddress ->
            srManager.routingRulePopulator.updateFwdObj(deviceId, portNum, ipAddress.toIpPrefix(),
                                                        mac, vlanId, popVlan, false)
        );
    } else {
        // Check whether the host vlan is valid for new interface configuration
        if ((!popVlan && hostVlanId.equals(vlanId)) ||
                (popVlan && hostVlanId.equals(VlanId.NONE))) {
            srManager.defaultRoutingHandler.updateBridging(deviceId, portNum, mac, vlanId, popVlan, true);
            // Update Forwarding objective and corresponding simple Next objective
            // for each Host and IP address connected to given port
            host.ipAddresses().forEach(ipAddress ->
                srManager.routingRulePopulator.updateFwdObj(deviceId, portNum, ipAddress.toIpPrefix(),
                                                            mac, vlanId, popVlan, true)
            );
        }
    }
}
 
Example 4
Source File: HostMobility.java    From onos with Apache License 2.0 5 votes vote down vote up
private Collection<? extends FlowRule> cleanupDevice(Device device, Host host) {
    List<FlowRule> flowRules = Lists.newLinkedList();
    MacAddress mac = host.mac();
    for (FlowRule rule : flowRuleService.getFlowEntries(device.id())) {
        for (Criterion c : rule.selector().criteria()) {
            if (c.type() == Type.ETH_DST || c.type() == Type.ETH_SRC) {
                EthCriterion eth = (EthCriterion) c;
                if (eth.mac().equals(mac)) {
                    flowRules.add(rule);
                }
            }
        }
    }
    return flowRules;
}
 
Example 5
Source File: VplsNeighbourHandlerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Generates reply message context by given source and destination host.
 *
 * @param src the source host
 * @param dst the destination host
 * @return the reply message context
 */
private TestMessageContext makeReplyContext(Host src, Host dst) {
    return new TestMessageContext(src.location(),
                                  src.mac(),
                                  dst.mac(),
                                  src.vlan(),
                                  NeighbourMessageType.REPLY);
}
 
Example 6
Source File: VplsNeighbourHandlerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Generates broadcast request message context by given source host.
 *
 * @param host the source host
 * @return the request message context
 */
private TestMessageContext makeBroadcastRequestContext(Host host) {
    return new TestMessageContext(host.location(),
                                  host.mac(),
                                  MacAddress.BROADCAST,
                                  host.vlan(),
                                  NeighbourMessageType.REQUEST);
}
 
Example 7
Source File: DefaultInstancePort.java    From onos with Apache License 2.0 5 votes vote down vote up
private DefaultInstancePort(Host host, State state,
                            DeviceId oldDeviceId, PortNumber oldPortNumber) {
    this.networkId = host.annotations().value(ANNOTATION_NETWORK_ID);
    this.portId = host.annotations().value(ANNOTATION_PORT_ID);
    this.macAddress = host.mac();

    this.ipAddress = host.ipAddresses().stream().findFirst().orElse(null);

    this.deviceId = host.location().deviceId();
    this.portNumber = host.location().port();
    this.state = state;
    this.oldDeviceId = oldDeviceId;
    this.oldPortNumber = oldPortNumber;
}
 
Example 8
Source File: L2BridgingComponent.java    From ngsdn-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * Insert flow rules to forward packets to a given host located at the given
 * device and port.
 * <p>
 * This method will be called at component activation for each host known by
 * ONOS, and every time a new host-added event is captured by the
 * InternalHostListener defined below.
 *
 * @param host     host instance
 * @param deviceId device where the host is located
 * @param port     port where the host is attached to
 */
private void learnHost(Host host, DeviceId deviceId, PortNumber port) {

    log.info("Adding L2 unicast rule on {} for host {} (port {})...",
             deviceId, host.id(), port);

    final String tableId = "IngressPipeImpl.l2_exact_table";
    // Match exactly on the host MAC address.
    final MacAddress hostMac = host.mac();
    final PiCriterion hostMacCriterion = PiCriterion.builder()
            .matchExact(PiMatchFieldId.of("hdr.ethernet.dst_addr"),
                        hostMac.toBytes())
            .build();

    // Action: set output port
    final PiAction l2UnicastAction = PiAction.builder()
            .withId(PiActionId.of("IngressPipeImpl.set_egress_port"))
            .withParameter(new PiActionParam(
                    PiActionParamId.of("port_num"),
                    port.toLong()))
            .build();

    // Forge flow rule.
    final FlowRule rule = Utils.buildFlowRule(
            deviceId, appId, tableId, hostMacCriterion, l2UnicastAction);

    // Insert.
    flowRuleService.applyFlowRules(rule);
}
 
Example 9
Source File: StatsFlowRuleManager.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Gets MAC Address of host.
 *
 * @param ipAddress IP Address of host
 * @return MAC Address of host
 */
private MacAddress getMacAddress(IpAddress ipAddress) {
    if (!hostService.getHostsByIp(ipAddress).isEmpty()) {
        Host host = hostService.getHostsByIp(ipAddress).stream().findAny().get();
        return host.mac();
    }

    return NO_HOST_MAC;
}
 
Example 10
Source File: HostsWebResource.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and adds new host based on given data and returns its host ID.
 *
 * @param node JsonNode containing host information
 * @return host ID of new host created
 */
private HostId parseHost(JsonNode node) {
    Host host = codec(Host.class).decode((ObjectNode) node, HostsWebResource.this);

    HostId hostId = host.id();
    DefaultHostDescription desc = new DefaultHostDescription(
            host.mac(), host.vlan(), host.locations(), host.ipAddresses(), host.innerVlan(),
            host.tpid(), host.configured(), (SparseAnnotations) host.annotations());
    hostProviderService.hostDetected(hostId, desc, false);

    return hostId;
}
 
Example 11
Source File: DefaultHostProbe.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs DefaultHostProbe with given retry.
 *
 * @param host host to be probed
 * @param connectPoint location to be verified
 * @param probeMac source MAC address of the probe
 * @param mode probe mode
 * @param retry number of retry
 */
DefaultHostProbe(Host host, ConnectPoint connectPoint, ProbeMode mode, MacAddress probeMac, int retry) {
    super(host.providerId(), host.id(), host.mac(), host.vlan(), host.locations(), host.ipAddresses(),
            host.configured());

    this.connectPoint = connectPoint;
    this.mode = mode;
    this.probeMac = probeMac;
    this.retry = retry;
}
 
Example 12
Source File: L2BridgingComponent.java    From onos-p4-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * Insert flow rules to forward packets to a given host located at the given
 * device and port.
 * <p>
 * This method will be called at component activation for each host known by
 * ONOS, and every time a new host-added event is captured by the
 * InternalHostListener defined below.
 *
 * @param host     host instance
 * @param deviceId device where the host is located
 * @param port     port where the host is attached to
 */
private void learnHost(Host host, DeviceId deviceId, PortNumber port) {

    log.info("Adding L2 unicast rule on {} for host {} (port {})...",
             deviceId, host.id(), port);

    // TODO EXERCISE 2
    // 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";
    // Match exactly on the host MAC address.
    final MacAddress hostMac = host.mac();
    final PiCriterion hostMacCriterion = PiCriterion.builder()
            .matchExact(PiMatchFieldId.of("hdr.ethernet.dst_addr"),
                        hostMac.toBytes())
            .build();

    // Action: set output port
    final PiAction l2UnicastAction = PiAction.builder()
            .withId(PiActionId.of("IngressPipeImpl.set_output_port"))
            .withParameter(new PiActionParam(
                    PiActionParamId.of("port_num"),
                    port.toLong()))
            .build();

    // Forge flow rule.
    final FlowRule rule = Utils.buildFlowRule(
            deviceId, appId, tableId, hostMacCriterion, l2UnicastAction);
    // ---- END SOLUTION ----

    // Insert.
    flowRuleService.applyFlowRules(rule);
}
 
Example 13
Source File: EvpnManager.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * update or withdraw evpn route from route admin service.
 *
 * @param host          host
 * @param vpnInstanceId vpn instance id
 * @param privateLabel  private label
 * @param type          operation type
 */
private void setPrivateRoute(Host host, VpnInstanceId vpnInstanceId,
                             Label privateLabel,
                             Operation type) {
    DeviceId deviceId = host.location().deviceId();
    Device device = deviceService.getDevice(deviceId);
    VpnInstance vpnInstance = vpnInstanceService.getInstance(vpnInstanceId);
    RouteDistinguisher rd = vpnInstance.routeDistinguisher();
    Set<VpnRouteTarget> importRouteTargets
            = vpnInstance.getImportRouteTargets();
    Set<VpnRouteTarget> exportRouteTargets
            = vpnInstance.getExportRouteTargets();
    EvpnInstanceName instanceName = vpnInstance.vpnInstanceName();
    String url = device.annotations().value(SWITCH_CHANNEL_ID);
    String controllerIp = url.substring(0, url.lastIndexOf(":"));

    if (controllerIp == null) {
        log.error(CANT_FIND_CONTROLLER_DEVICE, device.id().toString());
        return;
    }
    IpAddress ipAddress = IpAddress.valueOf(controllerIp);
    // create private route
    EvpnInstanceNextHop evpnNextHop = EvpnInstanceNextHop
            .evpnNextHop(ipAddress, privateLabel);
    EvpnInstancePrefix evpnPrefix = EvpnInstancePrefix
            .evpnPrefix(host.mac(), IpPrefix.valueOf(host.ipAddresses()
                                                             .iterator()
                                                             .next()
                                                             .getIp4Address(), 32));
    EvpnInstanceRoute evpnPrivateRoute
            = new EvpnInstanceRoute(instanceName,
                                    rd,
                                    new LinkedList<>(importRouteTargets),
                                    new LinkedList<>(exportRouteTargets),
                                    evpnPrefix,
                                    evpnNextHop,
                                    IpPrefix.valueOf(host.ipAddresses()
                                                             .iterator()
                                                             .next()
                                                             .getIp4Address(), 32),
                                    ipAddress,
                                    privateLabel);

    // change to public route
    EvpnRoute evpnRoute
            = new EvpnRoute(Source.LOCAL,
                            host.mac(),
                            IpPrefix.valueOf(host.ipAddresses()
                                                     .iterator()
                                                     .next()
                                                     .getIp4Address(), 32),
                            ipAddress,
                            rd,
                            new LinkedList<>(importRouteTargets),
                            new LinkedList<>(exportRouteTargets),
                            privateLabel);
    if (type.equals(Objective.Operation.ADD)) {
        //evpnRouteAdminService.update(Sets.newHashSet(evpnPrivateRoute));
        evpnInstanceRoutes.add(evpnPrivateRoute);
        evpnRouteAdminService.update(Sets.newHashSet(evpnRoute));

    } else {
        //evpnRouteAdminService.withdraw(Sets.newHashSet(evpnPrivateRoute));
        evpnInstanceRoutes.remove(evpnPrivateRoute);
        evpnRouteAdminService.withdraw(Sets.newHashSet(evpnRoute));
    }
}
 
Example 14
Source File: Ipv6RoutingComponent.java    From onos-p4-tutorial with Apache License 2.0 4 votes vote down vote up
/**
 * Sets up the given device with the necessary rules to route packets to the
 * given host.
 *
 * @param deviceId deviceId the device ID
 * @param host     the host
 */
private void setUpHostRules(DeviceId deviceId, Host host) {

    // Get all IPv6 addresses associated to this host. In this tutorial we
    // use hosts with only 1 IPv6 address.
    final Collection<Ip6Address> hostIpv6Addrs = host.ipAddresses().stream()
            .filter(IpAddress::isIp6)
            .map(IpAddress::getIp6Address)
            .collect(Collectors.toSet());

    if (hostIpv6Addrs.isEmpty()) {
        // Ignore.
        log.debug("No IPv6 addresses for host {}, ignore", host.id());
        return;
    } else {
        log.info("Adding routes on {} for host {} [{}]",
                 deviceId, host.id(), hostIpv6Addrs);
    }

    // Create an ECMP group with only one member, where the group ID is
    // derived from the host MAC.
    final MacAddress hostMac = host.mac();
    int groupId = macToGroupId(hostMac);

    final GroupDescription group = createNextHopGroup(
            groupId, Collections.singleton(hostMac), deviceId);

    // Map each host IPV6 address to corresponding /128 prefix and obtain a
    // flow rule that points to the group ID. In this tutorial we expect
    // only one flow rule per host.
    final List<FlowRule> flowRules = hostIpv6Addrs.stream()
            .map(IpAddress::toIpPrefix)
            .filter(IpPrefix::isIp6)
            .map(IpPrefix::getIp6Prefix)
            .map(prefix -> createRoutingRule(deviceId, prefix, groupId))
            .collect(Collectors.toList());

    // Helper function to install flows after groups, since here flows
    // points to the group and P4Runtime enforces this dependency during
    // write operations.
    insertInOrder(group, flowRules);
}
 
Example 15
Source File: ReactiveRoutingFib.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void setUpConnectivityHostToHost(IpAddress dstIpAddress,
                                        IpAddress srcIpAddress,
                                        MacAddress srcMacAddress,
                                        ConnectPoint srcConnectPoint) {
    checkNotNull(dstIpAddress);
    checkNotNull(srcIpAddress);
    checkNotNull(srcMacAddress);
    checkNotNull(srcConnectPoint);

    IpPrefix srcIpPrefix = srcIpAddress.toIpPrefix();
    IpPrefix dstIpPrefix = dstIpAddress.toIpPrefix();
    ConnectPoint dstConnectPoint = null;
    MacAddress dstMacAddress = null;

    for (Host host : hostService.getHostsByIp(dstIpAddress)) {
        if (host.mac() != null) {
            dstMacAddress = host.mac();
            dstConnectPoint = host.location();
            break;
        }
    }
    if (dstMacAddress == null) {
        hostService.startMonitoringIp(dstIpAddress);
        return;
    }

    //
    // Handle intent from source host to destination host
    //
    MultiPointToSinglePointIntent srcToDstIntent =
            hostToHostIntentGenerator(dstIpAddress, dstConnectPoint,
                    dstMacAddress, srcConnectPoint);
    submitReactiveIntent(dstIpPrefix, srcToDstIntent);

    //
    // Handle intent from destination host to source host
    //

    // Since we proactively handle the intent from destination host to
    // source host, we should check whether there is an exiting intent
    // first.
    if (mp2pIntentExists(srcIpPrefix)) {
        updateExistingMp2pIntent(srcIpPrefix, dstConnectPoint);
        return;
    } else {
        // There is no existing intent, create a new one.
        MultiPointToSinglePointIntent dstToSrcIntent =
                hostToHostIntentGenerator(srcIpAddress, srcConnectPoint,
                        srcMacAddress, dstConnectPoint);
        submitReactiveIntent(srcIpPrefix, dstToSrcIntent);
    }
}
 
Example 16
Source File: Ipv6RoutingComponent.java    From onos-p4-tutorial with Apache License 2.0 4 votes vote down vote up
/**
 * Sets up the given device with the necessary rules to route packets to the
 * given host.
 *
 * @param deviceId deviceId the device ID
 * @param host     the host
 */
private void setUpHostRules(DeviceId deviceId, Host host) {

    // Get all IPv6 addresses associated to this host. In this tutorial we
    // use hosts with only 1 IPv6 address.
    final Collection<Ip6Address> hostIpv6Addrs = host.ipAddresses().stream()
            .filter(IpAddress::isIp6)
            .map(IpAddress::getIp6Address)
            .collect(Collectors.toSet());

    if (hostIpv6Addrs.isEmpty()) {
        // Ignore.
        log.debug("No IPv6 addresses for host {}, ignore", host.id());
        return;
    } else {
        log.info("Adding routes on {} for host {} [{}]",
                 deviceId, host.id(), hostIpv6Addrs);
    }

    // Create an ECMP group with only one member, where the group ID is
    // derived from the host MAC.
    final MacAddress hostMac = host.mac();
    int groupId = macToGroupId(hostMac);

    final GroupDescription group = createNextHopGroup(
            groupId, Collections.singleton(hostMac), deviceId);

    // Map each host IPV6 address to corresponding /128 prefix and obtain a
    // flow rule that points to the group ID. In this tutorial we expect
    // only one flow rule per host.
    final List<FlowRule> flowRules = hostIpv6Addrs.stream()
            .map(IpAddress::toIpPrefix)
            .filter(IpPrefix::isIp6)
            .map(IpPrefix::getIp6Prefix)
            .map(prefix -> createRoutingRule(deviceId, prefix, groupId))
            .collect(Collectors.toList());

    // Helper function to install flows after groups, since here flows
    // points to the group and P4Runtime enforces this dependency during
    // write operations.
    insertInOrder(group, flowRules);
}
 
Example 17
Source File: HostHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
private void processHostRemoved(Host host) {
    MacAddress hostMac = host.mac();
    VlanId hostVlanId = host.vlan();
    Set<HostLocation> locations = effectiveLocations(host);
    Set<IpAddress> ips = host.ipAddresses();
    log.info("Host {}/{} is removed from {}", hostMac, hostVlanId, locations);

    locations.forEach(location -> {
        if (isDoubleTaggedHost(host)) {
            ips.forEach(ip ->
                processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
                                               host.innerVlan(), hostVlanId, host.tpid(), ip, true)
            );
        } else {
            processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, true);
            ips.forEach(ip ->
                processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, true)
            );
        }

        // Also remove redirection flows on the pair device if exists.
        Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(location.deviceId());
        Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(location.deviceId());
        if (pairDeviceId.isPresent() && pairLocalPort.isPresent()) {
            // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
            //       when the host is untagged
            VlanId vlanId = vlanForPairPort(hostVlanId, location);
            if (vlanId == null) {
                return;
            }

            processBridgingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId, true);
            ips.forEach(ip ->
                    processRoutingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId,
                            ip, true));
        }

        // Delete prefix from sr-device-subnet when the next hop host is removed
        srManager.routeService.getRouteTables().forEach(tableId -> {
            srManager.routeService.getRoutes(tableId).forEach(routeInfo -> {
                if (routeInfo.allRoutes().stream().anyMatch(rr -> ips.contains(rr.nextHop()))) {
                    log.debug("HostRemoved. removeSubnet {}, {}", location, routeInfo.prefix());
                    srManager.deviceConfiguration.removeSubnet(location, routeInfo.prefix());
                }
            });
        });

    });
}
 
Example 18
Source File: HostHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
private void processHostUpdatedEventInternal(HostEvent event) {
    Host host = event.subject();
    MacAddress hostMac = host.mac();
    VlanId hostVlanId = host.vlan();
    EthType hostTpid = host.tpid();
    Set<HostLocation> locations = effectiveLocations(host);
    Set<IpAddress> prevIps = event.prevSubject().ipAddresses();
    Set<IpAddress> newIps = host.ipAddresses();
    log.info("Host {}/{} is updated", hostMac, hostVlanId);

    locations.forEach(location -> {
        Sets.difference(prevIps, newIps).forEach(ip -> {
            if (isDoubleTaggedHost(host)) {
                processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
                                               host.innerVlan(), hostVlanId, hostTpid, ip, true);
            } else {
                processRoutingRule(location.deviceId(), location.port(), hostMac,
                                   hostVlanId, ip, true);
            }
        });
        Sets.difference(newIps, prevIps).forEach(ip -> {
            if (isDoubleTaggedHost(host)) {
                processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
                                               host.innerVlan(), hostVlanId, hostTpid, ip, false);
            } else {
                processRoutingRule(location.deviceId(), location.port(), hostMac,
                                   hostVlanId, ip, false);
            }
        });
    });

    // Use the pair link temporarily before the second location of a dual-homed host shows up.
    // This do not affect single-homed hosts since the flow will be blocked in
    // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively
    locations.forEach(location ->
        srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> {
            if (locations.stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) {
                Set<IpAddress> ipsToAdd = Sets.difference(newIps, prevIps);
                Set<IpAddress> ipsToRemove = Sets.difference(prevIps, newIps);

                srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> {
                    // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
                    //       when the host is untagged
                    VlanId vlanId = vlanForPairPort(hostVlanId, location);
                    if (vlanId == null) {
                        return;
                    }

                    ipsToRemove.forEach(ip ->
                            processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, true)
                    );
                    ipsToAdd.forEach(ip ->
                            processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, false)
                    );

                    if (srManager.activeProbing) {
                        probe(host, location, pairDeviceId, pairRemotePort);
                    }
                });
            }
        })
    );
}
 
Example 19
Source File: Ipv6RoutingComponent.java    From ngsdn-tutorial with Apache License 2.0 4 votes vote down vote up
/**
 * Sets up the given device with the necessary rules to route packets to the
 * given host.
 *
 * @param deviceId deviceId the device ID
 * @param host     the host
 */
private void setUpHostRules(DeviceId deviceId, Host host) {

    // Get all IPv6 addresses associated to this host. In this tutorial we
    // use hosts with only 1 IPv6 address.
    final Collection<Ip6Address> hostIpv6Addrs = host.ipAddresses().stream()
            .filter(IpAddress::isIp6)
            .map(IpAddress::getIp6Address)
            .collect(Collectors.toSet());

    if (hostIpv6Addrs.isEmpty()) {
        // Ignore.
        log.debug("No IPv6 addresses for host {}, ignore", host.id());
        return;
    } else {
        log.info("Adding routes on {} for host {} [{}]",
                 deviceId, host.id(), hostIpv6Addrs);
    }

    // Create an ECMP group with only one member, where the group ID is
    // derived from the host MAC.
    final MacAddress hostMac = host.mac();
    int groupId = macToGroupId(hostMac);

    final GroupDescription group = createNextHopGroup(
            groupId, Collections.singleton(hostMac), deviceId);

    // Map each host IPV6 address to corresponding /128 prefix and obtain a
    // flow rule that points to the group ID. In this tutorial we expect
    // only one flow rule per host.
    final List<FlowRule> flowRules = hostIpv6Addrs.stream()
            .map(IpAddress::toIpPrefix)
            .filter(IpPrefix::isIp6)
            .map(IpPrefix::getIp6Prefix)
            .map(prefix -> createRoutingRule(deviceId, prefix, groupId))
            .collect(Collectors.toList());

    // Helper function to install flows after groups, since here flows
    // points to the group and P4Runtime enforces this dependency during
    // write operations.
    insertInOrder(group, flowRules);
}
 
Example 20
Source File: TopologySimulator.java    From onos with Apache License 2.0 2 votes vote down vote up
/**
 * Produces a host description from the given host.
 *
 * @param host host to copy
 * @return host description
 */
static DefaultHostDescription description(Host host) {
    return new DefaultHostDescription(host.mac(), host.vlan(), host.location(),
                                      host.ipAddresses());
}