blob: 0dd6aef895d06128e6b24ed5bdfe2811d2c44191 [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) {
tomdc95b8a2014-09-17 08:07:26 -070077 // Stop processing if the packet has been handled, since we
78 // can't do any more to it.
alshabib7b2748f2014-09-16 20:21:11 -070079 if (context.isHandled()) {
80 return;
81 }
tomdc95b8a2014-09-17 08:07:26 -070082
tomc370ebd2014-09-16 01:25:21 -070083 InboundPacket pkt = context.inPacket();
84 HostId id = HostId.hostId(pkt.parsed().getDestinationMAC());
85
86 // Do we know who this is for? If not, flood and bail.
87 Host dst = hostService.getHost(id);
88 if (dst == null) {
89 flood(context);
90 return;
91 }
92
93 // Are we on an edge switch that our destination is on? If so,
94 // simply forward out to the destination and bail.
95 if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) {
alshabib7b2748f2014-09-16 20:21:11 -070096 installRule(context, dst.location().port());
tomc370ebd2014-09-16 01:25:21 -070097 return;
98 }
99
100 // Otherwise, get a set of paths that lead from here to the
101 // destination edge switch.
102 Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(),
tomdc95b8a2014-09-17 08:07:26 -0700103 context.inPacket().receivedFrom().deviceId(),
104 dst.location().deviceId());
tomc370ebd2014-09-16 01:25:21 -0700105 if (paths.isEmpty()) {
106 // If there are no paths, flood and bail.
107 flood(context);
108 return;
109 }
110
111 // Otherwise, pick a path that does not lead back to where we
112 // came from; if no such path, flood and bail.
113 Path path = pickForwardPath(paths, pkt.receivedFrom().port());
114 if (path == null) {
115 log.warn("Doh... don't know where to go...");
116 flood(context);
117 return;
118 }
119
120 // Otherwise forward and be done with it.
alshabib7b2748f2014-09-16 20:21:11 -0700121 installRule(context, path.src().port());
tomc370ebd2014-09-16 01:25:21 -0700122 }
123 }
124
125 // Selects a path from the given set that does not lead back to the
126 // specified port.
127 private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) {
128 for (Path path : paths) {
129 if (!path.src().port().equals(notToPort)) {
130 return path;
131 }
132 }
133 return null;
134 }
135
136 // Floods the specified packet.
137 private void flood(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700138 if (topologyService.isBroadcastPoint(topologyService.currentTopology(),
139 context.inPacket().receivedFrom())) {
alshabib7b2748f2014-09-16 20:21:11 -0700140 packetOutFlood(context);
tomc370ebd2014-09-16 01:25:21 -0700141 } else {
142 context.block();
143 }
144 }
145
alshabib7b2748f2014-09-16 20:21:11 -0700146 //Floods a packet out
147 private void packetOutFlood(PacketContext context) {
148 context.treatmentBuilder().add(Instructions.createOutput(PortNumber.FLOOD));
149 context.send();
150 }
151
152 // Install a rule forwarding the packet to the specified port.
153 private void installRule(PacketContext context, PortNumber portNumber) {
alshabib7b2748f2014-09-16 20:21:11 -0700154 Ethernet inPkt = context.inPacket().parsed();
155 TrafficSelector.Builder builder = new DefaultTrafficSelector.Builder();
156 builder.add(Criteria.matchEthType(inPkt.getEtherType()))
tomdc95b8a2014-09-17 08:07:26 -0700157 .add(Criteria.matchEthSrc(inPkt.getSourceMAC()))
158 .add(Criteria.matchEthDst(inPkt.getDestinationMAC()))
159 .add(Criteria.matchInPort(context.inPacket().receivedFrom().port()));
alshabib7b2748f2014-09-16 20:21:11 -0700160
161 TrafficTreatment.Builder treat = new DefaultTrafficTreatment.Builder();
162 treat.add(Instructions.createOutput(portNumber));
163
164 FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(),
tomdc95b8a2014-09-17 08:07:26 -0700165 builder.build(), treat.build());
alshabib7b2748f2014-09-16 20:21:11 -0700166
167 flowRuleService.applyFlowRules(f);
168
tomdc95b8a2014-09-17 08:07:26 -0700169 // we don't yet support bufferids in the flowservice so packet out and
170 // then install a flowmod.
171 context.treatmentBuilder().add(Instructions.createOutput(portNumber));
172 context.send();
tomc370ebd2014-09-16 01:25:21 -0700173 }
174
alshabib030111e2014-09-15 15:56:42 -0700175}
176
tomc370ebd2014-09-16 01:25:21 -0700177