Java Code Examples for org.onosproject.net.intf.Interface#connectPoint()

The following examples show how to use org.onosproject.net.intf.Interface#connectPoint() . 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
public TestArpRequestPacketContext(Interface fromInterface) {
    super(0, null, null, false);
    ARP arp = new ARP();
    arp.setOpCode(ARP.OP_REQUEST);

    IpAddress targetIp = fromInterface.ipAddressesList().get(0).ipAddress();
    arp.setTargetProtocolAddress(targetIp.toOctets());
    arp.setTargetHardwareAddress(MacAddress.BROADCAST.toBytes());
    arp.setSenderHardwareAddress(MacAddress.NONE.toBytes());
    arp.setSenderProtocolAddress(Ip4Address.valueOf(0).toOctets());
    arp.setHardwareAddressLength((byte) MacAddress.MAC_ADDRESS_LENGTH);
    Ethernet eth = new Ethernet();
    eth.setEtherType(Ethernet.TYPE_ARP);
    eth.setSourceMACAddress(MacAddress.NONE);
    eth.setDestinationMACAddress(MacAddress.BROADCAST);
    eth.setVlanID(fromInterface.vlan().toShort());
    eth.setPayload(arp);

    this.inPacket = new DefaultInboundPacket(fromInterface.connectPoint(), eth,
                                             ByteBuffer.wrap(eth.serialize()));
}
 
Example 2
Source File: TunnellingConnectivityManager.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Forwards a BGP packet to another connect point.
 *
 * @param context the packet context of the incoming packet
 */
private void forward(PacketContext context) {
    ConnectPoint outputPort = null;

    IPv4 ipv4 = (IPv4) context.inPacket().parsed().getPayload();
    IpAddress dstAddress = IpAddress.valueOf(ipv4.getDestinationAddress());

    if (context.inPacket().receivedFrom().equals(bgpSpeaker.connectPoint())) {
        if (bgpSpeaker.peers().contains(dstAddress)) {
            Interface intf = interfaceService.getMatchingInterface(dstAddress);
            if (intf != null) {
                outputPort = intf.connectPoint();
            }
        }
    } else {
        Set<Interface> interfaces =
                interfaceService.getInterfacesByPort(context.inPacket().receivedFrom());

        if (interfaces.stream()
                .flatMap(intf -> intf.ipAddressesList().stream())
                .anyMatch(ia -> ia.ipAddress().equals(dstAddress))) {
            outputPort = bgpSpeaker.connectPoint();
        }
    }

    if (outputPort != null) {
        TrafficTreatment t = DefaultTrafficTreatment.builder()
                .setOutput(outputPort.port()).build();
        OutboundPacket o = new DefaultOutboundPacket(
                outputPort.deviceId(), t, context.inPacket().unparsed());
        packetService.emit(o);
    }
}
 
Example 3
Source File: Dhcp4HandlerImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Writes DHCP record to the store according to the response DHCP packet (Offer, Ack).
 *
 * @param ethernet the DHCP packet
 * @param dhcpPayload the DHCP payload
 */
private void writeResponseDhcpRecord(Ethernet ethernet,
                                     DHCP dhcpPayload) {
    Optional<Interface> outInterface = getClientInterface(ethernet, dhcpPayload);
    if (!outInterface.isPresent()) {
        log.warn("Failed to determine where to send {}", dhcpPayload.getPacketType());
        return;
    }

    Interface outIface = outInterface.get();
    ConnectPoint location = outIface.connectPoint();
    VlanId vlanId = getVlanIdFromRelayAgentOption(dhcpPayload);
    if (vlanId == null) {
        vlanId = outIface.vlan();
    }
    MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
    HostId hostId = HostId.hostId(macAddress, vlanId);
    DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);
    if (record == null) {
        record = new DhcpRecord(HostId.hostId(macAddress, vlanId));
    } else {
        record = record.clone();
    }
    record.addLocation(new HostLocation(location, System.currentTimeMillis()));
    if (dhcpPayload.getPacketType() == DHCP.MsgType.DHCPACK) {
        record.ip4Address(Ip4Address.valueOf(dhcpPayload.getYourIPAddress()));
    }
    record.ip4Status(dhcpPayload.getPacketType());
    record.setDirectlyConnected(directlyConnected(dhcpPayload));
    record.updateLastSeen();
    dhcpRelayStore.updateDhcpRecord(HostId.hostId(macAddress, vlanId), record);
}
 
Example 4
Source File: SdnIpReactiveRouting.java    From onos with Apache License 2.0 5 votes vote down vote up
public ConnectPoint getEgressConnectPoint(IpAddress dstIpAddress) {
    LocationType type = getLocationType(dstIpAddress);
    if (type == LocationType.LOCAL) {
        Set<Host> hosts = hostService.getHostsByIp(dstIpAddress);
        if (!hosts.isEmpty()) {
            return hosts.iterator().next().location();
        } else {
            hostService.startMonitoringIp(dstIpAddress);
            return null;
        }
    } else if (type == LocationType.INTERNET) {
        IpAddress nextHopIpAddress = null;
        Route route = routeService.longestPrefixMatch(dstIpAddress);
        if (route != null) {
            nextHopIpAddress = route.nextHop();
            Interface it = interfaceService.getMatchingInterface(nextHopIpAddress);
            if (it != null) {
                return it.connectPoint();
            } else {
                return null;
            }
        } else {
            return null;
        }
    } else {
        return null;
    }
}
 
Example 5
Source File: SimpleFabricForwarding.java    From onos with Apache License 2.0 5 votes vote down vote up
private FilteredConnectPoint buildFilteredConnectedPoint(Interface iface) {
    Objects.requireNonNull(iface);
    TrafficSelector.Builder trafficSelector = DefaultTrafficSelector.builder();

    if (iface.vlan() != null && !iface.vlan().equals(VlanId.NONE)) {
        trafficSelector.matchVlanId(iface.vlan());
    }
    return new FilteredConnectPoint(iface.connectPoint(), trafficSelector.build());
}
 
Example 6
Source File: VplsIntentUtility.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Builds filtered connected point by a given network interface.
 *
 * @param iface the network interface
 * @return the filtered connected point of a given network interface
 */
static FilteredConnectPoint buildFilteredConnectedPoint(Interface iface) {
    Objects.requireNonNull(iface);
    TrafficSelector.Builder trafficSelector = DefaultTrafficSelector.builder();

    if (iface.vlan() != null && !iface.vlan().equals(VlanId.NONE)) {
        trafficSelector.matchVlanId(iface.vlan());
    }

    return new FilteredConnectPoint(iface.connectPoint(), trafficSelector.build());
}
 
Example 7
Source File: SdnIpFib.java    From onos with Apache License 2.0 5 votes vote down vote up
private void addInterface(Interface intf) {
    synchronized (this) {
        for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : routeIntents.entrySet()) {
            // Retrieve the IP prefix and affected intent
            IpPrefix prefix = entry.getKey();
            MultiPointToSinglePointIntent intent = entry.getValue();

            // Add new ingress FilteredConnectPoint
            Set<FilteredConnectPoint> ingressFilteredCPs =
                    Sets.newHashSet(intent.filteredIngressPoints());

            // Create the new traffic selector
            TrafficSelector.Builder selector =
                    buildIngressTrafficSelector(intf, prefix);

            // Create the Filtered ConnectPoint and add it to the existing set
            FilteredConnectPoint newIngressFilteredCP =
                    new FilteredConnectPoint(intf.connectPoint(), selector.build());
            ingressFilteredCPs.add(newIngressFilteredCP);

            // Create new intent
            MultiPointToSinglePointIntent newIntent =
                    MultiPointToSinglePointIntent.builder(intent)
                            .filteredIngressPoints(ingressFilteredCPs)
                            .build();

            routeIntents.put(entry.getKey(), newIntent);
            intentSynchronizer.submit(newIntent);
        }
    }
}
 
Example 8
Source File: Dhcp4HandlerImpl.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Send the DHCP ack to the requester host.
 * Modify Host or Route store according to the type of DHCP.
 *
 * @param ethernetPacketAck the packet
 * @param dhcpPayload the DHCP data
 */
private void handleDhcpAck(Ethernet ethernetPacketAck, DHCP dhcpPayload) {
    Optional<Interface> outInterface = getClientInterface(ethernetPacketAck, dhcpPayload);
    if (!outInterface.isPresent()) {
        log.warn("Can't find output interface for dhcp: {}", dhcpPayload);
        return;
    }

    Interface outIface = outInterface.get();
    HostLocation hostLocation = new HostLocation(outIface.connectPoint(), System.currentTimeMillis());
    MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
    VlanId vlanId = getVlanIdFromRelayAgentOption(dhcpPayload);
    if (vlanId == null) {
        vlanId = outIface.vlan();
    }
    HostId hostId = HostId.hostId(macAddress, vlanId);
    Ip4Address ip = Ip4Address.valueOf(dhcpPayload.getYourIPAddress());

    if (directlyConnected(dhcpPayload)) {
        // Add to host store if it connect to network directly
        Set<IpAddress> ips = Sets.newHashSet(ip);
        Host host = hostService.getHost(hostId);

        Set<HostLocation> hostLocations = Sets.newHashSet(hostLocation);
        if (host != null) {
            // Dual homing support:
            // if host exists, use old locations and new location
            hostLocations.addAll(host.locations());
        }
        HostDescription desc = new DefaultHostDescription(macAddress, vlanId,
                                                          hostLocations, ips, false);
        // Add IP address when dhcp server give the host new ip address
        providerService.hostDetected(hostId, desc, false);
    } else {
        // Add to route store if it does not connect to network directly
        // Get gateway host IP according to host mac address
        // TODO: remove relay store here
        DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);

        if (record == null) {
            log.warn("Can't find DHCP record of host {}", hostId);
            return;
        }

        MacAddress gwMac = record.nextHop().orElse(null);
        if (gwMac == null) {
            log.warn("Can't find gateway mac address from record {}", record);
            return;
        }

        HostId gwHostId = HostId.hostId(gwMac, record.vlanId());
        Host gwHost = hostService.getHost(gwHostId);

        if (gwHost == null) {
            log.warn("Can't find gateway host {}", gwHostId);
            return;
        }

        Ip4Address nextHopIp = gwHost.ipAddresses()
                .stream()
                .filter(IpAddress::isIp4)
                .map(IpAddress::getIp4Address)
                .findFirst()
                .orElse(null);

        if (nextHopIp == null) {
            log.warn("Can't find IP address of gateway {}", gwHost);
            return;
        }

        Route route = new Route(Route.Source.DHCP, ip.toIpPrefix(), nextHopIp);
        routeStore.replaceRoute(route);
    }
}
 
Example 9
Source File: SdnIpFib.java    From onos with Apache License 2.0 4 votes vote down vote up
private void removeInterface(Interface intf) {
    synchronized (this) {
        for (Map.Entry<IpPrefix, MultiPointToSinglePointIntent> entry : routeIntents.entrySet()) {
            // Retrieve the IP prefix and intent possibly affected
            IpPrefix prefix = entry.getKey();
            MultiPointToSinglePointIntent intent = entry.getValue();

            // The interface removed might be an ingress interface, so the
            // selector needs to match on the interface tagging params and
            // on the prefix
            TrafficSelector.Builder ingressSelector =
                    buildIngressTrafficSelector(intf, prefix);
            FilteredConnectPoint removedIngressFilteredCP =
                    new FilteredConnectPoint(intf.connectPoint(),
                                             ingressSelector.build());

            // The interface removed might be an egress interface, so the
            // selector needs to match only on the interface tagging params
            TrafficSelector.Builder selector = buildTrafficSelector(intf);
            FilteredConnectPoint removedEgressFilteredCP =
                    new FilteredConnectPoint(intf.connectPoint(), selector.build());

            if (intent.filteredEgressPoint().equals(removedEgressFilteredCP)) {
                 // The interface is an egress interface for the intent.
                 // This intent just lost its head. Remove it and let higher
                 // layer routing reroute
                intentSynchronizer.withdraw(routeIntents.remove(entry.getKey()));
            } else {
                if (intent.filteredIngressPoints().contains(removedIngressFilteredCP)) {
                     // The FilteredConnectPoint is an ingress
                     // FilteredConnectPoint for the intent
                    Set<FilteredConnectPoint> ingressFilteredCPs =
                            Sets.newHashSet(intent.filteredIngressPoints());

                    // Remove FilteredConnectPoint from the existing set
                    ingressFilteredCPs.remove(removedIngressFilteredCP);

                    if (!ingressFilteredCPs.isEmpty()) {
                         // There are still ingress points. Create a new
                         // intent and resubmit
                        MultiPointToSinglePointIntent newIntent =
                                MultiPointToSinglePointIntent.builder(intent)
                                        .filteredIngressPoints(ingressFilteredCPs)
                                        .build();

                        routeIntents.put(entry.getKey(), newIntent);
                        intentSynchronizer.submit(newIntent);
                    } else {
                         // No more ingress FilteredConnectPoint. Withdraw
                         //the intent
                        intentSynchronizer.withdraw(routeIntents.remove(entry.getKey()));
                    }
                }
            }
        }
    }
}