blob: 61c7f65bb9d2be576d233f021af2b439dd8a5f84 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
toma9a77c22014-10-03 17:05:20 -070016package org.onlab.onos.ifwd;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070023import org.onlab.onos.core.ApplicationId;
24import org.onlab.onos.core.CoreService;
toma9a77c22014-10-03 17:05:20 -070025import org.onlab.onos.net.Host;
26import org.onlab.onos.net.HostId;
27import org.onlab.onos.net.PortNumber;
28import org.onlab.onos.net.flow.DefaultTrafficSelector;
29import org.onlab.onos.net.flow.DefaultTrafficTreatment;
30import org.onlab.onos.net.flow.TrafficSelector;
31import org.onlab.onos.net.flow.TrafficTreatment;
32import org.onlab.onos.net.host.HostService;
33import org.onlab.onos.net.intent.HostToHostIntent;
toma9a77c22014-10-03 17:05:20 -070034import org.onlab.onos.net.intent.IntentService;
Brian O'Connor958d3812014-10-03 19:46:23 -070035import org.onlab.onos.net.packet.DefaultOutboundPacket;
toma9a77c22014-10-03 17:05:20 -070036import org.onlab.onos.net.packet.InboundPacket;
Brian O'Connor958d3812014-10-03 19:46:23 -070037import org.onlab.onos.net.packet.OutboundPacket;
toma9a77c22014-10-03 17:05:20 -070038import org.onlab.onos.net.packet.PacketContext;
39import org.onlab.onos.net.packet.PacketProcessor;
40import org.onlab.onos.net.packet.PacketService;
41import org.onlab.onos.net.topology.TopologyService;
42import org.onlab.packet.Ethernet;
43import org.slf4j.Logger;
44
Thomas Vachuskab97cf282014-10-20 23:31:12 -070045import static org.slf4j.LoggerFactory.getLogger;
46
toma9a77c22014-10-03 17:05:20 -070047/**
48 * WORK-IN-PROGRESS: Sample reactive forwarding application using intent framework.
49 */
50@Component(immediate = true)
51public class IntentReactiveForwarding {
52
toma9a77c22014-10-03 17:05:20 -070053 private final Logger log = getLogger(getClass());
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Thomas Vachuskab97cf282014-10-20 23:31:12 -070056 protected CoreService coreService;
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
toma9a77c22014-10-03 17:05:20 -070059 protected TopologyService topologyService;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected PacketService packetService;
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected IntentService intentService;
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected HostService hostService;
69
70 private ReactivePacketProcessor processor = new ReactivePacketProcessor();
Thomas Vachuskab97cf282014-10-20 23:31:12 -070071 private ApplicationId appId;
toma9a77c22014-10-03 17:05:20 -070072
73 @Activate
74 public void activate() {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070075 appId = coreService.registerApplication("org.onlab.onos.ifwd");
toma9a77c22014-10-03 17:05:20 -070076 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 2);
tom0511a522014-10-04 12:06:02 -070077 log.info("Started");
toma9a77c22014-10-03 17:05:20 -070078 }
79
80 @Deactivate
81 public void deactivate() {
82 packetService.removeProcessor(processor);
83 processor = null;
84 log.info("Stopped");
85 }
86
toma9a77c22014-10-03 17:05:20 -070087 /**
88 * Packet processor responsible for forwarding packets along their paths.
89 */
90 private class ReactivePacketProcessor implements PacketProcessor {
91
92 @Override
93 public void process(PacketContext context) {
94 // Stop processing if the packet has been handled, since we
95 // can't do any more to it.
96 if (context.isHandled()) {
97 return;
98 }
99
100 InboundPacket pkt = context.inPacket();
101 Ethernet ethPkt = pkt.parsed();
102
103 HostId srcId = HostId.hostId(ethPkt.getSourceMAC());
104 HostId dstId = HostId.hostId(ethPkt.getDestinationMAC());
105
106 // Do we know who this is for? If not, flood and bail.
107 Host dst = hostService.getHost(dstId);
108 if (dst == null) {
109 flood(context);
110 return;
111 }
112
113 // Otherwise forward and be done with it.
114 setUpConnectivity(context, srcId, dstId);
Brian O'Connor958d3812014-10-03 19:46:23 -0700115 forwardPacketToDst(context, dst);
toma9a77c22014-10-03 17:05:20 -0700116 }
117 }
118
119 // Floods the specified packet if permissible.
120 private void flood(PacketContext context) {
121 if (topologyService.isBroadcastPoint(topologyService.currentTopology(),
122 context.inPacket().receivedFrom())) {
123 packetOut(context, PortNumber.FLOOD);
124 } else {
125 context.block();
126 }
127 }
128
129 // Sends a packet out the specified port.
130 private void packetOut(PacketContext context, PortNumber portNumber) {
131 context.treatmentBuilder().setOutput(portNumber);
132 context.send();
133 }
134
Brian O'Connor958d3812014-10-03 19:46:23 -0700135 private void forwardPacketToDst(PacketContext context, Host dst) {
136 TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(dst.location().port()).build();
137 OutboundPacket packet = new DefaultOutboundPacket(dst.location().deviceId(),
tom0511a522014-10-04 12:06:02 -0700138 treatment, context.inPacket().unparsed());
Brian O'Connor958d3812014-10-03 19:46:23 -0700139 packetService.emit(packet);
140 log.info("sending packet: {}", packet);
141 }
142
toma9a77c22014-10-03 17:05:20 -0700143 // Install a rule forwarding the packet to the specified port.
144 private void setUpConnectivity(PacketContext context, HostId srcId, HostId dstId) {
145 TrafficSelector selector = DefaultTrafficSelector.builder().build();
146 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
147
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700148 HostToHostIntent intent = new HostToHostIntent(appId, srcId, dstId,
149 selector, treatment);
toma9a77c22014-10-03 17:05:20 -0700150
151 intentService.submit(intent);
152 }
153
154}