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.ifwd; |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 25 | import org.onlab.packet.Ethernet; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.net.Host; |
| 29 | import org.onosproject.net.HostId; |
| 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Jonathan Hart | 06ae79d | 2015-01-13 18:56:33 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.FlowRuleService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.TrafficSelector; |
| 35 | import org.onosproject.net.flow.TrafficTreatment; |
| 36 | import org.onosproject.net.host.HostService; |
| 37 | import org.onosproject.net.intent.HostToHostIntent; |
| 38 | import org.onosproject.net.intent.IntentService; |
| 39 | import org.onosproject.net.packet.DefaultOutboundPacket; |
| 40 | import org.onosproject.net.packet.InboundPacket; |
| 41 | import org.onosproject.net.packet.OutboundPacket; |
| 42 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 43 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.packet.PacketProcessor; |
| 45 | import org.onosproject.net.packet.PacketService; |
| 46 | import org.onosproject.net.topology.TopologyService; |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 49 | /** |
| 50 | * WORK-IN-PROGRESS: Sample reactive forwarding application using intent framework. |
| 51 | */ |
| 52 | @Component(immediate = true) |
| 53 | public class IntentReactiveForwarding { |
| 54 | |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 55 | private final Logger log = getLogger(getClass()); |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 58 | protected CoreService coreService; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 61 | protected TopologyService topologyService; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected PacketService packetService; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 67 | protected IntentService intentService; |
| 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected HostService hostService; |
| 71 | |
Jonathan Hart | 06ae79d | 2015-01-13 18:56:33 -0800 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected FlowRuleService flowRuleService; |
| 74 | |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 75 | private ReactivePacketProcessor processor = new ReactivePacketProcessor(); |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 76 | private ApplicationId appId; |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 77 | |
| 78 | @Activate |
| 79 | public void activate() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 80 | appId = coreService.registerApplication("org.onosproject.ifwd"); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 81 | |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 82 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 83 | |
| 84 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 85 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 86 | packetService.requestPackets(selector.build(), PacketPriority.REACTIVE, appId); |
| 87 | |
tom | 0511a52 | 2014-10-04 12:06:02 -0700 | [diff] [blame] | 88 | log.info("Started"); |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Deactivate |
| 92 | public void deactivate() { |
| 93 | packetService.removeProcessor(processor); |
| 94 | processor = null; |
| 95 | log.info("Stopped"); |
| 96 | } |
| 97 | |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Packet processor responsible for forwarding packets along their paths. |
| 100 | */ |
| 101 | private class ReactivePacketProcessor implements PacketProcessor { |
| 102 | |
| 103 | @Override |
| 104 | public void process(PacketContext context) { |
| 105 | // Stop processing if the packet has been handled, since we |
| 106 | // can't do any more to it. |
| 107 | if (context.isHandled()) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | InboundPacket pkt = context.inPacket(); |
| 112 | Ethernet ethPkt = pkt.parsed(); |
| 113 | |
Jonathan Hart | e8600eb | 2015-01-12 10:30:45 -0800 | [diff] [blame] | 114 | if (ethPkt == null) { |
| 115 | return; |
| 116 | } |
| 117 | |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 118 | HostId srcId = HostId.hostId(ethPkt.getSourceMAC()); |
| 119 | HostId dstId = HostId.hostId(ethPkt.getDestinationMAC()); |
| 120 | |
| 121 | // Do we know who this is for? If not, flood and bail. |
| 122 | Host dst = hostService.getHost(dstId); |
| 123 | if (dst == null) { |
| 124 | flood(context); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | // Otherwise forward and be done with it. |
| 129 | setUpConnectivity(context, srcId, dstId); |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 130 | forwardPacketToDst(context, dst); |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | // Floods the specified packet if permissible. |
| 135 | private void flood(PacketContext context) { |
| 136 | if (topologyService.isBroadcastPoint(topologyService.currentTopology(), |
| 137 | context.inPacket().receivedFrom())) { |
| 138 | packetOut(context, PortNumber.FLOOD); |
| 139 | } else { |
| 140 | context.block(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Sends a packet out the specified port. |
| 145 | private void packetOut(PacketContext context, PortNumber portNumber) { |
| 146 | context.treatmentBuilder().setOutput(portNumber); |
| 147 | context.send(); |
| 148 | } |
| 149 | |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 150 | private void forwardPacketToDst(PacketContext context, Host dst) { |
| 151 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(dst.location().port()).build(); |
| 152 | OutboundPacket packet = new DefaultOutboundPacket(dst.location().deviceId(), |
tom | 0511a52 | 2014-10-04 12:06:02 -0700 | [diff] [blame] | 153 | treatment, context.inPacket().unparsed()); |
Brian O'Connor | 958d381 | 2014-10-03 19:46:23 -0700 | [diff] [blame] | 154 | packetService.emit(packet); |
| 155 | log.info("sending packet: {}", packet); |
| 156 | } |
| 157 | |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 158 | // Install a rule forwarding the packet to the specified port. |
| 159 | private void setUpConnectivity(PacketContext context, HostId srcId, HostId dstId) { |
| 160 | TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 161 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 162 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 163 | HostToHostIntent intent = new HostToHostIntent(appId, srcId, dstId, |
| 164 | selector, treatment); |
tom | a9a77c2 | 2014-10-03 17:05:20 -0700 | [diff] [blame] | 165 | |
| 166 | intentService.submit(intent); |
| 167 | } |
| 168 | |
| 169 | } |