blob: 3af1a895bb0ac9b755f0e471e8c484c7bfeadd4f [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.openflow.controller.impl;
tom7ef8ff92014-09-17 13:08:06 -070017
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080018import com.google.common.collect.ArrayListMultimap;
alshabib64def642014-12-02 23:27:37 -080019import com.google.common.collect.Lists;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080020import com.google.common.collect.Multimap;
tom7ef8ff92014-09-17 13:08:06 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
Jonathan Hartbbd91d42015-02-27 11:18:04 -080024import org.apache.felix.scr.annotations.Modified;
Charles Chan3b00e1b2015-08-26 23:12:52 +080025import org.apache.felix.scr.annotations.Property;
alshabibb452fd72015-04-22 20:46:20 -070026import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
tom7ef8ff92014-09-17 13:08:06 -070028import org.apache.felix.scr.annotations.Service;
Charles Chan3b00e1b2015-08-26 23:12:52 +080029import org.onosproject.cfg.ComponentConfigService;
Charles Chanecfdfb72015-11-24 19:05:50 -080030import org.onosproject.core.CoreService;
Thomas Vachuska3358af22015-05-19 18:40:34 -070031import org.onosproject.net.driver.DefaultDriverProviderService;
alshabibb452fd72015-04-22 20:46:20 -070032import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.openflow.controller.DefaultOpenFlowPacketContext;
34import org.onosproject.openflow.controller.Dpid;
35import org.onosproject.openflow.controller.OpenFlowController;
36import org.onosproject.openflow.controller.OpenFlowEventListener;
Jian Lia78cdb22016-04-21 13:03:58 -070037import org.onosproject.openflow.controller.OpenFlowMessageListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.openflow.controller.OpenFlowPacketContext;
39import org.onosproject.openflow.controller.OpenFlowSwitch;
40import org.onosproject.openflow.controller.OpenFlowSwitchListener;
41import org.onosproject.openflow.controller.PacketListener;
42import org.onosproject.openflow.controller.RoleState;
43import org.onosproject.openflow.controller.driver.OpenFlowAgent;
Jonathan Hartbbd91d42015-02-27 11:18:04 -080044import org.osgi.service.component.ComponentContext;
Marc De Leenheerb9311372015-07-09 11:36:49 -070045import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsEntry;
46import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsReply;
Marc De Leenheer631ffce2014-10-28 16:29:07 -070047import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus;
48import org.projectfloodlight.openflow.protocol.OFExperimenter;
alshabib64def642014-12-02 23:27:37 -080049import org.projectfloodlight.openflow.protocol.OFFactories;
50import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
51import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
sangho6a0bb172015-02-05 12:24:48 -080052import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
53import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
54import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
55import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
tom7ef8ff92014-09-17 13:08:06 -070056import org.projectfloodlight.openflow.protocol.OFMessage;
57import org.projectfloodlight.openflow.protocol.OFPacketIn;
Marc De Leenheer631ffce2014-10-28 16:29:07 -070058import org.projectfloodlight.openflow.protocol.OFPortDesc;
sangho538108b2015-04-08 14:29:20 -070059import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
60import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
tom7ef8ff92014-09-17 13:08:06 -070061import org.projectfloodlight.openflow.protocol.OFPortStatus;
Ayaka Koshibe38594c22014-10-22 13:36:12 -070062import org.projectfloodlight.openflow.protocol.OFStatsReply;
alshabib64def642014-12-02 23:27:37 -080063import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
Jian Li2266bff2016-04-21 11:01:25 -070064import org.projectfloodlight.openflow.protocol.OFTableStatsEntry;
65import org.projectfloodlight.openflow.protocol.OFTableStatsReply;
Marc De Leenheerb9311372015-07-09 11:36:49 -070066import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
67import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
tom7ef8ff92014-09-17 13:08:06 -070068import org.slf4j.Logger;
69import org.slf4j.LoggerFactory;
70
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080071import java.util.Collection;
Marc De Leenheerb9311372015-07-09 11:36:49 -070072import java.util.Collections;
Marc De Leenheerb9311372015-07-09 11:36:49 -070073import java.util.LinkedList;
74import java.util.List;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080075import java.util.Set;
76import java.util.concurrent.ConcurrentHashMap;
HIGUCHI Yuta1979f552015-12-28 21:24:26 -080077import java.util.concurrent.ConcurrentMap;
Jonathan Hart6d44d192015-05-11 18:01:19 -070078import java.util.concurrent.CopyOnWriteArraySet;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080079import java.util.concurrent.ExecutorService;
80import java.util.concurrent.Executors;
81import java.util.concurrent.locks.Lock;
82import java.util.concurrent.locks.ReentrantLock;
Jian Li2266bff2016-04-21 11:01:25 -070083
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080084import static org.onlab.util.Tools.groupedThreads;
tom7ef8ff92014-09-17 13:08:06 -070085
86@Component(immediate = true)
87@Service
88public class OpenFlowControllerImpl implements OpenFlowController {
Charles Chanecfdfb72015-11-24 19:05:50 -080089 private static final String APP_ID = "org.onosproject.openflow-base";
Brian O'Connorff278502015-09-22 14:49:52 -070090 private static final String DEFAULT_OFPORT = "6633,6653";
Charles Chan3b00e1b2015-08-26 23:12:52 +080091 private static final int DEFAULT_WORKER_THREADS = 16;
tom7ef8ff92014-09-17 13:08:06 -070092
93 private static final Logger log =
94 LoggerFactory.getLogger(OpenFlowControllerImpl.class);
95
alshabibb452fd72015-04-22 20:46:20 -070096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanecfdfb72015-11-24 19:05:50 -080097 protected CoreService coreService;
98
99 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibb452fd72015-04-22 20:46:20 -0700100 protected DriverService driverService;
101
Thomas Vachuska3358af22015-05-19 18:40:34 -0700102 // References exists merely for sequencing purpose to assure drivers are loaded
103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected DefaultDriverProviderService defaultDriverProviderService;
105
Charles Chan3b00e1b2015-08-26 23:12:52 +0800106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected ComponentConfigService cfgService;
108
Brian O'Connore2a399e2015-09-22 15:32:50 -0700109 @Property(name = "openflowPorts", value = DEFAULT_OFPORT,
110 label = "Port numbers (comma separated) used by OpenFlow protocol; default is 6633,6653")
111 private String openflowPorts = DEFAULT_OFPORT;
Charles Chan3b00e1b2015-08-26 23:12:52 +0800112
113 @Property(name = "workerThreads", intValue = DEFAULT_WORKER_THREADS,
114 label = "Number of controller worker threads; default is 16")
115 private int workerThreads = DEFAULT_WORKER_THREADS;
116
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800117 protected ExecutorService executorMsgs =
Andrea Campanelladda93562016-03-02 11:08:12 -0800118 Executors.newFixedThreadPool(32, groupedThreads("onos/of", "event-stats-%d", log));
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800119
120 private final ExecutorService executorBarrier =
Andrea Campanelladda93562016-03-02 11:08:12 -0800121 Executors.newFixedThreadPool(4, groupedThreads("onos/of", "event-barrier-%d", log));
alshabib8f1cf4a2014-09-17 14:44:48 -0700122
Prince Pereirae7798032016-07-08 16:31:58 +0530123 //Separate executor thread for handling error messages and barrier replies for same failed
124 // transactions to avoid context switching of thread
125 protected ExecutorService executorErrorMsgs =
126 Executors.newSingleThreadExecutor(groupedThreads("onos/of", "event-error-msg-%d", log));
127
128 //concurrent hashmap to track failed transactions
129 protected ConcurrentMap<Long, Boolean> errorMsgs =
130 new ConcurrentHashMap<>();
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800131 protected ConcurrentMap<Dpid, OpenFlowSwitch> connectedSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700132 new ConcurrentHashMap<>();
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800133 protected ConcurrentMap<Dpid, OpenFlowSwitch> activeMasterSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700134 new ConcurrentHashMap<>();
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800135 protected ConcurrentMap<Dpid, OpenFlowSwitch> activeEqualSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700136 new ConcurrentHashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700137
138 protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent();
Jonathan Hart6d44d192015-05-11 18:01:19 -0700139 protected Set<OpenFlowSwitchListener> ofSwitchListener = new CopyOnWriteArraySet<>();
tom7ef8ff92014-09-17 13:08:06 -0700140
141 protected Multimap<Integer, PacketListener> ofPacketListener =
142 ArrayListMultimap.create();
143
Jonathan Hart6d44d192015-05-11 18:01:19 -0700144 protected Set<OpenFlowEventListener> ofEventListener = new CopyOnWriteArraySet<>();
tom7ef8ff92014-09-17 13:08:06 -0700145
Jian Lia78cdb22016-04-21 13:03:58 -0700146 protected Set<OpenFlowMessageListener> ofMessageListener = new CopyOnWriteArraySet<>();
Jian Li28247b52016-01-07 17:24:15 -0800147
sangho6a0bb172015-02-05 12:24:48 -0800148 protected Multimap<Dpid, OFFlowStatsEntry> fullFlowStats =
149 ArrayListMultimap.create();
150
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700151 protected Multimap<Dpid, OFTableStatsEntry> fullTableStats =
152 ArrayListMultimap.create();
153
sangho6a0bb172015-02-05 12:24:48 -0800154 protected Multimap<Dpid, OFGroupStatsEntry> fullGroupStats =
155 ArrayListMultimap.create();
156
157 protected Multimap<Dpid, OFGroupDescStatsEntry> fullGroupDescStats =
alshabib64def642014-12-02 23:27:37 -0800158 ArrayListMultimap.create();
159
sangho538108b2015-04-08 14:29:20 -0700160 protected Multimap<Dpid, OFPortStatsEntry> fullPortStats =
161 ArrayListMultimap.create();
162
tom7ef8ff92014-09-17 13:08:06 -0700163 private final Controller ctrl = new Controller();
164
165 @Activate
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800166 public void activate(ComponentContext context) {
Andrea Campanella3556f362016-04-28 15:18:10 -0700167 coreService.registerApplication(APP_ID, this::cleanup);
Charles Chan3b00e1b2015-08-26 23:12:52 +0800168 cfgService.registerProperties(getClass());
Brian O'Connorff278502015-09-22 14:49:52 -0700169 ctrl.setConfigParams(context.getProperties());
alshabibb452fd72015-04-22 20:46:20 -0700170 ctrl.start(agent, driverService);
tom7ef8ff92014-09-17 13:08:06 -0700171 }
172
Andrea Campanella3556f362016-04-28 15:18:10 -0700173 private void cleanup() {
174 // Close listening channel and all OF channels. Clean information about switches
175 // before deactivating
Charles Chanecfdfb72015-11-24 19:05:50 -0800176 ctrl.stop();
177 connectedSwitches.values().forEach(OpenFlowSwitch::disconnectSwitch);
Andrea Campanella3556f362016-04-28 15:18:10 -0700178 connectedSwitches.clear();
179 activeMasterSwitches.clear();
180 activeEqualSwitches.clear();
Charles Chanecfdfb72015-11-24 19:05:50 -0800181 }
182
tom7ef8ff92014-09-17 13:08:06 -0700183 @Deactivate
184 public void deactivate() {
Andrea Campanella3556f362016-04-28 15:18:10 -0700185 if (!connectedSwitches.isEmpty()) {
186 cleanup();
187 }
Charles Chan3b00e1b2015-08-26 23:12:52 +0800188 cfgService.unregisterProperties(getClass(), false);
tom7ef8ff92014-09-17 13:08:06 -0700189 }
190
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800191 @Modified
192 public void modified(ComponentContext context) {
Charles Chan3b00e1b2015-08-26 23:12:52 +0800193 ctrl.stop();
Brian O'Connorff278502015-09-22 14:49:52 -0700194 ctrl.setConfigParams(context.getProperties());
Charles Chan3b00e1b2015-08-26 23:12:52 +0800195 ctrl.start(agent, driverService);
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800196 }
197
tom7ef8ff92014-09-17 13:08:06 -0700198 @Override
199 public Iterable<OpenFlowSwitch> getSwitches() {
200 return connectedSwitches.values();
201 }
202
203 @Override
204 public Iterable<OpenFlowSwitch> getMasterSwitches() {
205 return activeMasterSwitches.values();
206 }
207
208 @Override
209 public Iterable<OpenFlowSwitch> getEqualSwitches() {
210 return activeEqualSwitches.values();
211 }
212
213 @Override
214 public OpenFlowSwitch getSwitch(Dpid dpid) {
215 return connectedSwitches.get(dpid);
216 }
217
218 @Override
219 public OpenFlowSwitch getMasterSwitch(Dpid dpid) {
220 return activeMasterSwitches.get(dpid);
221 }
222
223 @Override
224 public OpenFlowSwitch getEqualSwitch(Dpid dpid) {
225 return activeEqualSwitches.get(dpid);
226 }
227
228 @Override
229 public void addListener(OpenFlowSwitchListener listener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700230 if (!ofSwitchListener.contains(listener)) {
231 this.ofSwitchListener.add(listener);
tom7ef8ff92014-09-17 13:08:06 -0700232 }
233 }
234
235 @Override
236 public void removeListener(OpenFlowSwitchListener listener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700237 this.ofSwitchListener.remove(listener);
tom7ef8ff92014-09-17 13:08:06 -0700238 }
239
240 @Override
Jian Lia78cdb22016-04-21 13:03:58 -0700241 public void addMessageListener(OpenFlowMessageListener listener) {
242 ofMessageListener.add(listener);
243 }
244
245 @Override
246 public void removeMessageListener(OpenFlowMessageListener listener) {
247 ofMessageListener.remove(listener);
248 }
249
250 @Override
tom7ef8ff92014-09-17 13:08:06 -0700251 public void addPacketListener(int priority, PacketListener listener) {
252 ofPacketListener.put(priority, listener);
253 }
254
255 @Override
256 public void removePacketListener(PacketListener listener) {
257 ofPacketListener.values().remove(listener);
258 }
259
260 @Override
alshabibeec3a062014-09-17 18:01:26 -0700261 public void addEventListener(OpenFlowEventListener listener) {
262 ofEventListener.add(listener);
263 }
264
265 @Override
266 public void removeEventListener(OpenFlowEventListener listener) {
267 ofEventListener.remove(listener);
268 }
269
270 @Override
tom7ef8ff92014-09-17 13:08:06 -0700271 public void write(Dpid dpid, OFMessage msg) {
272 this.getSwitch(dpid).sendMsg(msg);
273 }
274
275 @Override
276 public void processPacket(Dpid dpid, OFMessage msg) {
sangho6a0bb172015-02-05 12:24:48 -0800277 Collection<OFFlowStatsEntry> flowStats;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700278 Collection<OFTableStatsEntry> tableStats;
sangho6a0bb172015-02-05 12:24:48 -0800279 Collection<OFGroupStatsEntry> groupStats;
280 Collection<OFGroupDescStatsEntry> groupDescStats;
sangho538108b2015-04-08 14:29:20 -0700281 Collection<OFPortStatsEntry> portStats;
sangho6a0bb172015-02-05 12:24:48 -0800282
tom7ef8ff92014-09-17 13:08:06 -0700283 switch (msg.getType()) {
284 case PORT_STATUS:
alshabib8f1cf4a2014-09-17 14:44:48 -0700285 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700286 l.portChanged(dpid, (OFPortStatus) msg);
287 }
288 break;
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700289 case FEATURES_REPLY:
290 for (OpenFlowSwitchListener l : ofSwitchListener) {
291 l.switchChanged(dpid);
292 }
293 break;
tom7ef8ff92014-09-17 13:08:06 -0700294 case PACKET_IN:
295 OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext
296 .packetContextFromPacketIn(this.getSwitch(dpid),
297 (OFPacketIn) msg);
298 for (PacketListener p : ofPacketListener.values()) {
299 p.handlePacket(pktCtx);
300 }
301 break;
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800302 // TODO: Consider using separate threadpool for sensitive messages.
303 // ie. Back to back error could cause us to starve.
304 case FLOW_REMOVED:
Andrea Campanelladda93562016-03-02 11:08:12 -0800305 executorMsgs.execute(new OFMessageHandler(dpid, msg));
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800306 break;
Prince Pereirae7798032016-07-08 16:31:58 +0530307 case ERROR:
308 log.debug("Received error message from {}: {}", dpid, msg);
309 errorMsgs.putIfAbsent(msg.getXid(), true);
310 executorErrorMsgs.execute(new OFMessageHandler(dpid, msg));
311 break;
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700312 case STATS_REPLY:
313 OFStatsReply reply = (OFStatsReply) msg;
sangho6a0bb172015-02-05 12:24:48 -0800314 switch (reply.getStatsType()) {
315 case PORT_DESC:
316 for (OpenFlowSwitchListener l : ofSwitchListener) {
317 l.switchChanged(dpid);
318 }
319 break;
320 case FLOW:
321 flowStats = publishFlowStats(dpid, (OFFlowStatsReply) reply);
322 if (flowStats != null) {
323 OFFlowStatsReply.Builder rep =
324 OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply();
325 rep.setEntries(Lists.newLinkedList(flowStats));
ssyoon9030fbcd92015-08-17 10:42:07 +0900326 rep.setXid(reply.getXid());
Andrea Campanelladda93562016-03-02 11:08:12 -0800327 executorMsgs.execute(new OFMessageHandler(dpid, rep.build()));
sangho6a0bb172015-02-05 12:24:48 -0800328 }
329 break;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700330 case TABLE:
331 tableStats = publishTableStats(dpid, (OFTableStatsReply) reply);
332 if (tableStats != null) {
333 OFTableStatsReply.Builder rep =
334 OFFactories.getFactory(msg.getVersion()).buildTableStatsReply();
335 rep.setEntries(Lists.newLinkedList(tableStats));
Andrea Campanelladda93562016-03-02 11:08:12 -0800336 executorMsgs.execute(new OFMessageHandler(dpid, rep.build()));
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700337 }
338 break;
sangho6a0bb172015-02-05 12:24:48 -0800339 case GROUP:
340 groupStats = publishGroupStats(dpid, (OFGroupStatsReply) reply);
341 if (groupStats != null) {
342 OFGroupStatsReply.Builder rep =
343 OFFactories.getFactory(msg.getVersion()).buildGroupStatsReply();
344 rep.setEntries(Lists.newLinkedList(groupStats));
345 rep.setXid(reply.getXid());
Andrea Campanelladda93562016-03-02 11:08:12 -0800346 executorMsgs.execute(new OFMessageHandler(dpid, rep.build()));
sangho6a0bb172015-02-05 12:24:48 -0800347 }
348 break;
349 case GROUP_DESC:
350 groupDescStats = publishGroupDescStats(dpid,
351 (OFGroupDescStatsReply) reply);
352 if (groupDescStats != null) {
353 OFGroupDescStatsReply.Builder rep =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700354 OFFactories.getFactory(msg.getVersion()).buildGroupDescStatsReply();
sangho6a0bb172015-02-05 12:24:48 -0800355 rep.setEntries(Lists.newLinkedList(groupDescStats));
356 rep.setXid(reply.getXid());
Andrea Campanelladda93562016-03-02 11:08:12 -0800357 executorMsgs.execute(new OFMessageHandler(dpid, rep.build()));
sangho6a0bb172015-02-05 12:24:48 -0800358 }
359 break;
sangho538108b2015-04-08 14:29:20 -0700360 case PORT:
Andrea Campanelladda93562016-03-02 11:08:12 -0800361 executorMsgs.execute(new OFMessageHandler(dpid, reply));
sangho538108b2015-04-08 14:29:20 -0700362 break;
alshabib5eb79392015-08-19 18:09:55 -0700363 case METER:
Andrea Campanelladda93562016-03-02 11:08:12 -0800364 executorMsgs.execute(new OFMessageHandler(dpid, reply));
alshabib5eb79392015-08-19 18:09:55 -0700365 break;
Marc De Leenheerb9311372015-07-09 11:36:49 -0700366 case EXPERIMENTER:
367 if (reply instanceof OFCalientFlowStatsReply) {
368 // Convert Calient flow statistics to regular flow stats
369 // TODO: parse remaining fields such as power levels etc. when we have proper monitoring API
370 OFFlowStatsReply.Builder fsr = getSwitch(dpid).factory().buildFlowStatsReply();
371 List<OFFlowStatsEntry> entries = new LinkedList<>();
372 for (OFCalientFlowStatsEntry entry : ((OFCalientFlowStatsReply) msg).getEntries()) {
373
374 // Single instruction, i.e., output to port
375 OFActionOutput action = OFFactories
376 .getFactory(msg.getVersion())
377 .actions()
378 .buildOutput()
379 .setPort(entry.getOutPort())
380 .build();
381 OFInstruction instruction = OFFactories
382 .getFactory(msg.getVersion())
383 .instructions()
384 .applyActions(Collections.singletonList(action));
385 OFFlowStatsEntry fs = getSwitch(dpid).factory().buildFlowStatsEntry()
386 .setMatch(entry.getMatch())
387 .setTableId(entry.getTableId())
388 .setDurationSec(entry.getDurationSec())
389 .setDurationNsec(entry.getDurationNsec())
390 .setPriority(entry.getPriority())
391 .setIdleTimeout(entry.getIdleTimeout())
392 .setHardTimeout(entry.getHardTimeout())
393 .setFlags(entry.getFlags())
394 .setCookie(entry.getCookie())
395 .setInstructions(Collections.singletonList(instruction))
396 .build();
397 entries.add(fs);
398 }
399 fsr.setEntries(entries);
400
401 flowStats = publishFlowStats(dpid, fsr.build());
402 if (flowStats != null) {
403 OFFlowStatsReply.Builder rep =
404 OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply();
405 rep.setEntries(Lists.newLinkedList(flowStats));
Andrea Campanelladda93562016-03-02 11:08:12 -0800406 executorMsgs.execute(new OFMessageHandler(dpid, rep.build()));
Marc De Leenheerb9311372015-07-09 11:36:49 -0700407 }
408 } else {
Andrea Campanelladda93562016-03-02 11:08:12 -0800409 executorMsgs.execute(new OFMessageHandler(dpid, reply));
Marc De Leenheerb9311372015-07-09 11:36:49 -0700410 }
411 break;
sangho6a0bb172015-02-05 12:24:48 -0800412 default:
alshabib5eb79392015-08-19 18:09:55 -0700413 log.warn("Discarding unknown stats reply type {}", reply.getStatsType());
Marc De Leenheerb9311372015-07-09 11:36:49 -0700414 break;
alshabib64def642014-12-02 23:27:37 -0800415 }
416 break;
alshabib8f1cf4a2014-09-17 14:44:48 -0700417 case BARRIER_REPLY:
Prince Pereirae7798032016-07-08 16:31:58 +0530418 if (errorMsgs.containsKey(msg.getXid())) {
419 //To make oferror msg handling and corresponding barrier reply serialized,
420 // executorErrorMsgs is used for both transaction
421 errorMsgs.remove(msg.getXid());
422 executorErrorMsgs.execute(new OFMessageHandler(dpid, msg));
423 } else {
424 executorBarrier.execute(new OFMessageHandler(dpid, msg));
425 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700426 break;
Marc De Leenheer631ffce2014-10-28 16:29:07 -0700427 case EXPERIMENTER:
Marc De Leenheerb9311372015-07-09 11:36:49 -0700428 long experimenter = ((OFExperimenter) msg).getExperimenter();
429 if (experimenter == 0x748771) {
430 // LINC-OE port stats
Marc De Leenheer631ffce2014-10-28 16:29:07 -0700431 OFCircuitPortStatus circuitPortStatus = (OFCircuitPortStatus) msg;
432 OFPortStatus.Builder portStatus = this.getSwitch(dpid).factory().buildPortStatus();
433 OFPortDesc.Builder portDesc = this.getSwitch(dpid).factory().buildPortDesc();
434 portDesc.setPortNo(circuitPortStatus.getPortNo())
435 .setHwAddr(circuitPortStatus.getHwAddr())
436 .setName(circuitPortStatus.getName())
437 .setConfig(circuitPortStatus.getConfig())
438 .setState(circuitPortStatus.getState());
439 portStatus.setReason(circuitPortStatus.getReason()).setDesc(portDesc.build());
440 for (OpenFlowSwitchListener l : ofSwitchListener) {
441 l.portChanged(dpid, portStatus.build());
442 }
443 } else {
444 log.warn("Handling experimenter type {} not yet implemented",
445 ((OFExperimenter) msg).getExperimenter(), msg);
446 }
447 break;
tom7ef8ff92014-09-17 13:08:06 -0700448 default:
449 log.warn("Handling message type {} not yet implemented {}",
450 msg.getType(), msg);
451 }
452 }
453
sangho6a0bb172015-02-05 12:24:48 -0800454 private synchronized Collection<OFFlowStatsEntry> publishFlowStats(Dpid dpid,
455 OFFlowStatsReply reply) {
alshabib64def642014-12-02 23:27:37 -0800456 //TODO: Get rid of synchronized
sangho6a0bb172015-02-05 12:24:48 -0800457 fullFlowStats.putAll(dpid, reply.getEntries());
alshabib64def642014-12-02 23:27:37 -0800458 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
sangho6a0bb172015-02-05 12:24:48 -0800459 return fullFlowStats.removeAll(dpid);
460 }
461 return null;
462 }
463
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700464 private synchronized Collection<OFTableStatsEntry> publishTableStats(Dpid dpid,
465 OFTableStatsReply reply) {
466 //TODO: Get rid of synchronized
467 fullTableStats.putAll(dpid, reply.getEntries());
468 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
469 return fullTableStats.removeAll(dpid);
470 }
471 return null;
472 }
473
sangho6a0bb172015-02-05 12:24:48 -0800474 private synchronized Collection<OFGroupStatsEntry> publishGroupStats(Dpid dpid,
475 OFGroupStatsReply reply) {
476 //TODO: Get rid of synchronized
477 fullGroupStats.putAll(dpid, reply.getEntries());
478 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
479 return fullGroupStats.removeAll(dpid);
480 }
481 return null;
482 }
483
484 private synchronized Collection<OFGroupDescStatsEntry> publishGroupDescStats(Dpid dpid,
485 OFGroupDescStatsReply reply) {
486 //TODO: Get rid of synchronized
487 fullGroupDescStats.putAll(dpid, reply.getEntries());
488 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
489 return fullGroupDescStats.removeAll(dpid);
alshabib64def642014-12-02 23:27:37 -0800490 }
491 return null;
492 }
493
sangho538108b2015-04-08 14:29:20 -0700494 private synchronized Collection<OFPortStatsEntry> publishPortStats(Dpid dpid,
495 OFPortStatsReply reply) {
496 fullPortStats.putAll(dpid, reply.getEntries());
497 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
498 return fullPortStats.removeAll(dpid);
499 }
500 return null;
501 }
502
tom7ef8ff92014-09-17 13:08:06 -0700503 @Override
504 public void setRole(Dpid dpid, RoleState role) {
Yuta HIGUCHI79cbd1c2014-10-02 16:57:57 -0700505 final OpenFlowSwitch sw = getSwitch(dpid);
506 if (sw == null) {
507 log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role);
508 return;
509 }
510 sw.setRole(role);
tom7ef8ff92014-09-17 13:08:06 -0700511 }
512
513 /**
514 * Implementation of an OpenFlow Agent which is responsible for
515 * keeping track of connected switches and the state in which
516 * they are.
517 */
518 public class OpenFlowSwitchAgent implements OpenFlowAgent {
519
520 private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class);
521 private final Lock switchLock = new ReentrantLock();
522
523 @Override
524 public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) {
alshabib9eab22f2014-10-20 17:17:31 -0700525
tom7ef8ff92014-09-17 13:08:06 -0700526 if (connectedSwitches.get(dpid) != null) {
527 log.error("Trying to add connectedSwitch but found a previous "
528 + "value for dpid: {}", dpid);
529 return false;
530 } else {
Yuta HIGUCHIeb3f30b2014-10-22 11:34:49 -0700531 log.info("Added switch {}", dpid);
tom7ef8ff92014-09-17 13:08:06 -0700532 connectedSwitches.put(dpid, sw);
alshabib8f1cf4a2014-09-17 14:44:48 -0700533 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700534 l.switchAdded(dpid);
535 }
536 return true;
537 }
538 }
539
540 @Override
541 public boolean validActivation(Dpid dpid) {
542 if (connectedSwitches.get(dpid) == null) {
543 log.error("Trying to activate switch but is not in "
544 + "connected switches: dpid {}. Aborting ..",
545 dpid);
546 return false;
547 }
548 if (activeMasterSwitches.get(dpid) != null ||
549 activeEqualSwitches.get(dpid) != null) {
550 log.error("Trying to activate switch but it is already "
551 + "activated: dpid {}. Found in activeMaster: {} "
Ray Milkey6bc43c22015-11-06 13:22:38 -0800552 + "Found in activeEqual: {}. Aborting ..",
553 dpid,
554 (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y',
555 (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y');
tom7ef8ff92014-09-17 13:08:06 -0700556 return false;
557 }
558 return true;
559 }
560
561
562 @Override
563 public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) {
564 switchLock.lock();
565 try {
566 if (!validActivation(dpid)) {
567 return false;
568 }
569 activeMasterSwitches.put(dpid, sw);
570 return true;
571 } finally {
572 switchLock.unlock();
573 }
574 }
575
576 @Override
577 public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) {
578 switchLock.lock();
579 try {
580 if (!validActivation(dpid)) {
581 return false;
582 }
583 activeEqualSwitches.put(dpid, sw);
584 log.info("Added Activated EQUAL Switch {}", dpid);
585 return true;
586 } finally {
587 switchLock.unlock();
588 }
589 }
590
591 @Override
592 public void transitionToMasterSwitch(Dpid dpid) {
593 switchLock.lock();
594 try {
595 if (activeMasterSwitches.containsKey(dpid)) {
596 return;
597 }
598 OpenFlowSwitch sw = activeEqualSwitches.remove(dpid);
599 if (sw == null) {
600 sw = getSwitch(dpid);
601 if (sw == null) {
602 log.error("Transition to master called on sw {}, but switch "
603 + "was not found in controller-cache", dpid);
604 return;
605 }
606 }
607 log.info("Transitioned switch {} to MASTER", dpid);
608 activeMasterSwitches.put(dpid, sw);
609 } finally {
610 switchLock.unlock();
611 }
612 }
613
614
615 @Override
616 public void transitionToEqualSwitch(Dpid dpid) {
617 switchLock.lock();
618 try {
619 if (activeEqualSwitches.containsKey(dpid)) {
620 return;
621 }
622 OpenFlowSwitch sw = activeMasterSwitches.remove(dpid);
623 if (sw == null) {
624 sw = getSwitch(dpid);
625 if (sw == null) {
626 log.error("Transition to equal called on sw {}, but switch "
627 + "was not found in controller-cache", dpid);
628 return;
629 }
630 }
631 log.info("Transitioned switch {} to EQUAL", dpid);
632 activeEqualSwitches.put(dpid, sw);
633 } finally {
634 switchLock.unlock();
635 }
636
637 }
638
639 @Override
640 public void removeConnectedSwitch(Dpid dpid) {
641 connectedSwitches.remove(dpid);
642 OpenFlowSwitch sw = activeMasterSwitches.remove(dpid);
643 if (sw == null) {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700644 log.debug("sw was null for {}", dpid);
tom7ef8ff92014-09-17 13:08:06 -0700645 sw = activeEqualSwitches.remove(dpid);
646 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700647 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700648 l.switchRemoved(dpid);
649 }
650 }
651
652 @Override
Jian Lia78cdb22016-04-21 13:03:58 -0700653 public void processDownstreamMessage(Dpid dpid, List<OFMessage> m) {
654 for (OpenFlowMessageListener listener : ofMessageListener) {
655 listener.handleOutgoingMessage(dpid, m);
656 }
657 }
658
659
660 @Override
tom7ef8ff92014-09-17 13:08:06 -0700661 public void processMessage(Dpid dpid, OFMessage m) {
662 processPacket(dpid, m);
Jian Lia78cdb22016-04-21 13:03:58 -0700663
664 for (OpenFlowMessageListener listener : ofMessageListener) {
665 listener.handleIncomingMessage(dpid, m);
666 }
tom7ef8ff92014-09-17 13:08:06 -0700667 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700668
669 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700670 public void returnRoleReply(Dpid dpid, RoleState requested, RoleState response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700671 for (OpenFlowSwitchListener l : ofSwitchListener) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700672 l.receivedRoleReply(dpid, requested, response);
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700673 }
674 }
tom7ef8ff92014-09-17 13:08:06 -0700675 }
676
Jian Li152b8852015-12-07 14:47:25 -0800677 /**
Jian Li2266bff2016-04-21 11:01:25 -0700678 * OpenFlow message handler.
Jian Li152b8852015-12-07 14:47:25 -0800679 */
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800680 protected final class OFMessageHandler implements Runnable {
alshabib8f1cf4a2014-09-17 14:44:48 -0700681
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800682 protected final OFMessage msg;
683 protected final Dpid dpid;
alshabib8f1cf4a2014-09-17 14:44:48 -0700684
685 public OFMessageHandler(Dpid dpid, OFMessage msg) {
686 this.msg = msg;
687 this.dpid = dpid;
688 }
689
690 @Override
691 public void run() {
alshabibeec3a062014-09-17 18:01:26 -0700692 for (OpenFlowEventListener listener : ofEventListener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700693 listener.handleMessage(dpid, msg);
694 }
695 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700696 }
tom7ef8ff92014-09-17 13:08:06 -0700697}