Search in sources :

Example 1 with ARP

use of org.onlab.packet.ARP 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 2 with ARP

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

the class K8sSwitchingArpHandler method processPacketIn.

/**
 * Processes ARP request packets.
 *
 * @param context   packet context
 * @param ethPacket ethernet packet
 */
private void processPacketIn(PacketContext context, Ethernet ethPacket) {
    // if the ARP mode is configured as broadcast mode, we simply ignore ARP packet_in
    if (ARP_BROADCAST_MODE.equals(getArpMode())) {
        return;
    }
    DeviceId deviceId = context.inPacket().receivedFrom().deviceId();
    if (!allK8sDevices(k8sNodeService, k8sHostService).contains(deviceId)) {
        return;
    }
    ARP arpPacket = (ARP) ethPacket.getPayload();
    if (arpPacket.getOpCode() == ARP.OP_REQUEST) {
        processArpRequest(context, ethPacket);
    } else if (arpPacket.getOpCode() == ARP.OP_REPLY) {
        processArpReply(context, ethPacket);
    }
}
Also used : DeviceId(org.onosproject.net.DeviceId) ARP(org.onlab.packet.ARP)

Example 3 with ARP

use of org.onlab.packet.ARP 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 4 with ARP

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

the class KubevirtNetworkingUtil method buildGarpPacket.

/**
 * Builds a GARP packet using the given source MAC and source IP address.
 *
 * @param srcMac source MAC address
 * @param srcIp  source IP address
 * @return GARP packet
 */
public static Ethernet buildGarpPacket(MacAddress srcMac, IpAddress srcIp) {
    if (srcMac == null || srcIp == null) {
        return null;
    }
    Ethernet ethernet = new Ethernet();
    ethernet.setDestinationMACAddress(MacAddress.BROADCAST);
    ethernet.setSourceMACAddress(srcMac);
    ethernet.setEtherType(Ethernet.TYPE_ARP);
    ARP arp = new ARP();
    arp.setOpCode(ARP.OP_REPLY);
    arp.setProtocolType(ARP.PROTO_TYPE_IP);
    arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
    arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH);
    arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
    arp.setSenderHardwareAddress(srcMac.toBytes());
    arp.setTargetHardwareAddress(MacAddress.BROADCAST.toBytes());
    arp.setSenderProtocolAddress(srcIp.toOctets());
    arp.setTargetProtocolAddress(srcIp.toOctets());
    ethernet.setPayload(arp);
    return ethernet;
}
Also used : Ethernet(org.onlab.packet.Ethernet) ARP(org.onlab.packet.ARP)

Example 5 with ARP

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

the class CastorArpManager method createArpContext.

/**
 * Extracts context information from ARP packets.
 *
 * @param eth input Ethernet frame that is thought to be ARP
 * @param inPort in port
 * @return MessageContext object if the packet was a valid ARP packet,
 * otherwise null
 */
private MessageContext createArpContext(Ethernet eth, ConnectPoint inPort) {
    if (eth.getEtherType() != Ethernet.TYPE_ARP) {
        return null;
    }
    ARP arp = (ARP) eth.getPayload();
    IpAddress target = Ip4Address.valueOf(arp.getTargetProtocolAddress());
    IpAddress sender = Ip4Address.valueOf(arp.getSenderProtocolAddress());
    MessageType type;
    if (arp.getOpCode() == ARP.OP_REQUEST) {
        type = MessageType.REQUEST;
    } else if (arp.getOpCode() == ARP.OP_REPLY) {
        type = MessageType.REPLY;
    } else {
        return null;
    }
    return new MessageContext(eth, inPort, Protocol.ARP, type, target, sender);
}
Also used : IpAddress(org.onlab.packet.IpAddress) ARP(org.onlab.packet.ARP) TYPE_ARP(org.onlab.packet.Ethernet.TYPE_ARP)

Aggregations

ARP (org.onlab.packet.ARP)20 Ethernet (org.onlab.packet.Ethernet)15 IpAddress (org.onlab.packet.IpAddress)9 MacAddress (org.onlab.packet.MacAddress)7 DefaultOutboundPacket (org.onosproject.net.packet.DefaultOutboundPacket)7 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)6 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)6 ByteBuffer (java.nio.ByteBuffer)5 Set (java.util.Set)5 Ip4Address (org.onlab.packet.Ip4Address)5 ConnectPoint (org.onosproject.net.ConnectPoint)5 DeviceId (org.onosproject.net.DeviceId)5 PacketService (org.onosproject.net.packet.PacketService)5 Logger (org.slf4j.Logger)5 Objects (java.util.Objects)4 ExecutorService (java.util.concurrent.ExecutorService)4 Executors.newSingleThreadExecutor (java.util.concurrent.Executors.newSingleThreadExecutor)4 Collectors (java.util.stream.Collectors)4 Test (org.junit.Test)4 VlanId (org.onlab.packet.VlanId)4