Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.fwd; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | bc4a793 | 2014-10-21 11:46:00 -0700 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.Dictionary; |
| 21 | import java.util.Set; |
| 22 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Modified; |
| 27 | import org.apache.felix.scr.annotations.Property; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 30 | import org.onlab.packet.Ethernet; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
Jonathan Hart | 06ae79d | 2015-01-13 18:56:33 -0800 | [diff] [blame] | 33 | import org.onosproject.net.Device; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.Host; |
| 35 | import org.onosproject.net.HostId; |
| 36 | import org.onosproject.net.Path; |
| 37 | import org.onosproject.net.PortNumber; |
Jonathan Hart | 06ae79d | 2015-01-13 18:56:33 -0800 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceEvent; |
| 39 | import org.onosproject.net.device.DeviceListener; |
| 40 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.flow.DefaultFlowRule; |
| 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.FlowRule; |
| 45 | import org.onosproject.net.flow.FlowRuleService; |
| 46 | import org.onosproject.net.flow.TrafficSelector; |
| 47 | import org.onosproject.net.flow.TrafficTreatment; |
| 48 | import org.onosproject.net.host.HostService; |
| 49 | import org.onosproject.net.packet.InboundPacket; |
| 50 | import org.onosproject.net.packet.PacketContext; |
| 51 | import org.onosproject.net.packet.PacketProcessor; |
| 52 | import org.onosproject.net.packet.PacketService; |
| 53 | import org.onosproject.net.topology.TopologyService; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 54 | import org.osgi.service.component.ComponentContext; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 55 | import org.slf4j.Logger; |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 56 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Sample reactive forwarding application. |
| 59 | */ |
| 60 | @Component(immediate = true) |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 61 | public class ReactiveForwarding { |
| 62 | |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 63 | private static final int TIMEOUT = 10; |
alshabib | a0e0498 | 2014-10-03 13:03:19 -0700 | [diff] [blame] | 64 | private static final int PRIORITY = 10; |
Jonathan Hart | 06ae79d | 2015-01-13 18:56:33 -0800 | [diff] [blame] | 65 | private static final int PUNT_RULE_PRIORITY = 5; |
alshabib | ba5ac48 | 2014-10-02 17:15:20 -0700 | [diff] [blame] | 66 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected TopologyService topologyService; |
| 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected PacketService packetService; |
| 74 | |
alshabib | 8aef1ad | 2014-09-15 17:47:31 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected HostService hostService; |
| 77 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected FlowRuleService flowRuleService; |
| 80 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 06ae79d | 2015-01-13 18:56:33 -0800 | [diff] [blame] | 82 | protected DeviceService deviceService; |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 85 | protected CoreService coreService; |
| 86 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 87 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 88 | |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 89 | private ApplicationId appId; |
| 90 | |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 91 | @Property(name = "packetOutOnly", boolValue = false, |
| 92 | label = "Enable packet-out only forwarding; default is false") |
| 93 | private boolean packetOutOnly = false; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 94 | |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 95 | @Property(name = "ipv6Forwarding", boolValue = false, |
| 96 | label = "Enable IPv6 forwarding; default is false") |
| 97 | private boolean ipv6Forwarding = false; |
| 98 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 99 | @Activate |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 100 | public void activate(ComponentContext context) { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 101 | appId = coreService.registerApplication("org.onosproject.fwd"); |
Jonathan Hart | 06ae79d | 2015-01-13 18:56:33 -0800 | [diff] [blame] | 102 | deviceService.addListener(new InternalDeviceListener()); |
| 103 | pushRules(); |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 104 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2); |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 105 | readComponentConfiguration(context); |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 106 | log.info("Started with Application ID {}", appId.id()); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Deactivate |
| 110 | public void deactivate() { |
alshabib | a68eb96 | 2014-09-24 20:34:13 -0700 | [diff] [blame] | 111 | flowRuleService.removeFlowRulesById(appId); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 112 | packetService.removeProcessor(processor); |
| 113 | processor = null; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 114 | log.info("Stopped"); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 115 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 116 | |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 117 | @Modified |
| 118 | public void modified(ComponentContext context) { |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 119 | readComponentConfiguration(context); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Extracts properties from the component configuration context. |
| 124 | * |
| 125 | * @param context the component context |
| 126 | */ |
| 127 | private void readComponentConfiguration(ComponentContext context) { |
| 128 | Dictionary<?, ?> properties = context.getProperties(); |
| 129 | boolean packetOutOnlyEnabled = isPropertyEnabled(properties, "packetOutOnly"); |
| 130 | if (packetOutOnly != packetOutOnlyEnabled) { |
| 131 | packetOutOnly = packetOutOnlyEnabled; |
| 132 | log.info("Configured. Packet-out only forwarding is {}", |
| 133 | packetOutOnly ? "enabled" : "disabled"); |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 134 | } |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 135 | boolean ipv6ForwardingEnabled = isPropertyEnabled(properties, "ipv6Forwarding"); |
| 136 | if (ipv6Forwarding != ipv6ForwardingEnabled) { |
| 137 | ipv6Forwarding = ipv6ForwardingEnabled; |
| 138 | log.info("Configured. IPv6 forwarding is {}", |
| 139 | ipv6Forwarding ? "enabled" : "disabled"); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Check property name is defined and set to true. |
| 145 | * |
| 146 | * @param properties properties to be looked up |
| 147 | * @param propertyName the name of the property to look up |
| 148 | * @return true when the propertyName is defined and set to true |
| 149 | */ |
| 150 | private static boolean isPropertyEnabled(Dictionary<?, ?> properties, String propertyName) { |
| 151 | boolean enabled = false; |
| 152 | try { |
| 153 | String flag = (String) properties.get(propertyName); |
| 154 | if (flag != null) { |
| 155 | enabled = flag.equals("true"); |
| 156 | } |
| 157 | } catch (ClassCastException e) { |
| 158 | // No propertyName defined. |
| 159 | enabled = false; |
| 160 | } |
| 161 | return enabled; |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 162 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * Packet processor responsible for forwarding packets along their paths. |
| 166 | */ |
| 167 | private class ReactivePacketProcessor implements PacketProcessor { |
| 168 | |
| 169 | @Override |
| 170 | public void process(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 171 | // Stop processing if the packet has been handled, since we |
| 172 | // can't do any more to it. |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 173 | if (context.isHandled()) { |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 174 | return; |
| 175 | } |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 176 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 177 | InboundPacket pkt = context.inPacket(); |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 178 | Ethernet ethPkt = pkt.parsed(); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 179 | |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 180 | if (ethPkt == null) { |
| 181 | return; |
| 182 | } |
| 183 | |
Kunihiro Ishiguro | d37c9ca | 2014-12-31 16:05:40 +0900 | [diff] [blame] | 184 | // Bail if this is deemed to be a control packet. |
| 185 | if (isControlPacket(ethPkt)) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | // Skip IPv6 multicast packet when IPv6 forward is disabled. |
| 190 | if (!ipv6Forwarding && isIpv6Multicast(ethPkt)) { |
Thomas Vachuska | 01a6ec0 | 2014-11-05 09:54:09 -0800 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 194 | HostId id = HostId.hostId(ethPkt.getDestinationMAC()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 195 | |
Thomas Vachuska | e1bcb0b | 2014-10-27 17:45:10 -0700 | [diff] [blame] | 196 | // Do not process link-local addresses in any way. |
| 197 | if (id.mac().isLinkLocal()) { |
| 198 | return; |
| 199 | } |
| 200 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 201 | // Do we know who this is for? If not, flood and bail. |
| 202 | Host dst = hostService.getHost(id); |
| 203 | if (dst == null) { |
| 204 | flood(context); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | // Are we on an edge switch that our destination is on? If so, |
| 209 | // simply forward out to the destination and bail. |
| 210 | if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) { |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 211 | if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) { |
| 212 | installRule(context, dst.location().port()); |
| 213 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 214 | return; |
| 215 | } |
| 216 | |
| 217 | // Otherwise, get a set of paths that lead from here to the |
| 218 | // destination edge switch. |
| 219 | Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(), |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 220 | pkt.receivedFrom().deviceId(), |
| 221 | dst.location().deviceId()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 222 | if (paths.isEmpty()) { |
| 223 | // If there are no paths, flood and bail. |
| 224 | flood(context); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | // Otherwise, pick a path that does not lead back to where we |
| 229 | // came from; if no such path, flood and bail. |
| 230 | Path path = pickForwardPath(paths, pkt.receivedFrom().port()); |
| 231 | if (path == null) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 232 | log.warn("Doh... don't know where to go... {} -> {} received on {}", |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 233 | ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(), |
| 234 | pkt.receivedFrom()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 235 | flood(context); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | // Otherwise forward and be done with it. |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 240 | installRule(context, path.src().port()); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 241 | } |
Thomas Vachuska | 01a6ec0 | 2014-11-05 09:54:09 -0800 | [diff] [blame] | 242 | |
| 243 | } |
| 244 | |
| 245 | // Indicates whether this is a control packet, e.g. LLDP, BDDP |
| 246 | private boolean isControlPacket(Ethernet eth) { |
| 247 | short type = eth.getEtherType(); |
| 248 | return type == Ethernet.TYPE_LLDP || type == Ethernet.TYPE_BSN; |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Thomas Vachuska | 5dd52f7 | 2014-11-28 19:27:45 -0800 | [diff] [blame] | 251 | // Indicated whether this is an IPv6 multicast packet. |
| 252 | private boolean isIpv6Multicast(Ethernet eth) { |
| 253 | return eth.getEtherType() == Ethernet.TYPE_IPV6 && eth.isMulticast(); |
| 254 | } |
| 255 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 256 | // Selects a path from the given set that does not lead back to the |
| 257 | // specified port. |
| 258 | private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) { |
| 259 | for (Path path : paths) { |
| 260 | if (!path.src().port().equals(notToPort)) { |
| 261 | return path; |
| 262 | } |
| 263 | } |
| 264 | return null; |
| 265 | } |
| 266 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 267 | // Floods the specified packet if permissible. |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 268 | private void flood(PacketContext context) { |
tom | dc95b8a | 2014-09-17 08:07:26 -0700 | [diff] [blame] | 269 | if (topologyService.isBroadcastPoint(topologyService.currentTopology(), |
tom | c16656f | 2014-10-15 18:30:31 -0700 | [diff] [blame] | 270 | context.inPacket().receivedFrom())) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 271 | packetOut(context, PortNumber.FLOOD); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 272 | } else { |
| 273 | context.block(); |
| 274 | } |
| 275 | } |
| 276 | |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 277 | // Sends a packet out the specified port. |
| 278 | private void packetOut(PacketContext context, PortNumber portNumber) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 279 | context.treatmentBuilder().setOutput(portNumber); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 280 | context.send(); |
| 281 | } |
| 282 | |
| 283 | // Install a rule forwarding the packet to the specified port. |
| 284 | private void installRule(PacketContext context, PortNumber portNumber) { |
tom | 642b226 | 2014-09-17 13:52:55 -0700 | [diff] [blame] | 285 | // We don't yet support bufferids in the flowservice so packet out first. |
| 286 | packetOut(context, portNumber); |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 287 | if (!packetOutOnly) { |
| 288 | // Install the flow rule to handle this type of message from now on. |
| 289 | Ethernet inPkt = context.inPacket().parsed(); |
| 290 | TrafficSelector.Builder builder = DefaultTrafficSelector.builder(); |
| 291 | builder.matchEthType(inPkt.getEtherType()) |
| 292 | .matchEthSrc(inPkt.getSourceMAC()) |
| 293 | .matchEthDst(inPkt.getDestinationMAC()) |
| 294 | .matchInport(context.inPacket().receivedFrom().port()); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 295 | |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 296 | TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(); |
| 297 | treat.setOutput(portNumber); |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 298 | |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 299 | FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(), |
| 300 | builder.build(), treat.build(), PRIORITY, appId, TIMEOUT, false); |
alshabib | 6eb438a | 2014-10-01 16:39:37 -0700 | [diff] [blame] | 301 | |
Thomas Vachuska | bd7f4b3 | 2014-12-04 20:54:55 -0800 | [diff] [blame] | 302 | flowRuleService.applyFlowRules(f); |
| 303 | } |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Jonathan Hart | 06ae79d | 2015-01-13 18:56:33 -0800 | [diff] [blame] | 306 | /** |
| 307 | * Pushes flow rules to all devices. |
| 308 | */ |
| 309 | private void pushRules() { |
| 310 | for (Device device : deviceService.getDevices()) { |
| 311 | pushRules(device); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Pushes flow rules to the device to receive packets that need |
| 317 | * to be processed. |
| 318 | * |
| 319 | * @param device the device to push the rules to |
| 320 | */ |
| 321 | private synchronized void pushRules(Device device) { |
| 322 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 323 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 324 | |
| 325 | // Get all IPv4 packets |
| 326 | sbuilder.matchEthType(Ethernet.TYPE_IPV4); |
| 327 | tbuilder.punt(); |
| 328 | FlowRule flowArp = |
| 329 | new DefaultFlowRule(device.id(), |
| 330 | sbuilder.build(), tbuilder.build(), |
| 331 | PUNT_RULE_PRIORITY, appId, 0, true); |
| 332 | |
| 333 | flowRuleService.applyFlowRules(flowArp); |
| 334 | } |
| 335 | |
| 336 | public class InternalDeviceListener implements DeviceListener { |
| 337 | |
| 338 | @Override |
| 339 | public void event(DeviceEvent event) { |
| 340 | Device device = event.subject(); |
| 341 | switch (event.type()) { |
| 342 | case DEVICE_ADDED: |
| 343 | pushRules(device); |
| 344 | break; |
| 345 | case DEVICE_AVAILABILITY_CHANGED: |
| 346 | case DEVICE_SUSPENDED: |
| 347 | case DEVICE_UPDATED: |
| 348 | case DEVICE_REMOVED: |
| 349 | case PORT_ADDED: |
| 350 | case PORT_UPDATED: |
| 351 | case PORT_REMOVED: |
| 352 | default: |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 358 | } |
| 359 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 360 | |