blob: 1accddb8dad98731cf06237c14fe7494270e246d [file] [log] [blame]
alshabib030111e2014-09-15 15:56:42 -07001package org.onlab.onos.fwd;
2
alshabibe9d3a322014-09-23 15:18:33 -07003import static org.slf4j.LoggerFactory.getLogger;
4
5import java.util.Set;
6
alshabib030111e2014-09-15 15:56:42 -07007import org.apache.felix.scr.annotations.Activate;
8import org.apache.felix.scr.annotations.Component;
9import org.apache.felix.scr.annotations.Deactivate;
10import org.apache.felix.scr.annotations.Reference;
11import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabiba68eb962014-09-24 20:34:13 -070012import org.onlab.onos.ApplicationId;
tomc370ebd2014-09-16 01:25:21 -070013import org.onlab.onos.net.Host;
14import org.onlab.onos.net.HostId;
15import org.onlab.onos.net.Path;
16import org.onlab.onos.net.PortNumber;
alshabib7b2748f2014-09-16 20:21:11 -070017import org.onlab.onos.net.flow.DefaultFlowRule;
18import org.onlab.onos.net.flow.DefaultTrafficSelector;
19import org.onlab.onos.net.flow.DefaultTrafficTreatment;
20import org.onlab.onos.net.flow.FlowRule;
21import org.onlab.onos.net.flow.FlowRuleService;
22import org.onlab.onos.net.flow.TrafficSelector;
23import org.onlab.onos.net.flow.TrafficTreatment;
alshabib8aef1ad2014-09-15 17:47:31 -070024import org.onlab.onos.net.host.HostService;
tomc370ebd2014-09-16 01:25:21 -070025import org.onlab.onos.net.packet.InboundPacket;
26import org.onlab.onos.net.packet.PacketContext;
alshabib030111e2014-09-15 15:56:42 -070027import org.onlab.onos.net.packet.PacketProcessor;
28import org.onlab.onos.net.packet.PacketService;
29import org.onlab.onos.net.topology.TopologyService;
alshabib7b2748f2014-09-16 20:21:11 -070030import org.onlab.packet.Ethernet;
tomc370ebd2014-09-16 01:25:21 -070031import org.slf4j.Logger;
alshabib030111e2014-09-15 15:56:42 -070032
tomc370ebd2014-09-16 01:25:21 -070033/**
34 * Sample reactive forwarding application.
35 */
36@Component(immediate = true)
alshabib030111e2014-09-15 15:56:42 -070037public class ReactiveForwarding {
38
alshabibba5ac482014-10-02 17:15:20 -070039 private static final int TIMEOUT = 10;
alshabiba0e04982014-10-03 13:03:19 -070040 private static final int PRIORITY = 10;
alshabibba5ac482014-10-02 17:15:20 -070041
tomc370ebd2014-09-16 01:25:21 -070042 private final Logger log = getLogger(getClass());
43
alshabib030111e2014-09-15 15:56:42 -070044 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
45 protected TopologyService topologyService;
46
47 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 protected PacketService packetService;
49
alshabib8aef1ad2014-09-15 17:47:31 -070050 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected HostService hostService;
52
alshabib7b2748f2014-09-16 20:21:11 -070053 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected FlowRuleService flowRuleService;
55
tomc370ebd2014-09-16 01:25:21 -070056 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
alshabib030111e2014-09-15 15:56:42 -070057
alshabiba68eb962014-09-24 20:34:13 -070058 private ApplicationId appId;
59
alshabib030111e2014-09-15 15:56:42 -070060 @Activate
61 public void activate() {
alshabiba68eb962014-09-24 20:34:13 -070062 appId = ApplicationId.getAppId();
alshabibc274c902014-10-03 14:58:27 -070063 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2);
alshabiba68eb962014-09-24 20:34:13 -070064 log.info("Started with Application ID {}", appId.id());
alshabib030111e2014-09-15 15:56:42 -070065 }
66
67 @Deactivate
68 public void deactivate() {
alshabiba68eb962014-09-24 20:34:13 -070069 flowRuleService.removeFlowRulesById(appId);
alshabib030111e2014-09-15 15:56:42 -070070 packetService.removeProcessor(processor);
71 processor = null;
tomc370ebd2014-09-16 01:25:21 -070072 log.info("Stopped");
alshabib030111e2014-09-15 15:56:42 -070073 }
tomc370ebd2014-09-16 01:25:21 -070074
75
76 /**
77 * Packet processor responsible for forwarding packets along their paths.
78 */
79 private class ReactivePacketProcessor implements PacketProcessor {
80
81 @Override
82 public void process(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -070083 // Stop processing if the packet has been handled, since we
84 // can't do any more to it.
alshabib7b2748f2014-09-16 20:21:11 -070085 if (context.isHandled()) {
86 return;
87 }
tomdc95b8a2014-09-17 08:07:26 -070088
tomc370ebd2014-09-16 01:25:21 -070089 InboundPacket pkt = context.inPacket();
tom642b2262014-09-17 13:52:55 -070090 Ethernet ethPkt = pkt.parsed();
alshabib6eb438a2014-10-01 16:39:37 -070091
tom642b2262014-09-17 13:52:55 -070092 HostId id = HostId.hostId(ethPkt.getDestinationMAC());
tomc370ebd2014-09-16 01:25:21 -070093
94 // Do we know who this is for? If not, flood and bail.
95 Host dst = hostService.getHost(id);
96 if (dst == null) {
97 flood(context);
98 return;
99 }
100
101 // Are we on an edge switch that our destination is on? If so,
102 // simply forward out to the destination and bail.
103 if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) {
alshabib6eb438a2014-10-01 16:39:37 -0700104 if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) {
105 installRule(context, dst.location().port());
106 }
tomc370ebd2014-09-16 01:25:21 -0700107 return;
108 }
109
110 // Otherwise, get a set of paths that lead from here to the
111 // destination edge switch.
112 Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(),
alshabibe9d3a322014-09-23 15:18:33 -0700113 pkt.receivedFrom().deviceId(),
114 dst.location().deviceId());
tomc370ebd2014-09-16 01:25:21 -0700115 if (paths.isEmpty()) {
116 // If there are no paths, flood and bail.
117 flood(context);
118 return;
119 }
120
121 // Otherwise, pick a path that does not lead back to where we
122 // came from; if no such path, flood and bail.
123 Path path = pickForwardPath(paths, pkt.receivedFrom().port());
124 if (path == null) {
tom642b2262014-09-17 13:52:55 -0700125 log.warn("Doh... don't know where to go... {} -> {} received on {}",
alshabibe9d3a322014-09-23 15:18:33 -0700126 ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(),
127 pkt.receivedFrom());
tomc370ebd2014-09-16 01:25:21 -0700128 flood(context);
129 return;
130 }
131
132 // Otherwise forward and be done with it.
alshabib7b2748f2014-09-16 20:21:11 -0700133 installRule(context, path.src().port());
tomc370ebd2014-09-16 01:25:21 -0700134 }
135 }
136
137 // Selects a path from the given set that does not lead back to the
138 // specified port.
139 private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) {
140 for (Path path : paths) {
141 if (!path.src().port().equals(notToPort)) {
142 return path;
143 }
144 }
145 return null;
146 }
147
tom642b2262014-09-17 13:52:55 -0700148 // Floods the specified packet if permissible.
tomc370ebd2014-09-16 01:25:21 -0700149 private void flood(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700150 if (topologyService.isBroadcastPoint(topologyService.currentTopology(),
alshabibe9d3a322014-09-23 15:18:33 -0700151 context.inPacket().receivedFrom())) {
tom642b2262014-09-17 13:52:55 -0700152 packetOut(context, PortNumber.FLOOD);
tomc370ebd2014-09-16 01:25:21 -0700153 } else {
154 context.block();
155 }
156 }
157
tom642b2262014-09-17 13:52:55 -0700158 // Sends a packet out the specified port.
159 private void packetOut(PacketContext context, PortNumber portNumber) {
alshabib010c31d2014-09-26 10:01:12 -0700160 context.treatmentBuilder().setOutput(portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700161 context.send();
162 }
163
164 // Install a rule forwarding the packet to the specified port.
165 private void installRule(PacketContext context, PortNumber portNumber) {
tom642b2262014-09-17 13:52:55 -0700166 // We don't yet support bufferids in the flowservice so packet out first.
167 packetOut(context, portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700168
alshabib7b2748f2014-09-16 20:21:11 -0700169
alshabib7b2748f2014-09-16 20:21:11 -0700170
alshabibc274c902014-10-03 14:58:27 -0700171 // Install the flow rule to handle this type of message from now on.
172 Ethernet inPkt = context.inPacket().parsed();
173 TrafficSelector.Builder builder = DefaultTrafficSelector.builder();
174 builder.matchEthType(inPkt.getEtherType())
175 .matchEthSrc(inPkt.getSourceMAC())
176 .matchEthDst(inPkt.getDestinationMAC())
177 .matchInport(context.inPacket().receivedFrom().port());
alshabib7b2748f2014-09-16 20:21:11 -0700178
alshabibc274c902014-10-03 14:58:27 -0700179 TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
180 treat.setOutput(portNumber);
alshabib6eb438a2014-10-01 16:39:37 -0700181
alshabibc274c902014-10-03 14:58:27 -0700182 FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(),
183 builder.build(), treat.build(), PRIORITY, appId, TIMEOUT);
184
185 flowRuleService.applyFlowRules(f);
186
tomc370ebd2014-09-16 01:25:21 -0700187 }
188
alshabib030111e2014-09-15 15:56:42 -0700189}
190
tomc370ebd2014-09-16 01:25:21 -0700191