Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 1 | /* |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [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 | */ |
Jian Li | 8df54a9 | 2018-08-23 17:01:31 +0900 | [diff] [blame] | 16 | package org.onosproject.simplefabric.impl; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.osgi.service.component.annotations.Activate; |
| 19 | import org.osgi.service.component.annotations.Component; |
| 20 | import org.osgi.service.component.annotations.Deactivate; |
| 21 | import org.osgi.service.component.annotations.Reference; |
| 22 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.net.intf.Interface; |
| 27 | import org.onosproject.net.intf.InterfaceService; |
| 28 | import org.onosproject.net.neighbour.NeighbourMessageContext; |
| 29 | import org.onosproject.net.neighbour.NeighbourMessageHandler; |
| 30 | import org.onosproject.net.neighbour.NeighbourResolutionService; |
| 31 | import org.onosproject.net.Host; |
| 32 | import org.onosproject.net.host.HostService; |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 33 | import org.onosproject.simplefabric.api.FabricNetwork; |
Jian Li | c7efc1d | 2018-08-23 16:37:34 +0900 | [diff] [blame] | 34 | import org.onosproject.simplefabric.api.SimpleFabricEvent; |
| 35 | import org.onosproject.simplefabric.api.SimpleFabricListener; |
| 36 | import org.onosproject.simplefabric.api.SimpleFabricService; |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import java.util.HashSet; |
| 41 | import java.util.Objects; |
| 42 | import java.util.Set; |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * Handles neighbour messages for on behalf of the L2 Network application. Handlers |
| 47 | * will be changed automatically by interface or network configuration events. |
| 48 | */ |
| 49 | @Component(immediate = true, enabled = false) |
| 50 | public class SimpleFabricNeighbour { |
| 51 | |
| 52 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 53 | protected ApplicationId appId; |
| 54 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 56 | protected CoreService coreService; |
| 57 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 59 | protected HostService hostService; |
| 60 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 62 | protected InterfaceService interfaceService; |
| 63 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 65 | protected NeighbourResolutionService neighbourService; |
| 66 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 68 | protected SimpleFabricService simpleFabric; |
| 69 | |
| 70 | private final InternalSimpleFabricListener simpleFabricListener = |
| 71 | new InternalSimpleFabricListener(); |
| 72 | |
| 73 | private L2NetworkNeighbourMessageHandler neighbourHandler = |
| 74 | new L2NetworkNeighbourMessageHandler(); |
| 75 | |
| 76 | private Set<Interface> monitoredInterfaces = new HashSet<>(); |
| 77 | |
| 78 | @Activate |
| 79 | public void activate() { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 80 | appId = simpleFabric.appId(); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 81 | simpleFabric.addListener(simpleFabricListener); |
| 82 | refresh(); |
| 83 | log.info("simple fabric neighbour started"); |
| 84 | } |
| 85 | |
| 86 | @Deactivate |
| 87 | public void deactivate() { |
| 88 | simpleFabric.removeListener(simpleFabricListener); |
| 89 | unregister(); |
| 90 | monitoredInterfaces.clear(); |
| 91 | log.info("simple fabric neighbour stoped"); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Registers neighbour handler to all available interfaces. |
| 96 | */ |
| 97 | protected void refresh() { |
| 98 | Set<Interface> interfaces = interfaceService.getInterfaces(); |
| 99 | // check for new interfaces |
| 100 | for (Interface intf : interfaces) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 101 | if (!monitoredInterfaces.contains(intf) && simpleFabric.isFabricNetworkInterface(intf)) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 102 | log.info("simple fabric neighbour register handler: {}", intf); |
| 103 | neighbourService.registerNeighbourHandler(intf, neighbourHandler, appId); |
| 104 | monitoredInterfaces.add(intf); |
| 105 | } else { |
| 106 | log.debug("simple fabric neighobur unknown interface: {}", intf); |
| 107 | } |
| 108 | } |
| 109 | // check for removed interfaces |
| 110 | Set<Interface> monitoredInterfacesToBeRemoved = new HashSet<>(); |
| 111 | for (Interface intf : monitoredInterfaces) { |
| 112 | if (!interfaces.contains(intf)) { |
| 113 | log.info("simple fabric neighbour unregister handler: {}", intf); |
| 114 | neighbourService.unregisterNeighbourHandler(intf, neighbourHandler, appId); |
| 115 | monitoredInterfacesToBeRemoved.add(intf); |
| 116 | } |
| 117 | } |
| 118 | for (Interface intf : monitoredInterfacesToBeRemoved) { |
| 119 | monitoredInterfaces.remove(intf); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Unregisters neighbour handler to all available interfaces. |
| 125 | */ |
| 126 | protected void unregister() { |
| 127 | log.info("simple fabric neighbour unregister handler"); |
| 128 | neighbourService.unregisterNeighbourHandlers(appId); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Handles request messages. |
| 133 | * |
| 134 | * @param context the message context |
| 135 | */ |
| 136 | protected void handleRequest(NeighbourMessageContext context) { |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 137 | MacAddress mac = simpleFabric.vMacForIp(context.target()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 138 | if (mac != null) { |
| 139 | log.trace("simple fabric neightbour request on virtualGatewayAddress {}; response to {} {} mac={}", |
| 140 | context.target(), context.inPort(), context.vlan(), mac); |
| 141 | context.reply(mac); |
| 142 | return; |
| 143 | } |
| 144 | // else forward to corresponding host |
| 145 | |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 146 | FabricNetwork fabricNetwork = simpleFabric.fabricNetwork(context.inPort(), context.vlan()); |
| 147 | if (fabricNetwork != null) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 148 | int numForwards = 0; |
| 149 | if (!context.dstMac().isBroadcast() && !context.dstMac().isMulticast()) { |
| 150 | for (Host host : hostService.getHostsByMac(context.dstMac())) { |
| 151 | log.trace("simple fabric neightbour request forward unicast to {}", host.location()); |
| 152 | context.forward(host.location()); // ASSUME: vlan is same |
| 153 | // TODO: may need to check host.location().time() |
| 154 | numForwards++; |
| 155 | } |
| 156 | if (numForwards > 0) { |
| 157 | return; |
| 158 | } |
| 159 | } |
| 160 | // else do broadcast to all host in the same l2 network |
| 161 | log.trace("simple fabric neightbour request forward broadcast: {} {}", |
| 162 | context.inPort(), context.vlan()); |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 163 | for (Interface iface : fabricNetwork.interfaces()) { |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 164 | if (!context.inPort().equals(iface.connectPoint())) { |
| 165 | log.trace("simple fabric forward neighbour request broadcast to {}", iface); |
| 166 | context.forward(iface); |
| 167 | } |
| 168 | } |
| 169 | } else { |
| 170 | log.warn("simple fabric neightbour request drop: {} {}", |
| 171 | context.inPort(), context.vlan()); |
| 172 | context.drop(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Handles reply messages between VLAN tagged interfaces. |
| 178 | * |
| 179 | * @param context the message context |
| 180 | * @param hostService the host service |
| 181 | */ |
| 182 | protected void handleReply(NeighbourMessageContext context, |
| 183 | HostService hostService) { |
| 184 | // Find target L2 Network, then reply to the host |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 185 | FabricNetwork fabricNetwork = simpleFabric.fabricNetwork(context.inPort(), context.vlan()); |
| 186 | if (fabricNetwork != null) { |
| 187 | // TODO: need to check and update simpleFabric.DefaultFabricNetwork |
| 188 | MacAddress mac = simpleFabric.vMacForIp(context.target()); |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 189 | if (mac != null) { |
| 190 | log.trace("simple fabric neightbour response message to virtual gateway; drop: {} {} target={}", |
| 191 | context.inPort(), context.vlan(), context.target()); |
| 192 | context.drop(); |
| 193 | } else { |
| 194 | // forward reply to the hosts of the dstMac |
| 195 | Set<Host> hosts = hostService.getHostsByMac(context.dstMac()); |
| 196 | log.trace("simple fabric neightbour response message forward: {} {} target={} -> {}", |
| 197 | context.inPort(), context.vlan(), context.target(), hosts); |
| 198 | hosts.stream() |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 199 | .map(host -> simpleFabric.hostInterface(host)) |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 200 | .filter(Objects::nonNull) |
| 201 | .forEach(context::forward); |
| 202 | } |
| 203 | } else { |
| 204 | // this might be happened when we remove an interface from L2 Network |
| 205 | // just ignore this message |
Jian Li | e2d8751 | 2018-08-23 17:33:05 +0900 | [diff] [blame] | 206 | log.warn("simple fabric neightbour response message drop for unknown fabricNetwork: {} {}", |
Lee Yongjae | 7c27bb4 | 2017-11-17 12:00:45 +0900 | [diff] [blame] | 207 | context.inPort(), context.vlan()); |
| 208 | context.drop(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | private class L2NetworkNeighbourMessageHandler implements NeighbourMessageHandler { |
| 213 | @Override |
| 214 | public void handleMessage(NeighbourMessageContext context, |
| 215 | HostService hostService) { |
| 216 | switch (context.type()) { |
| 217 | case REQUEST: |
| 218 | handleRequest(context); |
| 219 | break; |
| 220 | case REPLY: |
| 221 | handleReply(context, hostService); |
| 222 | break; |
| 223 | default: |
| 224 | log.warn("simple fabric neightor unknown context type: {}", context.type()); |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | private class InternalSimpleFabricListener implements SimpleFabricListener { |
| 231 | @Override |
| 232 | public void event(SimpleFabricEvent event) { |
| 233 | switch (event.type()) { |
| 234 | case SIMPLE_FABRIC_UPDATED: |
| 235 | refresh(); |
| 236 | break; |
| 237 | default: |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | } |
| 244 | |