blob: ce8593884390ca9c5a55b90afc6746ef0e5347fc [file] [log] [blame]
alshabib030111e2014-09-15 15:56:42 -07001package org.onlab.onos.fwd;
2
alshabib7b2748f2014-09-16 20:21:11 -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;
tomc370ebd2014-09-16 01:25:21 -070012import org.onlab.onos.net.Host;
13import org.onlab.onos.net.HostId;
14import org.onlab.onos.net.Path;
15import org.onlab.onos.net.PortNumber;
alshabib7b2748f2014-09-16 20:21:11 -070016import org.onlab.onos.net.flow.DefaultFlowRule;
17import org.onlab.onos.net.flow.DefaultTrafficSelector;
18import org.onlab.onos.net.flow.DefaultTrafficTreatment;
19import org.onlab.onos.net.flow.FlowRule;
20import org.onlab.onos.net.flow.FlowRuleService;
21import org.onlab.onos.net.flow.TrafficSelector;
22import org.onlab.onos.net.flow.TrafficTreatment;
23import org.onlab.onos.net.flow.criteria.Criteria;
alshabib55a55d92014-09-16 11:59:31 -070024import org.onlab.onos.net.flow.instructions.Instructions;
alshabib8aef1ad2014-09-15 17:47:31 -070025import org.onlab.onos.net.host.HostService;
tomc370ebd2014-09-16 01:25:21 -070026import org.onlab.onos.net.packet.InboundPacket;
27import org.onlab.onos.net.packet.PacketContext;
alshabib030111e2014-09-15 15:56:42 -070028import org.onlab.onos.net.packet.PacketProcessor;
29import org.onlab.onos.net.packet.PacketService;
30import org.onlab.onos.net.topology.TopologyService;
alshabib7b2748f2014-09-16 20:21:11 -070031import org.onlab.packet.Ethernet;
tomc370ebd2014-09-16 01:25:21 -070032import org.slf4j.Logger;
alshabib030111e2014-09-15 15:56:42 -070033
tomc370ebd2014-09-16 01:25:21 -070034/**
35 * Sample reactive forwarding application.
36 */
37@Component(immediate = true)
alshabib030111e2014-09-15 15:56:42 -070038public class ReactiveForwarding {
39
tomc370ebd2014-09-16 01:25:21 -070040 private final Logger log = getLogger(getClass());
41
alshabib030111e2014-09-15 15:56:42 -070042 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
43 protected TopologyService topologyService;
44
45 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
46 protected PacketService packetService;
47
alshabib8aef1ad2014-09-15 17:47:31 -070048 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected HostService hostService;
50
alshabib7b2748f2014-09-16 20:21:11 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected FlowRuleService flowRuleService;
53
tomc370ebd2014-09-16 01:25:21 -070054 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
alshabib030111e2014-09-15 15:56:42 -070055
56 @Activate
57 public void activate() {
alshabib030111e2014-09-15 15:56:42 -070058 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1);
tomc370ebd2014-09-16 01:25:21 -070059 log.info("Started");
alshabib030111e2014-09-15 15:56:42 -070060 }
61
62 @Deactivate
63 public void deactivate() {
64 packetService.removeProcessor(processor);
65 processor = null;
tomc370ebd2014-09-16 01:25:21 -070066 log.info("Stopped");
alshabib030111e2014-09-15 15:56:42 -070067 }
tomc370ebd2014-09-16 01:25:21 -070068
69
70 /**
71 * Packet processor responsible for forwarding packets along their paths.
72 */
73 private class ReactivePacketProcessor implements PacketProcessor {
74
75 @Override
76 public void process(PacketContext context) {
alshabib7b2748f2014-09-16 20:21:11 -070077 if (context.isHandled()) {
78 return;
79 }
tomc370ebd2014-09-16 01:25:21 -070080 InboundPacket pkt = context.inPacket();
81 HostId id = HostId.hostId(pkt.parsed().getDestinationMAC());
82
83 // Do we know who this is for? If not, flood and bail.
84 Host dst = hostService.getHost(id);
85 if (dst == null) {
86 flood(context);
87 return;
88 }
89
90 // Are we on an edge switch that our destination is on? If so,
91 // simply forward out to the destination and bail.
92 if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) {
alshabib7b2748f2014-09-16 20:21:11 -070093 installRule(context, dst.location().port());
tomc370ebd2014-09-16 01:25:21 -070094 return;
95 }
96
97 // Otherwise, get a set of paths that lead from here to the
98 // destination edge switch.
alshabib7b2748f2014-09-16 20:21:11 -070099
tomc370ebd2014-09-16 01:25:21 -0700100 Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(),
alshabib7b2748f2014-09-16 20:21:11 -0700101 context.inPacket().receivedFrom().deviceId(),
102 dst.location().deviceId());
tomc370ebd2014-09-16 01:25:21 -0700103 if (paths.isEmpty()) {
104 // If there are no paths, flood and bail.
105 flood(context);
106 return;
107 }
108
109 // Otherwise, pick a path that does not lead back to where we
110 // came from; if no such path, flood and bail.
111 Path path = pickForwardPath(paths, pkt.receivedFrom().port());
112 if (path == null) {
113 log.warn("Doh... don't know where to go...");
114 flood(context);
115 return;
116 }
117
118 // Otherwise forward and be done with it.
alshabib7b2748f2014-09-16 20:21:11 -0700119 installRule(context, path.src().port());
tomc370ebd2014-09-16 01:25:21 -0700120 }
121 }
122
123 // Selects a path from the given set that does not lead back to the
124 // specified port.
125 private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) {
126 for (Path path : paths) {
127 if (!path.src().port().equals(notToPort)) {
128 return path;
129 }
130 }
131 return null;
132 }
133
134 // Floods the specified packet.
135 private void flood(PacketContext context) {
136 boolean canBcast = topologyService.isBroadcastPoint(topologyService.currentTopology(),
alshabib7b2748f2014-09-16 20:21:11 -0700137 context.inPacket().receivedFrom());
tomc370ebd2014-09-16 01:25:21 -0700138 if (canBcast) {
alshabib7b2748f2014-09-16 20:21:11 -0700139 packetOutFlood(context);
tomc370ebd2014-09-16 01:25:21 -0700140 } else {
141 context.block();
142 }
143 }
144
alshabib7b2748f2014-09-16 20:21:11 -0700145 //Floods a packet out
146 private void packetOutFlood(PacketContext context) {
147 context.treatmentBuilder().add(Instructions.createOutput(PortNumber.FLOOD));
148 context.send();
149 }
150
151 // Install a rule forwarding the packet to the specified port.
152 private void installRule(PacketContext context, PortNumber portNumber) {
153 // we don't yet support bufferids in the flowservice so packet out and
154 // then install a flowmod.
tomc370ebd2014-09-16 01:25:21 -0700155 context.treatmentBuilder().add(Instructions.createOutput(portNumber));
156 context.send();
alshabib7b2748f2014-09-16 20:21:11 -0700157
158
159 Ethernet inPkt = context.inPacket().parsed();
160 TrafficSelector.Builder builder = new DefaultTrafficSelector.Builder();
161 builder.add(Criteria.matchEthType(inPkt.getEtherType()))
162 .add(Criteria.matchEthSrc(inPkt.getSourceMAC()))
163 .add(Criteria.matchEthDst(inPkt.getDestinationMAC()))
164 .add(Criteria.matchInPort(context.inPacket().receivedFrom().port()));
165
166 TrafficTreatment.Builder treat = new DefaultTrafficTreatment.Builder();
167 treat.add(Instructions.createOutput(portNumber));
168
169 FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(),
170 builder.build(), treat.build());
171
172 flowRuleService.applyFlowRules(f);
173
tomc370ebd2014-09-16 01:25:21 -0700174 }
175
alshabib030111e2014-09-15 15:56:42 -0700176}
177
tomc370ebd2014-09-16 01:25:21 -0700178