blob: f7fbdbbfbc68048bb5b58d0b1ddbaff6e1f5b36f [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;
alshabibb5522ff2014-09-29 19:20:00 -070029import org.onlab.onos.net.proxyarp.ProxyArpService;
alshabib030111e2014-09-15 15:56:42 -070030import org.onlab.onos.net.topology.TopologyService;
alshabibb5522ff2014-09-29 19:20:00 -070031import org.onlab.packet.ARP;
alshabib7b2748f2014-09-16 20:21:11 -070032import org.onlab.packet.Ethernet;
tomc370ebd2014-09-16 01:25:21 -070033import org.slf4j.Logger;
alshabib030111e2014-09-15 15:56:42 -070034
tomc370ebd2014-09-16 01:25:21 -070035/**
36 * Sample reactive forwarding application.
37 */
38@Component(immediate = true)
alshabib030111e2014-09-15 15:56:42 -070039public class ReactiveForwarding {
40
tomc370ebd2014-09-16 01:25:21 -070041 private final Logger log = getLogger(getClass());
42
alshabib030111e2014-09-15 15:56:42 -070043 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
44 protected TopologyService topologyService;
45
46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 protected PacketService packetService;
48
alshabib8aef1ad2014-09-15 17:47:31 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected HostService hostService;
51
alshabib7b2748f2014-09-16 20:21:11 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected FlowRuleService flowRuleService;
54
alshabibb5522ff2014-09-29 19:20:00 -070055 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected ProxyArpService proxyArpService;
57
tomc370ebd2014-09-16 01:25:21 -070058 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
alshabib030111e2014-09-15 15:56:42 -070059
alshabiba68eb962014-09-24 20:34:13 -070060 private ApplicationId appId;
61
alshabib030111e2014-09-15 15:56:42 -070062 @Activate
63 public void activate() {
alshabiba68eb962014-09-24 20:34:13 -070064 appId = ApplicationId.getAppId();
alshabib030111e2014-09-15 15:56:42 -070065 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1);
alshabiba68eb962014-09-24 20:34:13 -070066 log.info("Started with Application ID {}", appId.id());
alshabib030111e2014-09-15 15:56:42 -070067 }
68
69 @Deactivate
70 public void deactivate() {
alshabiba68eb962014-09-24 20:34:13 -070071 flowRuleService.removeFlowRulesById(appId);
alshabib030111e2014-09-15 15:56:42 -070072 packetService.removeProcessor(processor);
73 processor = null;
tomc370ebd2014-09-16 01:25:21 -070074 log.info("Stopped");
alshabib030111e2014-09-15 15:56:42 -070075 }
tomc370ebd2014-09-16 01:25:21 -070076
77
78 /**
79 * Packet processor responsible for forwarding packets along their paths.
80 */
81 private class ReactivePacketProcessor implements PacketProcessor {
82
83 @Override
84 public void process(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -070085 // Stop processing if the packet has been handled, since we
86 // can't do any more to it.
alshabib7b2748f2014-09-16 20:21:11 -070087 if (context.isHandled()) {
88 return;
89 }
tomdc95b8a2014-09-17 08:07:26 -070090
tomc370ebd2014-09-16 01:25:21 -070091 InboundPacket pkt = context.inPacket();
tom642b2262014-09-17 13:52:55 -070092 Ethernet ethPkt = pkt.parsed();
alshabibb5522ff2014-09-29 19:20:00 -070093 if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
94 ARP arp = (ARP) ethPkt.getPayload();
95 if (arp.getOpCode() == ARP.OP_REPLY) {
96 proxyArpService.forward(ethPkt);
97 } else if (arp.getOpCode() == ARP.OP_REQUEST) {
98 proxyArpService.reply(ethPkt);
99 }
100 context.block();
101 return;
102 }
alshabib6eb438a2014-10-01 16:39:37 -0700103
tom642b2262014-09-17 13:52:55 -0700104 HostId id = HostId.hostId(ethPkt.getDestinationMAC());
tomc370ebd2014-09-16 01:25:21 -0700105
106 // Do we know who this is for? If not, flood and bail.
107 Host dst = hostService.getHost(id);
108 if (dst == null) {
109 flood(context);
110 return;
111 }
112
113 // Are we on an edge switch that our destination is on? If so,
114 // simply forward out to the destination and bail.
115 if (pkt.receivedFrom().deviceId().equals(dst.location().deviceId())) {
alshabib6eb438a2014-10-01 16:39:37 -0700116 if (!context.inPacket().receivedFrom().port().equals(dst.location().port())) {
117 installRule(context, dst.location().port());
118 }
tomc370ebd2014-09-16 01:25:21 -0700119 return;
120 }
121
122 // Otherwise, get a set of paths that lead from here to the
123 // destination edge switch.
124 Set<Path> paths = topologyService.getPaths(topologyService.currentTopology(),
alshabibe9d3a322014-09-23 15:18:33 -0700125 pkt.receivedFrom().deviceId(),
126 dst.location().deviceId());
tomc370ebd2014-09-16 01:25:21 -0700127 if (paths.isEmpty()) {
128 // If there are no paths, flood and bail.
129 flood(context);
130 return;
131 }
132
133 // Otherwise, pick a path that does not lead back to where we
134 // came from; if no such path, flood and bail.
135 Path path = pickForwardPath(paths, pkt.receivedFrom().port());
136 if (path == null) {
tom642b2262014-09-17 13:52:55 -0700137 log.warn("Doh... don't know where to go... {} -> {} received on {}",
alshabibe9d3a322014-09-23 15:18:33 -0700138 ethPkt.getSourceMAC(), ethPkt.getDestinationMAC(),
139 pkt.receivedFrom());
tomc370ebd2014-09-16 01:25:21 -0700140 flood(context);
141 return;
142 }
143
144 // Otherwise forward and be done with it.
alshabib7b2748f2014-09-16 20:21:11 -0700145 installRule(context, path.src().port());
tomc370ebd2014-09-16 01:25:21 -0700146 }
147 }
148
149 // Selects a path from the given set that does not lead back to the
150 // specified port.
151 private Path pickForwardPath(Set<Path> paths, PortNumber notToPort) {
152 for (Path path : paths) {
153 if (!path.src().port().equals(notToPort)) {
154 return path;
155 }
156 }
157 return null;
158 }
159
tom642b2262014-09-17 13:52:55 -0700160 // Floods the specified packet if permissible.
tomc370ebd2014-09-16 01:25:21 -0700161 private void flood(PacketContext context) {
tomdc95b8a2014-09-17 08:07:26 -0700162 if (topologyService.isBroadcastPoint(topologyService.currentTopology(),
alshabibe9d3a322014-09-23 15:18:33 -0700163 context.inPacket().receivedFrom())) {
tom642b2262014-09-17 13:52:55 -0700164 packetOut(context, PortNumber.FLOOD);
tomc370ebd2014-09-16 01:25:21 -0700165 } else {
166 context.block();
167 }
168 }
169
tom642b2262014-09-17 13:52:55 -0700170 // Sends a packet out the specified port.
171 private void packetOut(PacketContext context, PortNumber portNumber) {
alshabib010c31d2014-09-26 10:01:12 -0700172 context.treatmentBuilder().setOutput(portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700173 context.send();
174 }
175
176 // Install a rule forwarding the packet to the specified port.
177 private void installRule(PacketContext context, PortNumber portNumber) {
tom642b2262014-09-17 13:52:55 -0700178 // We don't yet support bufferids in the flowservice so packet out first.
179 packetOut(context, portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700180
alshabib6eb438a2014-10-01 16:39:37 -0700181 if (context.inPacket().parsed().getEtherType() == Ethernet.TYPE_IPV4) {
alshabib7b2748f2014-09-16 20:21:11 -0700182
alshabib6eb438a2014-10-01 16:39:37 -0700183 // Install the flow rule to handle this type of message from now on.
184 Ethernet inPkt = context.inPacket().parsed();
185 TrafficSelector.Builder builder = new DefaultTrafficSelector.Builder();
186 builder.matchEthType(inPkt.getEtherType())
187 .matchEthSrc(inPkt.getSourceMAC())
188 .matchEthDst(inPkt.getDestinationMAC())
189 .matchInport(context.inPacket().receivedFrom().port());
alshabib7b2748f2014-09-16 20:21:11 -0700190
alshabib6eb438a2014-10-01 16:39:37 -0700191 TrafficTreatment.Builder treat = new DefaultTrafficTreatment.Builder();
192 treat.setOutput(portNumber);
alshabib7b2748f2014-09-16 20:21:11 -0700193
alshabib6eb438a2014-10-01 16:39:37 -0700194 FlowRule f = new DefaultFlowRule(context.inPacket().receivedFrom().deviceId(),
195 builder.build(), treat.build(), 0, appId);
196
197 flowRuleService.applyFlowRules(f);
198 }
tomc370ebd2014-09-16 01:25:21 -0700199 }
200
alshabib030111e2014-09-15 15:56:42 -0700201}
202
tomc370ebd2014-09-16 01:25:21 -0700203