Search in sources :

Example 36 with MacAddress

use of org.onlab.packet.MacAddress in project onos by opennetworkinglab.

the class PiCriterionTranslatorsTest method testIPv6NDLinkLayerAddressCriterion.

@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()));
}
Also used : IPv6NDLinkLayerAddressCriterion(org.onosproject.net.flow.criteria.IPv6NDLinkLayerAddressCriterion) PiExactFieldMatch(org.onosproject.net.pi.runtime.PiExactFieldMatch) MacAddress(org.onlab.packet.MacAddress) Test(org.junit.Test)

Example 37 with MacAddress

use of org.onlab.packet.MacAddress in project onos by opennetworkinglab.

the class PiFlowRuleTranslatorImplTest method testTranslateFlowRules.

@Test
public void testTranslateFlowRules() throws Exception {
    ApplicationId appId = new DefaultApplicationId(1, "test");
    MacAddress ethDstMac = MacAddress.valueOf(random.nextLong());
    MacAddress ethSrcMac = MacAddress.valueOf(random.nextLong());
    short ethType = (short) (0x0000FFFF & random.nextInt());
    short outPort = (short) random.nextInt(65);
    short inPort = (short) random.nextInt(65);
    int timeout = random.nextInt(100);
    int priority = random.nextInt(100);
    TrafficSelector matchInPort1 = DefaultTrafficSelector.builder().matchInPort(PortNumber.portNumber(inPort)).matchEthDst(ethDstMac).matchEthSrc(ethSrcMac).matchEthType(ethType).build();
    TrafficSelector emptySelector = DefaultTrafficSelector.builder().build();
    TrafficTreatment outPort2 = DefaultTrafficTreatment.builder().setOutput(PortNumber.portNumber(outPort)).build();
    FlowRule rule1 = DefaultFlowRule.builder().forDevice(DEVICE_ID).forTable(INGRESS_TABLE0_CONTROL_TABLE0).fromApp(appId).withSelector(matchInPort1).withTreatment(outPort2).makeTemporary(timeout).withPriority(priority).build();
    FlowRule rule2 = DefaultFlowRule.builder().forDevice(DEVICE_ID).forTable(INGRESS_TABLE0_CONTROL_TABLE0).fromApp(appId).withSelector(matchInPort1).withTreatment(outPort2).makeTemporary(timeout).withPriority(priority).build();
    FlowRule defActionRule = DefaultFlowRule.builder().forDevice(DEVICE_ID).forTable(INGRESS_TABLE0_CONTROL_TABLE0).fromApp(appId).withSelector(emptySelector).withTreatment(outPort2).makeTemporary(timeout).withPriority(priority).build();
    PiTableEntry entry1 = PiFlowRuleTranslatorImpl.translate(rule1, pipeconf, null);
    PiTableEntry entry2 = PiFlowRuleTranslatorImpl.translate(rule2, pipeconf, null);
    PiTableEntry defActionEntry = PiFlowRuleTranslatorImpl.translate(defActionRule, pipeconf, null);
    // check equality, i.e. same rules must produce same entries
    new EqualsTester().addEqualityGroup(rule1, rule2).addEqualityGroup(entry1, entry2).testEquals();
    // parse values stored in entry1
    PiTernaryFieldMatch inPortParam = (PiTernaryFieldMatch) entry1.matchKey().fieldMatch(HDR_STANDARD_METADATA_INGRESS_PORT).get();
    PiTernaryFieldMatch ethDstParam = (PiTernaryFieldMatch) entry1.matchKey().fieldMatch(HDR_HDR_ETHERNET_DST_ADDR).get();
    PiTernaryFieldMatch ethSrcParam = (PiTernaryFieldMatch) entry1.matchKey().fieldMatch(HDR_HDR_ETHERNET_SRC_ADDR).get();
    PiTernaryFieldMatch ethTypeParam = (PiTernaryFieldMatch) entry1.matchKey().fieldMatch(HDR_HDR_ETHERNET_ETHER_TYPE).get();
    Optional<Double> expectedTimeout = pipeconf.pipelineModel().table(INGRESS_TABLE0_CONTROL_TABLE0).get().supportsAging() ? Optional.of((double) rule1.timeout()) : Optional.empty();
    // check that values stored in entry are the same used for the flow rule
    assertThat("Incorrect inPort match param value", inPortParam.value().asReadOnlyBuffer().getShort(), is(equalTo(inPort)));
    assertThat("Incorrect inPort match param mask", inPortParam.mask().asReadOnlyBuffer().getShort(), is(equalTo(IN_PORT_MASK)));
    assertThat("Incorrect ethDestMac match param value", ethDstParam.value().asArray(), is(equalTo(ethDstMac.toBytes())));
    assertThat("Incorrect ethDestMac match param mask", ethDstParam.mask().asArray(), is(equalTo(MacAddress.BROADCAST.toBytes())));
    assertThat("Incorrect ethSrcMac match param value", ethSrcParam.value().asArray(), is(equalTo(ethSrcMac.toBytes())));
    assertThat("Incorrect ethSrcMac match param mask", ethSrcParam.mask().asArray(), is(equalTo(MacAddress.BROADCAST.toBytes())));
    assertThat("Incorrect ethType match param value", ethTypeParam.value().asReadOnlyBuffer().getShort(), is(equalTo(ethType)));
    assertThat("Incorrect ethType match param mask", ethTypeParam.mask().asReadOnlyBuffer().getShort(), is(equalTo(ETH_TYPE_MASK)));
    // FIXME: re-enable when P4Runtime priority handling will be moved out of transltion service
    // see PiFlowRuleTranslatorImpl
    // assertThat("Incorrect priority value",
    // entry1.priority().get(), is(equalTo(MAX_PI_PRIORITY - rule1.priority())));
    assertThat("Incorrect timeout value", entry1.timeout(), is(equalTo(expectedTimeout)));
    assertThat("Match key should be empty", defActionEntry.matchKey(), is(equalTo(PiMatchKey.EMPTY)));
    assertThat("Priority should not be set", !defActionEntry.priority().isPresent());
}
Also used : EqualsTester(com.google.common.testing.EqualsTester) PiTernaryFieldMatch(org.onosproject.net.pi.runtime.PiTernaryFieldMatch) MacAddress(org.onlab.packet.MacAddress) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) DefaultApplicationId(org.onosproject.core.DefaultApplicationId) PiTableEntry(org.onosproject.net.pi.runtime.PiTableEntry) TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) DefaultFlowRule(org.onosproject.net.flow.DefaultFlowRule) FlowRule(org.onosproject.net.flow.FlowRule) DefaultApplicationId(org.onosproject.core.DefaultApplicationId) ApplicationId(org.onosproject.core.ApplicationId) Test(org.junit.Test)

Example 38 with MacAddress

use of org.onlab.packet.MacAddress in project onos by opennetworkinglab.

the class K8sRoutingArpHandler method processArpPacket.

private void processArpPacket(PacketContext context, Ethernet ethernet) {
    DeviceId deviceId = context.inPacket().receivedFrom().deviceId();
    if (!allK8sDevices(k8sNodeService, k8sHostService).contains(deviceId)) {
        return;
    }
    ARP arp = (ARP) ethernet.getPayload();
    if (arp.getOpCode() == ARP.OP_REPLY) {
        IpAddress spa = Ip4Address.valueOf(arp.getSenderProtocolAddress());
        MacAddress sha = MacAddress.valueOf(arp.getSenderHardwareAddress());
        log.info("ARP reply from ip: {}, mac: {}", spa, sha);
        Set<IpAddress> gatewayIps = k8sNodeService.completeNodes().stream().map(K8sNode::extGatewayIp).collect(Collectors.toSet());
        if (!gatewayIps.contains(spa)) {
            return;
        }
        log.info("ARP reply from external gateway ip: {}, mac: {}", spa, sha);
        k8sNodeService.completeNodes().stream().filter(n -> n.extGatewayMac() == null).forEach(n -> {
            K8sNode updated = n.updateExtGatewayMac(sha);
            k8sNodeService.updateNode(updated);
        });
    }
}
Also used : CoreService(org.onosproject.core.CoreService) K8sNodeAdminService(org.onosproject.k8snode.api.K8sNodeAdminService) Tools.groupedThreads(org.onlab.util.Tools.groupedThreads) ARP(org.onlab.packet.ARP) K8sNodeListener(org.onosproject.k8snode.api.K8sNodeListener) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) ByteBuffer(java.nio.ByteBuffer) Ethernet(org.onlab.packet.Ethernet) Component(org.osgi.service.component.annotations.Component) TrafficSelector(org.onosproject.net.flow.TrafficSelector) EXT_ENTRY_TABLE(org.onosproject.k8snetworking.api.Constants.EXT_ENTRY_TABLE) ApplicationId(org.onosproject.core.ApplicationId) Thread.sleep(java.lang.Thread.sleep) K8sNodeEvent(org.onosproject.k8snode.api.K8sNodeEvent) Activate(org.osgi.service.component.annotations.Activate) K8sNode(org.onosproject.k8snode.api.K8sNode) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) ExecutorService(java.util.concurrent.ExecutorService) K8S_NETWORKING_APP_ID(org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID) IpAddress(org.onlab.packet.IpAddress) NodeId(org.onosproject.cluster.NodeId) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) Ip4Address(org.onlab.packet.Ip4Address) Logger(org.slf4j.Logger) Deactivate(org.osgi.service.component.annotations.Deactivate) VlanId(org.onlab.packet.VlanId) PacketProcessor(org.onosproject.net.packet.PacketProcessor) PRIORITY_ARP_REPLY_RULE(org.onosproject.k8snetworking.api.Constants.PRIORITY_ARP_REPLY_RULE) Set(java.util.Set) K8sFlowRuleService(org.onosproject.k8snetworking.api.K8sFlowRuleService) PacketService(org.onosproject.net.packet.PacketService) Executors.newSingleThreadExecutor(java.util.concurrent.Executors.newSingleThreadExecutor) Collectors(java.util.stream.Collectors) K8sHostAdminService(org.onosproject.k8snode.api.K8sHostAdminService) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) Objects(java.util.Objects) PRIORITY_ARP_POD_RULE(org.onosproject.k8snetworking.api.Constants.PRIORITY_ARP_POD_RULE) InboundPacket(org.onosproject.net.packet.InboundPacket) PacketContext(org.onosproject.net.packet.PacketContext) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) K8sNetworkingUtil.allK8sDevices(org.onosproject.k8snetworking.util.K8sNetworkingUtil.allK8sDevices) MacAddress(org.onlab.packet.MacAddress) ClusterService(org.onosproject.cluster.ClusterService) DeviceId(org.onosproject.net.DeviceId) Reference(org.osgi.service.component.annotations.Reference) LeadershipService(org.onosproject.cluster.LeadershipService) DefaultOutboundPacket(org.onosproject.net.packet.DefaultOutboundPacket) K8sNode(org.onosproject.k8snode.api.K8sNode) DeviceId(org.onosproject.net.DeviceId) IpAddress(org.onlab.packet.IpAddress) MacAddress(org.onlab.packet.MacAddress) ARP(org.onlab.packet.ARP)

Example 39 with MacAddress

use of org.onlab.packet.MacAddress in project onos by opennetworkinglab.

the class K8sSwitchingArpHandler method processArpRequest.

private void processArpRequest(PacketContext context, Ethernet ethPacket) {
    ARP arpPacket = (ARP) ethPacket.getPayload();
    K8sPort srcK8sPort = k8sNetworkService.ports().stream().filter(p -> p.macAddress().equals(ethPacket.getSourceMAC())).findAny().orElse(null);
    PortNumber srcPortNum = context.inPacket().receivedFrom().port();
    DeviceId srcDeviceId = context.inPacket().receivedFrom().deviceId();
    boolean isEntryPort = false;
    for (K8sNode node : k8sNodeService.completeNodes()) {
        if (srcDeviceId.equals(node.intgBridge()) && srcPortNum.equals(node.intgEntryPortNum())) {
            isEntryPort = true;
        }
    }
    // integration bridge entry port, we simply ignore the ARP request...
    if (srcK8sPort == null && !isEntryPort) {
        log.warn("Failed to find source port(MAC:{})", ethPacket.getSourceMAC());
        return;
    }
    IpAddress targetIp = Ip4Address.valueOf(arpPacket.getTargetProtocolAddress());
    // look up the MAC address from regular k8s ports
    MacAddress replyMac = k8sNetworkService.ports().stream().filter(p -> p.ipAddress().equals(targetIp)).map(K8sPort::macAddress).findAny().orElse(null);
    // look up the MAC address from special integration entry port (e.g., LOCAL, k8s-int-os)
    for (K8sNetwork network : k8sNetworkService.networks()) {
        if (network.gatewayIp().equals(targetIp)) {
            K8sNode node = k8sNodeService.node(network.name());
            replyMac = node.intgEntryPortMac();
        }
    }
    if (replyMac == null) {
        String cidr = k8sNetworkService.networks().stream().map(K8sNetwork::cidr).findAny().orElse(null);
        if (cidr != null) {
            String unshiftedIp = unshiftIpDomain(targetIp.toString(), SHIFTED_IP_PREFIX, cidr);
            replyMac = k8sNetworkService.ports().stream().filter(p -> p.ipAddress().equals(IpAddress.valueOf(unshiftedIp))).map(K8sPort::macAddress).findAny().orElse(null);
        }
    }
    if (replyMac == null) {
        Set<String> serviceIps = k8sServiceService.services().stream().map(s -> s.getSpec().getClusterIP()).collect(Collectors.toSet());
        if (serviceIps.contains(targetIp.toString())) {
            replyMac = MacAddress.valueOf(SERVICE_FAKE_MAC_STR);
        }
    }
    if (replyMac == null) {
        if (targetIp.toString().startsWith(NODE_IP_PREFIX)) {
            String targetIpPrefix = targetIp.toString().split("\\.")[1];
            String nodePrefix = NODE_IP_PREFIX + "." + targetIpPrefix;
            String origNodeCidr = k8sNodeService.completeNodes().stream().map(n -> n.nodeIp().toString()).findAny().orElse(null);
            if (origNodeCidr != null) {
                String origNodeIp = unshiftIpDomain(targetIp.toString(), nodePrefix, origNodeCidr);
                IpPrefix k8sNodeIpCidr = IpPrefix.valueOf(IpAddress.valueOf(origNodeCidr), 24);
                SubnetUtils k8sNodeSubnet = new SubnetUtils(k8sNodeIpCidr.toString());
                String k8sNodeGateway = getGatewayIp(k8sNodeIpCidr.toString()).toString();
                String seekIp = "";
                if (!k8sNodeSubnet.getInfo().isInRange(origNodeIp)) {
                    replyMac = extHostMacStore.asJavaMap().get(IpAddress.valueOf(k8sNodeGateway));
                    seekIp = k8sNodeGateway;
                } else {
                    replyMac = extHostMacStore.asJavaMap().get(IpAddress.valueOf(origNodeIp));
                    seekIp = origNodeIp;
                }
                // we need to manually learn their MAC addresses
                if (replyMac == null) {
                    ConnectPoint cp = context.inPacket().receivedFrom();
                    K8sNode k8sNode = k8sNodeService.node(cp.deviceId());
                    if (k8sNode != null) {
                        // we use fake IP and MAC address as a source to
                        // query destination MAC address
                        setArpRequest(MacAddress.valueOf(NODE_FAKE_MAC_STR).toBytes(), IpAddress.valueOf(NODE_FAKE_IP_STR).toOctets(), IpAddress.valueOf(seekIp).toOctets(), k8sNode);
                        context.block();
                        return;
                    }
                }
            }
        }
    }
    if (replyMac == null) {
        replyMac = MacAddress.valueOf(gatewayMac);
    }
    Ethernet ethReply = ARP.buildArpReply(targetIp.getIp4Address(), replyMac, ethPacket);
    TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(context.inPacket().receivedFrom().port()).build();
    packetService.emit(new DefaultOutboundPacket(context.inPacket().receivedFrom().deviceId(), treatment, ByteBuffer.wrap(ethReply.serialize())));
    context.block();
}
Also used : ConsistentMap(org.onosproject.store.service.ConsistentMap) GATEWAY_MAC(org.onosproject.k8snetworking.impl.OsgiPropertyConstants.GATEWAY_MAC) ARP_TABLE(org.onosproject.k8snetworking.api.Constants.ARP_TABLE) SERVICE_FAKE_MAC_STR(org.onosproject.k8snetworking.api.Constants.SERVICE_FAKE_MAC_STR) CoreService(org.onosproject.core.CoreService) K8sNodeAdminService(org.onosproject.k8snode.api.K8sNodeAdminService) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) LoggerFactory(org.slf4j.LoggerFactory) Tools.groupedThreads(org.onlab.util.Tools.groupedThreads) NODE_IP_PREFIX(org.onosproject.k8snetworking.api.Constants.NODE_IP_PREFIX) ARP(org.onlab.packet.ARP) K8sNodeListener(org.onosproject.k8snode.api.K8sNodeListener) ARP_MODE(org.onosproject.k8snetworking.impl.OsgiPropertyConstants.ARP_MODE) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) ByteBuffer(java.nio.ByteBuffer) ConnectPoint(org.onosproject.net.ConnectPoint) Ethernet(org.onlab.packet.Ethernet) StorageService(org.onosproject.store.service.StorageService) INET(org.onlab.packet.IpAddress.Version.INET) ApplicationId(org.onosproject.core.ApplicationId) KryoNamespaces(org.onosproject.store.serializers.KryoNamespaces) MastershipService(org.onosproject.mastership.MastershipService) NodeId(org.onosproject.cluster.NodeId) K8sNetworkingUtil.getGatewayIp(org.onosproject.k8snetworking.util.K8sNetworkingUtil.getGatewayIp) Serializer(org.onosproject.store.service.Serializer) Ip4Address(org.onlab.packet.Ip4Address) Deactivate(org.osgi.service.component.annotations.Deactivate) PacketProcessor(org.onosproject.net.packet.PacketProcessor) NODE_FAKE_MAC_STR(org.onosproject.k8snetworking.api.Constants.NODE_FAKE_MAC_STR) Set(java.util.Set) K8sFlowRuleService(org.onosproject.k8snetworking.api.K8sFlowRuleService) PacketService(org.onosproject.net.packet.PacketService) Executors.newSingleThreadExecutor(java.util.concurrent.Executors.newSingleThreadExecutor) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) EthType(org.onlab.packet.EthType) PacketContext(org.onosproject.net.packet.PacketContext) ARP_BROADCAST_MODE(org.onosproject.k8snetworking.api.Constants.ARP_BROADCAST_MODE) ClusterService(org.onosproject.cluster.ClusterService) DeviceId(org.onosproject.net.DeviceId) IpPrefix(org.onlab.packet.IpPrefix) Dictionary(java.util.Dictionary) K8sNetworkingUtil.unshiftIpDomain(org.onosproject.k8snetworking.util.K8sNetworkingUtil.unshiftIpDomain) Tools(org.onlab.util.Tools) K8sServiceService(org.onosproject.k8snetworking.api.K8sServiceService) ComponentContext(org.osgi.service.component.ComponentContext) KryoNamespace(org.onlab.util.KryoNamespace) K8sNetworkService(org.onosproject.k8snetworking.api.K8sNetworkService) ARP_MODE_DEFAULT(org.onosproject.k8snetworking.impl.OsgiPropertyConstants.ARP_MODE_DEFAULT) K8sNodeInfo(org.onosproject.k8snode.api.K8sNodeInfo) Component(org.osgi.service.component.annotations.Component) TrafficSelector(org.onosproject.net.flow.TrafficSelector) K8sPort(org.onosproject.k8snetworking.api.K8sPort) K8sNodeEvent(org.onosproject.k8snode.api.K8sNodeEvent) Activate(org.osgi.service.component.annotations.Activate) K8sNetworkingUtil.getPropertyValue(org.onosproject.k8snetworking.util.K8sNetworkingUtil.getPropertyValue) K8sNode(org.onosproject.k8snode.api.K8sNode) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) ExecutorService(java.util.concurrent.ExecutorService) K8S_NETWORKING_APP_ID(org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID) IpAddress(org.onlab.packet.IpAddress) ComponentConfigService(org.onosproject.cfg.ComponentConfigService) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) PRIORITY_ARP_CONTROL_RULE(org.onosproject.k8snetworking.api.Constants.PRIORITY_ARP_CONTROL_RULE) Logger(org.slf4j.Logger) SHIFTED_IP_PREFIX(org.onosproject.k8snetworking.api.Constants.SHIFTED_IP_PREFIX) VlanId(org.onlab.packet.VlanId) K8sHostService(org.onosproject.k8snode.api.K8sHostService) ARP_PROXY_MODE(org.onosproject.k8snetworking.api.Constants.ARP_PROXY_MODE) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ConfigProperty(org.onosproject.cfg.ConfigProperty) K8sNetwork(org.onosproject.k8snetworking.api.K8sNetwork) Modified(org.osgi.service.component.annotations.Modified) GATEWAY_MAC_DEFAULT(org.onosproject.k8snetworking.impl.OsgiPropertyConstants.GATEWAY_MAC_DEFAULT) K8sNetworkingUtil.allK8sDevices(org.onosproject.k8snetworking.util.K8sNetworkingUtil.allK8sDevices) MacAddress(org.onlab.packet.MacAddress) Reference(org.osgi.service.component.annotations.Reference) NODE_FAKE_IP_STR(org.onosproject.k8snetworking.api.Constants.NODE_FAKE_IP_STR) LeadershipService(org.onosproject.cluster.LeadershipService) SubnetUtils(org.apache.commons.net.util.SubnetUtils) DefaultOutboundPacket(org.onosproject.net.packet.DefaultOutboundPacket) SubnetUtils(org.apache.commons.net.util.SubnetUtils) DeviceId(org.onosproject.net.DeviceId) DefaultOutboundPacket(org.onosproject.net.packet.DefaultOutboundPacket) MacAddress(org.onlab.packet.MacAddress) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) ConnectPoint(org.onosproject.net.ConnectPoint) IpPrefix(org.onlab.packet.IpPrefix) K8sNode(org.onosproject.k8snode.api.K8sNode) K8sNetwork(org.onosproject.k8snetworking.api.K8sNetwork) Ethernet(org.onlab.packet.Ethernet) K8sPort(org.onosproject.k8snetworking.api.K8sPort) IpAddress(org.onlab.packet.IpAddress) PortNumber(org.onosproject.net.PortNumber) ARP(org.onlab.packet.ARP)

Example 40 with MacAddress

use of org.onlab.packet.MacAddress in project onos by opennetworkinglab.

the class K8sSwitchingHostProvider method processPortAdded.

/**
 * Processes port addition event.
 *
 * @param port port object used in ONOS
 */
private void processPortAdded(Port port) {
    K8sPort k8sPort = portToK8sPortByName(port);
    if (k8sPort == null) {
        k8sPort = portToK8sPortByMac(port);
        if (k8sPort == null) {
            log.warn(ERR_ADD_HOST + "Kubernetes port for {} not found", port);
            return;
        }
    }
    K8sNetwork k8sNet = k8sNetworkService.network(k8sPort.networkId());
    if (k8sNet == null) {
        log.warn(ERR_ADD_HOST + "Kubernetes network {} not found", k8sPort.networkId());
        return;
    }
    MacAddress mac = k8sPort.macAddress();
    HostId hostId = HostId.hostId(mac);
    // connect point is the combination of switch ID with port number where
    // the host is attached to
    ConnectPoint connectPoint = new ConnectPoint(port.element().id(), port.number());
    long createTime = System.currentTimeMillis();
    // update k8s port number by referring to ONOS port number
    k8sNetworkService.updatePort(k8sPort.updatePortNumber(port.number()).updateState(K8sPort.State.ACTIVE));
    // we check whether the host already attached to same locations
    Host host = hostService.getHost(hostId);
    // build host annotations to include a set of meta info from neutron
    DefaultAnnotations.Builder annotations = DefaultAnnotations.builder().set(ANNOTATION_NETWORK_ID, k8sPort.networkId()).set(ANNOTATION_PORT_ID, k8sPort.portId()).set(ANNOTATION_CREATE_TIME, String.valueOf(createTime)).set(ANNOTATION_SEGMENT_ID, k8sNet.segmentId());
    HostDescription hostDesc = new DefaultHostDescription(mac, VlanId.NONE, new HostLocation(connectPoint, createTime), ImmutableSet.of(k8sPort.ipAddress()), annotations.build());
    if (host != null) {
        Set<HostLocation> locations = host.locations().stream().filter(l -> l.deviceId().equals(connectPoint.deviceId())).filter(l -> l.port().equals(connectPoint.port())).collect(Collectors.toSet());
        // therefore, we simply add this into the location list
        if (locations.isEmpty()) {
            hostProviderService.addLocationToHost(hostId, new HostLocation(connectPoint, createTime));
        }
        // the hostDetected method invocation in turn triggers host Update event
        if (locations.size() == 1) {
            hostProviderService.hostDetected(hostId, hostDesc, false);
        }
    } else {
        hostProviderService.hostDetected(hostId, hostDesc, false);
    }
}
Also used : HostLocation(org.onosproject.net.HostLocation) K8sNetworkingUtil.existingContainerPortByName(org.onosproject.k8snetworking.util.K8sNetworkingUtil.existingContainerPortByName) CoreService(org.onosproject.core.CoreService) DeviceService(org.onosproject.net.device.DeviceService) LoggerFactory(org.slf4j.LoggerFactory) Tools.groupedThreads(org.onlab.util.Tools.groupedThreads) K8sNodeListener(org.onosproject.k8snode.api.K8sNodeListener) K8sNetworkListener(org.onosproject.k8snetworking.api.K8sNetworkListener) ConnectPoint(org.onosproject.net.ConnectPoint) HostProviderService(org.onosproject.net.host.HostProviderService) Port(org.onosproject.net.Port) PORT_MAC(org.onosproject.net.AnnotationKeys.PORT_MAC) K8sNetworkingUtil.isContainer(org.onosproject.k8snetworking.util.K8sNetworkingUtil.isContainer) MastershipService(org.onosproject.mastership.MastershipService) ANNOTATION_CREATE_TIME(org.onosproject.k8snetworking.api.Constants.ANNOTATION_CREATE_TIME) ImmutableSet(com.google.common.collect.ImmutableSet) Device(org.onosproject.net.Device) Deactivate(org.osgi.service.component.annotations.Deactivate) Set(java.util.Set) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) GENEVE(org.onosproject.k8snetworking.api.Constants.GENEVE) DeviceEvent(org.onosproject.net.device.DeviceEvent) K8sNetworkAdminService(org.onosproject.k8snetworking.api.K8sNetworkAdminService) DeviceId(org.onosproject.net.DeviceId) HostDescription(org.onosproject.net.host.HostDescription) INIT(org.onosproject.k8snode.api.K8sNodeState.INIT) HostProviderRegistry(org.onosproject.net.host.HostProviderRegistry) Tools(org.onlab.util.Tools) Host(org.onosproject.net.Host) PORT_NAME(org.onosproject.net.AnnotationKeys.PORT_NAME) GRE(org.onosproject.k8snetworking.api.Constants.GRE) HostService(org.onosproject.net.host.HostService) Strings(com.google.common.base.Strings) DefaultHostDescription(org.onosproject.net.host.DefaultHostDescription) Component(org.osgi.service.component.annotations.Component) ANNOTATION_PORT_ID(org.onosproject.k8snetworking.api.Constants.ANNOTATION_PORT_ID) ANNOTATION_NETWORK_ID(org.onosproject.k8snetworking.api.Constants.ANNOTATION_NETWORK_ID) K8sNetworkEvent(org.onosproject.k8snetworking.api.K8sNetworkEvent) K8sPort(org.onosproject.k8snetworking.api.K8sPort) K8sNodeEvent(org.onosproject.k8snode.api.K8sNodeEvent) Activate(org.osgi.service.component.annotations.Activate) K8sNode(org.onosproject.k8snode.api.K8sNode) HostId(org.onosproject.net.HostId) ExecutorService(java.util.concurrent.ExecutorService) K8S_NETWORKING_APP_ID(org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID) AbstractProvider(org.onosproject.net.provider.AbstractProvider) DeviceListener(org.onosproject.net.device.DeviceListener) Logger(org.slf4j.Logger) HostProvider(org.onosproject.net.host.HostProvider) VXLAN(org.onosproject.k8snetworking.api.Constants.VXLAN) VlanId(org.onlab.packet.VlanId) K8sHostService(org.onosproject.k8snode.api.K8sHostService) ProviderId(org.onosproject.net.provider.ProviderId) ANNOTATION_SEGMENT_ID(org.onosproject.k8snetworking.api.Constants.ANNOTATION_SEGMENT_ID) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) DefaultAnnotations(org.onosproject.net.DefaultAnnotations) K8sNetworkingUtil.existingContainerPortByMac(org.onosproject.k8snetworking.util.K8sNetworkingUtil.existingContainerPortByMac) K8sNetwork(org.onosproject.k8snetworking.api.K8sNetwork) K8sNetworkingUtil.allK8sDevices(org.onosproject.k8snetworking.util.K8sNetworkingUtil.allK8sDevices) MacAddress(org.onlab.packet.MacAddress) K8sNodeService(org.onosproject.k8snode.api.K8sNodeService) Reference(org.osgi.service.component.annotations.Reference) DefaultHostDescription(org.onosproject.net.host.DefaultHostDescription) DefaultAnnotations(org.onosproject.net.DefaultAnnotations) Host(org.onosproject.net.Host) MacAddress(org.onlab.packet.MacAddress) HostId(org.onosproject.net.HostId) ConnectPoint(org.onosproject.net.ConnectPoint) HostDescription(org.onosproject.net.host.HostDescription) DefaultHostDescription(org.onosproject.net.host.DefaultHostDescription) K8sNetwork(org.onosproject.k8snetworking.api.K8sNetwork) HostLocation(org.onosproject.net.HostLocation) K8sPort(org.onosproject.k8snetworking.api.K8sPort)

Aggregations

MacAddress (org.onlab.packet.MacAddress)175 VlanId (org.onlab.packet.VlanId)67 IpAddress (org.onlab.packet.IpAddress)65 ConnectPoint (org.onosproject.net.ConnectPoint)55 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)54 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)53 DeviceId (org.onosproject.net.DeviceId)44 TrafficSelector (org.onosproject.net.flow.TrafficSelector)40 Host (org.onosproject.net.Host)38 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)36 Set (java.util.Set)33 HostLocation (org.onosproject.net.HostLocation)32 Logger (org.slf4j.Logger)32 Ethernet (org.onlab.packet.Ethernet)31 PortNumber (org.onosproject.net.PortNumber)29 List (java.util.List)27 HostId (org.onosproject.net.HostId)27 Interface (org.onosproject.net.intf.Interface)27 IpPrefix (org.onlab.packet.IpPrefix)26 LoggerFactory (org.slf4j.LoggerFactory)24