blob: 7669294f9b7e39443f3f2d0af7d76c33b9debcea [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) {
alshabib9842a4e2014-09-16 21:42:35 -070077 /*
78 * stop processing if the packet has been handled,
79 * we can't do any more to it
80 */
alshabib7b2748f2014-09-16 20:21:11 -070081 if (context.isHandled()) {
82 return;
83 }
tomc370ebd2014-09-16 01:25:21 -070084 InboundPacket pkt = context.inPacket();
85 HostId id = HostId.hostId(pkt.parsed().getDestinationMAC());
86
87 // Do we know who this is for? If not, flood and bail.
88 Host dst = hostService.getHost(id);
89 if (dst == null) {
90 flood(context);
91 return;
92 }
93
94 // Are we on an edge switch that our destination is on? If so,
95 // simply forward out to the destination and bail.
96 if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) {
alshabib7b2748f2014-09-16 20:21:11 -070097 installRule(context, dst.location().port());
tomc370ebd2014-09-16 01:25:21 -070098 return;
99 }
100
101 // Otherwise, get a set of paths that lead from here to the
102 // destination edge switch.
alshabib7b2748f2014-09-16 20:21:11 -0700103
tomc370ebd2014-09-16 01:25:21 -0700104 Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(),
alshabib7b2748f2014-09-16 20:21:11 -0700105 context.inPacket().receivedFrom().deviceId(),
106 dst.location().deviceId());
tomc370ebd2014-09-16 01:25:21 -0700107 if (paths.isEmpty()) {
108 // If there are no paths, flood and bail.
109 flood(context);
110 return;
111 }
112
113 // Otherwise, pick a path that does not lead back to where we
114 // came from; if no such path, flood and bail.
115 Path path = pickForwardPath(paths, pkt.receivedFrom().port());
116 if (path == null) {
117 log.warn("Doh... don't know where to go...");
118 flood(context);
119 return;
120 }
121
122 // Otherwise forward and be done with it.
alshabib7b2748f2014-09-16 20:21:11 -0700123 installRule(context, path.src().port());
tomc370ebd2014-09-16 01:25:21 -0700124 }
125 }
126
127 // Selects a path from the given set that does not lead back to the
128 // specified port.
129 private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) {
130 for (Path path : paths) {
131 if (!path.src().port().equals(notToPort)) {
132 return path;
133 }
134 }
135 return null;
136 }
137
138 // Floods the specified packet.
139 private void flood(PacketContext context) {
140 boolean canBcast = topologyService.isBroadcastPoint(topologyService.currentTopology(),
alshabib7b2748f2014-09-16 20:21:11 -0700141 context.inPacket().receivedFrom());
tomc370ebd2014-09-16 01:25:21 -0700142 if (canBcast) {
alshabib7b2748f2014-09-16 20:21:11 -0700143 packetOutFlood(context);
tomc370ebd2014-09-16 01:25:21 -0700144 } else {
145 context.block();
146 }
147 }
148
alshabib7b2748f2014-09-16 20:21:11 -0700149 //Floods a packet out
150 private void packetOutFlood(PacketContext context) {
151 context.treatmentBuilder().add(Instructions.createOutput(PortNumber.FLOOD));
152 context.send();
153 }
154
155 // Install a rule forwarding the packet to the specified port.
156 private void installRule(PacketContext context, PortNumber portNumber) {
157 // we don't yet support bufferids in the flowservice so packet out and
158 // then install a flowmod.
tomc370ebd2014-09-16 01:25:21 -0700159 context.treatmentBuilder().add(Instructions.createOutput(portNumber));
160 context.send();
alshabib7b2748f2014-09-16 20:21:11 -0700161
162
163 Ethernet inPkt = context.inPacket().parsed();
164 TrafficSelector.Builder builder = new DefaultTrafficSelector.Builder();
165 builder.add(Criteria.matchEthType(inPkt.getEtherType()))
166 .add(Criteria.matchEthSrc(inPkt.getSourceMAC()))
167 .add(Criteria.matchEthDst(inPkt.getDestinationMAC()))
168 .add(Criteria.matchInPort(context.inPacket().receivedFrom().port()));
169
170 TrafficTreatment.Builder treat = new DefaultTrafficTreatment.Builder();
171 treat.add(Instructions.createOutput(portNumber));
172
173 FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(),
174 builder.build(), treat.build());
175
176 flowRuleService.applyFlowRules(f);
177
tomc370ebd2014-09-16 01:25:21 -0700178 }
179
alshabib030111e2014-09-15 15:56:42 -0700180}
181
tomc370ebd2014-09-16 01:25:21 -0700182