Jonathan Hart
Committed by Gerrit Code Review

Implementation of the NeighbourResolutionService.

Change-Id: I41fc48578df3027ec71ee7369171c8988ee7a85e
......@@ -21,36 +21,54 @@ import org.onosproject.net.host.HostService;
import java.util.Set;
import static org.onlab.packet.VlanId.vlanId;
import static org.onosproject.net.HostId.hostId;
/**
* Default neighbour message handler which implements basic proxying on a flat
* Default neighbour message handler which implements basic proxying on an
* L2 network (i.e. ProxyArp behaviour).
*/
public class DefaultNeighbourMessageHandler implements NeighbourMessageHandler {
@Override
public void handleMessage(NeighbourMessageContext context, HostService hostService) {
// See if we have the target host in the host store
Set<Host> hosts = hostService.getHostsByIp(context.target());
switch (context.type()) {
case REPLY:
Host h = hostService.getHost(hostId(context.packet().getDestinationMAC(),
vlanId(context.packet().getVlanID())));
Host dst = null;
Host src = hostService.getHost(hostId(context.srcMac(), context.vlan()));
if (h == null) {
context.flood();
} else {
context.proxy(h.location());
}
break;
case REQUEST:
// See if we have the target host in the host store
Set<Host> hosts = hostService.getHostsByIp(context.target());
Host dst = null;
Host src = hostService.getHost(hostId(context.srcMac(), context.vlan()));
for (Host host : hosts) {
if (host.vlan().equals(context.vlan())) {
dst = host;
break;
for (Host host : hosts) {
if (host.vlan().equals(context.vlan())) {
dst = host;
break;
}
}
if (src != null && dst != null) {
// We know the target host so we can respond
context.reply(dst.mac());
return;
}
}
if (src != null && dst != null) {
// We know the target host so we can respond
context.reply(dst.mac());
return;
// The request couldn't be resolved.
// Flood the request on all ports except the incoming port.
context.flood();
break;
default:
break;
}
// The request couldn't be resolved.
// Flood the request on all ports except the incoming port.
context.flood();
}
}
......
......@@ -24,8 +24,6 @@ import org.onlab.packet.VlanId;
import org.onosproject.incubator.net.intf.Interface;
import org.onosproject.net.ConnectPoint;
import static com.google.common.base.Preconditions.checkState;
/**
* Context of an incoming neighbor message (e.g. ARP, NDP).
*
......@@ -34,156 +32,91 @@ import static com.google.common.base.Preconditions.checkState;
* response to the incoming message.</p>
*/
@Beta
public class NeighbourMessageContext {
private final NeighbourProtocol protocol;
private final NeighbourMessageType type;
private final IpAddress target;
private final IpAddress sender;
private final Ethernet eth;
private final ConnectPoint inPort;
private final NeighbourMessageActions actions;
/**
* Creates a new neighbour message context.
*
* @param actions actions
* @param eth ethernet frame
* @param inPort incoming port
* @param protocol message protocol
* @param type message type
* @param target target IP address
* @param sender sender IP address
*/
public NeighbourMessageContext(NeighbourMessageActions actions,
Ethernet eth, ConnectPoint inPort,
NeighbourProtocol protocol, NeighbourMessageType type,
IpAddress target, IpAddress sender) {
this.actions = actions;
this.eth = eth;
this.inPort = inPort;
this.protocol = protocol;
this.type = type;
this.target = target;
this.sender = sender;
}
public interface NeighbourMessageContext {
/**
* Gets the port where the packet came in to the network.
*
* @return connect point
*/
public ConnectPoint inPort() {
return inPort;
}
ConnectPoint inPort();
/**
* Gets the full parsed representation of the packet.
*
* @return ethernet header
*/
public Ethernet packet() {
return eth;
}
Ethernet packet();
/**
* Gets the protocol of the packet.
*
* @return protocol
*/
public NeighbourProtocol protocol() {
return protocol;
}
NeighbourProtocol protocol();
/**
* Gets the message type of the packet.
*
* @return message type
*/
public NeighbourMessageType type() {
return type;
}
NeighbourMessageType type();
/**
* Gets the vlan of the packet, if any.
*
* @return vlan
*/
public VlanId vlan() {
return VlanId.vlanId(eth.getVlanID());
}
VlanId vlan();
/**
* Gets the source MAC address of the message.
*
* @return source MAC address
*/
public MacAddress srcMac() {
return MacAddress.valueOf(eth.getSourceMACAddress());
}
MacAddress srcMac();
/**
* Gets the target IP address of the message.
*
* @return target IP address
*/
public IpAddress target() {
return target;
}
IpAddress target();
/**
* Gets the source IP address of the message.
*
* @return source IP address
*/
public IpAddress sender() {
return sender;
}
IpAddress sender();
/**
* Proxies the message to a given output port.
*
* @param outPort output port
*/
public void proxy(ConnectPoint outPort) {
actions.proxy(this, outPort);
}
void proxy(ConnectPoint outPort);
/**
* Proxies the message to a given interface.
*
* @param outIntf output interface
*/
public void proxy(Interface outIntf) {
actions.proxy(this, outIntf);
}
void proxy(Interface outIntf);
/**
* Replies to the request message with a given MAC address.
*
* @param targetMac target MAC address
*/
public void reply(MacAddress targetMac) {
checkState(type == NeighbourMessageType.REQUEST, "can only reply to requests");
actions.reply(this, targetMac);
}
void reply(MacAddress targetMac);
/**
* Floods the incoming message out all ports except the input port.
*/
public void flood() {
actions.flood(this);
}
void flood();
/**
* Drops the incoming message.
*/
public void drop() {
actions.drop(this);
}
void drop();
}
......
......@@ -45,4 +45,20 @@ public interface NeighbourResolutionService {
*/
void registerNeighbourHandler(Interface intf, NeighbourMessageHandler handler);
/**
* Unregisters a neighbour message handler that was assigned to a connect
* point.
*
* @param connectPoint connect point
* @param handler neighbour message handler
*/
void unregisterNeighbourHandler(ConnectPoint connectPoint, NeighbourMessageHandler handler);
/**
* Unregisters a neighbour message handler that was assigned to an interface.
*
* @param intf interface
* @param handler neighbour message handler
*/
void unregisterNeighbourHandler(Interface intf, NeighbourMessageHandler handler);
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.net.neighbour.impl;
import com.google.common.annotations.Beta;
import org.onlab.packet.ARP;
import org.onlab.packet.Ethernet;
import org.onlab.packet.ICMP6;
import org.onlab.packet.IPv6;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.Ip6Address;
import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onlab.packet.VlanId;
import org.onlab.packet.ndp.NeighborSolicitation;
import org.onosproject.incubator.net.intf.Interface;
import org.onosproject.incubator.net.neighbour.NeighbourMessageActions;
import org.onosproject.incubator.net.neighbour.NeighbourMessageContext;
import org.onosproject.incubator.net.neighbour.NeighbourMessageType;
import org.onosproject.incubator.net.neighbour.NeighbourProtocol;
import org.onosproject.net.ConnectPoint;
import static com.google.common.base.Preconditions.checkState;
/**
* Default implementation of a neighbour message context.
*/
@Beta
public class DefaultNeighbourMessageContext implements NeighbourMessageContext {
private final NeighbourProtocol protocol;
private final NeighbourMessageType type;
private final IpAddress target;
private final IpAddress sender;
private final Ethernet eth;
private final ConnectPoint inPort;
private final NeighbourMessageActions actions;
/**
* Creates a new neighbour message context.
*
* @param actions actions
* @param eth ethernet frame
* @param inPort incoming port
* @param protocol message protocol
* @param type message type
* @param target target IP address
* @param sender sender IP address
*/
DefaultNeighbourMessageContext(NeighbourMessageActions actions,
Ethernet eth, ConnectPoint inPort,
NeighbourProtocol protocol,
NeighbourMessageType type,
IpAddress target, IpAddress sender) {
this.actions = actions;
this.eth = eth;
this.inPort = inPort;
this.protocol = protocol;
this.type = type;
this.target = target;
this.sender = sender;
}
@Override
public ConnectPoint inPort() {
return inPort;
}
@Override
public Ethernet packet() {
return eth;
}
@Override
public NeighbourProtocol protocol() {
return protocol;
}
@Override
public NeighbourMessageType type() {
return type;
}
@Override
public VlanId vlan() {
return VlanId.vlanId(eth.getVlanID());
}
@Override
public MacAddress srcMac() {
return MacAddress.valueOf(eth.getSourceMACAddress());
}
@Override
public IpAddress target() {
return target;
}
@Override
public IpAddress sender() {
return sender;
}
@Override
public void proxy(ConnectPoint outPort) {
actions.proxy(this, outPort);
}
@Override
public void proxy(Interface outIntf) {
actions.proxy(this, outIntf);
}
@Override
public void reply(MacAddress targetMac) {
checkState(type == NeighbourMessageType.REQUEST, "can only reply to requests");
actions.reply(this, targetMac);
}
@Override
public void flood() {
actions.flood(this);
}
@Override
public void drop() {
actions.drop(this);
}
/**
* Attempts to create a MessageContext for the given Ethernet frame. If the
* frame is a valid ARP or NDP request or response, a context will be
* created.
*
* @param eth input Ethernet frame
* @param inPort in port
* @return MessageContext if the packet was ARP or NDP, otherwise null
*/
public static NeighbourMessageContext createContext(Ethernet eth,
ConnectPoint inPort,
NeighbourMessageActions actions) {
if (eth.getEtherType() == Ethernet.TYPE_ARP) {
return createArpContext(eth, inPort, actions);
} else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
return createNdpContext(eth, inPort, actions);
}
return null;
}
/**
* 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 static NeighbourMessageContext createArpContext(Ethernet eth,
ConnectPoint inPort,
NeighbourMessageActions actions) {
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());
NeighbourMessageType type;
if (arp.getOpCode() == ARP.OP_REQUEST) {
type = NeighbourMessageType.REQUEST;
} else if (arp.getOpCode() == ARP.OP_REPLY) {
type = NeighbourMessageType.REPLY;
} else {
return null;
}
return new DefaultNeighbourMessageContext(actions, eth, inPort,
NeighbourProtocol.ARP, type, target, sender);
}
/**
* Extracts context information from NDP packets.
*
* @param eth input Ethernet frame that is thought to be NDP
* @param inPort in port
* @return MessageContext object if the packet was a valid NDP packet,
* otherwise null
*/
private static NeighbourMessageContext createNdpContext(Ethernet eth,
ConnectPoint inPort,
NeighbourMessageActions actions) {
if (eth.getEtherType() != Ethernet.TYPE_IPV6) {
return null;
}
IPv6 ipv6 = (IPv6) eth.getPayload();
if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
return null;
}
ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
IpAddress sender = Ip6Address.valueOf(ipv6.getSourceAddress());
IpAddress target = null;
NeighbourMessageType type;
if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
type = NeighbourMessageType.REQUEST;
NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload();
target = Ip6Address.valueOf(nsol.getTargetAddress());
} else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
type = NeighbourMessageType.REPLY;
} else {
return null;
}
return new DefaultNeighbourMessageContext(actions, eth, inPort,
NeighbourProtocol.NDP, type, target, sender);
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Implementation of neighbour resolution service.
*/
package org.onosproject.incubator.net.neighbour.impl;