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