Jonathan Hart | 06e8908 | 2016-08-08 17:21:01 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jonathan Hart | 06e8908 | 2016-08-08 17:21:01 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ray Milkey | b65d784 | 2017-08-03 16:28:24 -0700 | [diff] [blame] | 17 | package org.onosproject.net.neighbour; |
Jonathan Hart | 06e8908 | 2016-08-08 17:21:01 -0700 | [diff] [blame] | 18 | |
| 19 | import org.onosproject.net.Host; |
| 20 | import org.onosproject.net.host.HostService; |
| 21 | |
| 22 | import java.util.Set; |
| 23 | |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 24 | import static org.onlab.packet.VlanId.vlanId; |
Jonathan Hart | 06e8908 | 2016-08-08 17:21:01 -0700 | [diff] [blame] | 25 | import static org.onosproject.net.HostId.hostId; |
| 26 | |
| 27 | /** |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 28 | * Default neighbour message handler which implements basic proxying on an |
Jonathan Hart | 06e8908 | 2016-08-08 17:21:01 -0700 | [diff] [blame] | 29 | * L2 network (i.e. ProxyArp behaviour). |
| 30 | */ |
| 31 | public class DefaultNeighbourMessageHandler implements NeighbourMessageHandler { |
| 32 | @Override |
| 33 | public void handleMessage(NeighbourMessageContext context, HostService hostService) { |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 34 | switch (context.type()) { |
| 35 | case REPLY: |
| 36 | Host h = hostService.getHost(hostId(context.packet().getDestinationMAC(), |
| 37 | vlanId(context.packet().getVlanID()))); |
Jonathan Hart | 06e8908 | 2016-08-08 17:21:01 -0700 | [diff] [blame] | 38 | |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 39 | if (h == null) { |
| 40 | context.flood(); |
| 41 | } else { |
Jonathan Hart | 2efe0c2 | 2016-09-21 12:04:45 -0700 | [diff] [blame] | 42 | context.forward(h.location()); |
Jonathan Hart | 06e8908 | 2016-08-08 17:21:01 -0700 | [diff] [blame] | 43 | } |
Jonathan Hart | 9bdaaec | 2016-08-22 13:33:45 -0700 | [diff] [blame] | 44 | break; |
| 45 | case REQUEST: |
| 46 | // See if we have the target host in the host store |
| 47 | Set<Host> hosts = hostService.getHostsByIp(context.target()); |
| 48 | |
| 49 | Host dst = null; |
| 50 | Host src = hostService.getHost(hostId(context.srcMac(), context.vlan())); |
| 51 | |
| 52 | for (Host host : hosts) { |
| 53 | if (host.vlan().equals(context.vlan())) { |
| 54 | dst = host; |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (src != null && dst != null) { |
| 60 | // We know the target host so we can respond |
| 61 | context.reply(dst.mac()); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | // The request couldn't be resolved. |
| 66 | // Flood the request on all ports except the incoming port. |
| 67 | context.flood(); |
| 68 | break; |
| 69 | default: |
| 70 | break; |
Jonathan Hart | 06e8908 | 2016-08-08 17:21:01 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jonathan Hart | 06e8908 | 2016-08-08 17:21:01 -0700 | [diff] [blame] | 73 | } |
| 74 | } |