blob: 333a7565870df4ddefae3aec3ca880fb63db0d87 [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;
37import org.onosproject.openflow.controller.OpenFlowPacketContext;
38import org.onosproject.openflow.controller.OpenFlowSwitch;
39import org.onosproject.openflow.controller.OpenFlowSwitchListener;
40import org.onosproject.openflow.controller.PacketListener;
41import org.onosproject.openflow.controller.RoleState;
42import org.onosproject.openflow.controller.driver.OpenFlowAgent;
Jonathan Hartbbd91d42015-02-27 11:18:04 -080043import org.osgi.service.component.ComponentContext;
Marc De Leenheerb9311372015-07-09 11:36:49 -070044import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsEntry;
45import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsReply;
Marc De Leenheer631ffce2014-10-28 16:29:07 -070046import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus;
47import org.projectfloodlight.openflow.protocol.OFExperimenter;
alshabib64def642014-12-02 23:27:37 -080048import org.projectfloodlight.openflow.protocol.OFFactories;
49import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
50import org.projectfloodlight.openflow.protocol.OFFlowStatsReply;
sangho6a0bb172015-02-05 12:24:48 -080051import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
52import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
53import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
54import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
tom7ef8ff92014-09-17 13:08:06 -070055import org.projectfloodlight.openflow.protocol.OFMessage;
56import org.projectfloodlight.openflow.protocol.OFPacketIn;
Marc De Leenheer631ffce2014-10-28 16:29:07 -070057import org.projectfloodlight.openflow.protocol.OFPortDesc;
sangho538108b2015-04-08 14:29:20 -070058import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
59import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
tom7ef8ff92014-09-17 13:08:06 -070060import org.projectfloodlight.openflow.protocol.OFPortStatus;
Ayaka Koshibe38594c22014-10-22 13:36:12 -070061import org.projectfloodlight.openflow.protocol.OFStatsReply;
alshabib64def642014-12-02 23:27:37 -080062import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
Jian Li2266bff2016-04-21 11:01:25 -070063import org.projectfloodlight.openflow.protocol.OFTableStatsEntry;
64import org.projectfloodlight.openflow.protocol.OFTableStatsReply;
Marc De Leenheerb9311372015-07-09 11:36:49 -070065import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
66import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
tom7ef8ff92014-09-17 13:08:06 -070067import org.slf4j.Logger;
68import org.slf4j.LoggerFactory;
69
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080070import java.util.Collection;
Marc De Leenheerb9311372015-07-09 11:36:49 -070071import java.util.Collections;
Marc De Leenheerb9311372015-07-09 11:36:49 -070072import java.util.LinkedList;
73import java.util.List;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080074import java.util.Set;
75import java.util.concurrent.ConcurrentHashMap;
HIGUCHI Yuta1979f552015-12-28 21:24:26 -080076import java.util.concurrent.ConcurrentMap;
Jonathan Hart6d44d192015-05-11 18:01:19 -070077import java.util.concurrent.CopyOnWriteArraySet;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080078import java.util.concurrent.ExecutorService;
79import java.util.concurrent.Executors;
80import java.util.concurrent.locks.Lock;
81import java.util.concurrent.locks.ReentrantLock;
Jian Li2266bff2016-04-21 11:01:25 -070082
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080083import static org.onlab.util.Tools.groupedThreads;
tom7ef8ff92014-09-17 13:08:06 -070084
85@Component(immediate = true)
86@Service
87public class OpenFlowControllerImpl implements OpenFlowController {
Charles Chanecfdfb72015-11-24 19:05:50 -080088 private static final String APP_ID = "org.onosproject.openflow-base";
Brian O'Connorff278502015-09-22 14:49:52 -070089 private static final String DEFAULT_OFPORT = "6633,6653";
Charles Chan3b00e1b2015-08-26 23:12:52 +080090 private static final int DEFAULT_WORKER_THREADS = 16;
tom7ef8ff92014-09-17 13:08:06 -070091
92 private static final Logger log =
93 LoggerFactory.getLogger(OpenFlowControllerImpl.class);
94
alshabibb452fd72015-04-22 20:46:20 -070095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanecfdfb72015-11-24 19:05:50 -080096 protected CoreService coreService;
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibb452fd72015-04-22 20:46:20 -070099 protected DriverService driverService;
100
Thomas Vachuska3358af22015-05-19 18:40:34 -0700101 // References exists merely for sequencing purpose to assure drivers are loaded
102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected DefaultDriverProviderService defaultDriverProviderService;
104
Charles Chan3b00e1b2015-08-26 23:12:52 +0800105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected ComponentConfigService cfgService;
107
Brian O'Connore2a399e2015-09-22 15:32:50 -0700108 @Property(name = "openflowPorts", value = DEFAULT_OFPORT,
109 label = "Port numbers (comma separated) used by OpenFlow protocol; default is 6633,6653")
110 private String openflowPorts = DEFAULT_OFPORT;
Charles Chan3b00e1b2015-08-26 23:12:52 +0800111
112 @Property(name = "workerThreads", intValue = DEFAULT_WORKER_THREADS,
113 label = "Number of controller worker threads; default is 16")
114 private int workerThreads = DEFAULT_WORKER_THREADS;
115
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800116 protected ExecutorService executorMsgs =
Andrea Campanelladda93562016-03-02 11:08:12 -0800117 Executors.newFixedThreadPool(32, groupedThreads("onos/of", "event-stats-%d", log));
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800118
119 private final ExecutorService executorBarrier =
Andrea Campanelladda93562016-03-02 11:08:12 -0800120 Executors.newFixedThreadPool(4, groupedThreads("onos/of", "event-barrier-%d", log));
alshabib8f1cf4a2014-09-17 14:44:48 -0700121
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800122 protected ConcurrentMap<Dpid, OpenFlowSwitch> connectedSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700123 new ConcurrentHashMap<>();
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800124 protected ConcurrentMap<Dpid, OpenFlowSwitch> activeMasterSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700125 new ConcurrentHashMap<>();
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800126 protected ConcurrentMap<Dpid, OpenFlowSwitch> activeEqualSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700127 new ConcurrentHashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700128
129 protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent();
Jonathan Hart6d44d192015-05-11 18:01:19 -0700130 protected Set<OpenFlowSwitchListener> ofSwitchListener = new CopyOnWriteArraySet<>();
tom7ef8ff92014-09-17 13:08:06 -0700131
132 protected Multimap<Integer, PacketListener> ofPacketListener =
133 ArrayListMultimap.create();
134
Jonathan Hart6d44d192015-05-11 18:01:19 -0700135 protected Set<OpenFlowEventListener> ofEventListener = new CopyOnWriteArraySet<>();
tom7ef8ff92014-09-17 13:08:06 -0700136
Jian Li28247b52016-01-07 17:24:15 -0800137 protected boolean monitorAllEvents = false;
138
sangho6a0bb172015-02-05 12:24:48 -0800139 protected Multimap<Dpid, OFFlowStatsEntry> fullFlowStats =
140 ArrayListMultimap.create();
141
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700142 protected Multimap<Dpid, OFTableStatsEntry> fullTableStats =
143 ArrayListMultimap.create();
144
sangho6a0bb172015-02-05 12:24:48 -0800145 protected Multimap<Dpid, OFGroupStatsEntry> fullGroupStats =
146 ArrayListMultimap.create();
147
148 protected Multimap<Dpid, OFGroupDescStatsEntry> fullGroupDescStats =
alshabib64def642014-12-02 23:27:37 -0800149 ArrayListMultimap.create();
150
sangho538108b2015-04-08 14:29:20 -0700151 protected Multimap<Dpid, OFPortStatsEntry> fullPortStats =
152 ArrayListMultimap.create();
153
tom7ef8ff92014-09-17 13:08:06 -0700154 private final Controller ctrl = new Controller();
155
156 @Activate
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800157 public void activate(ComponentContext context) {
Charles Chanecfdfb72015-11-24 19:05:50 -0800158 coreService.registerApplication(APP_ID, this::preDeactivate);
Charles Chan3b00e1b2015-08-26 23:12:52 +0800159 cfgService.registerProperties(getClass());
Brian O'Connorff278502015-09-22 14:49:52 -0700160 ctrl.setConfigParams(context.getProperties());
alshabibb452fd72015-04-22 20:46:20 -0700161 ctrl.start(agent, driverService);
tom7ef8ff92014-09-17 13:08:06 -0700162 }
163
Charles Chanecfdfb72015-11-24 19:05:50 -0800164 private void preDeactivate() {
165 // Close listening channel and all OF channels before deactivating
166 ctrl.stop();
167 connectedSwitches.values().forEach(OpenFlowSwitch::disconnectSwitch);
168 }
169
tom7ef8ff92014-09-17 13:08:06 -0700170 @Deactivate
171 public void deactivate() {
Ray Milkeyeba68172016-03-07 11:03:25 -0800172 preDeactivate();
Charles Chan3b00e1b2015-08-26 23:12:52 +0800173 cfgService.unregisterProperties(getClass(), false);
Charles Chanecfdfb72015-11-24 19:05:50 -0800174 connectedSwitches.clear();
175 activeMasterSwitches.clear();
176 activeEqualSwitches.clear();
tom7ef8ff92014-09-17 13:08:06 -0700177 }
178
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800179 @Modified
180 public void modified(ComponentContext context) {
Charles Chan3b00e1b2015-08-26 23:12:52 +0800181 ctrl.stop();
Brian O'Connorff278502015-09-22 14:49:52 -0700182 ctrl.setConfigParams(context.getProperties());
Charles Chan3b00e1b2015-08-26 23:12:52 +0800183 ctrl.start(agent, driverService);
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800184 }
185
tom7ef8ff92014-09-17 13:08:06 -0700186 @Override
187 public Iterable<OpenFlowSwitch> getSwitches() {
188 return connectedSwitches.values();
189 }
190
191 @Override
192 public Iterable<OpenFlowSwitch> getMasterSwitches() {
193 return activeMasterSwitches.values();
194 }
195
196 @Override
197 public Iterable<OpenFlowSwitch> getEqualSwitches() {
198 return activeEqualSwitches.values();
199 }
200
201 @Override
202 public OpenFlowSwitch getSwitch(Dpid dpid) {
203 return connectedSwitches.get(dpid);
204 }
205
206 @Override
207 public OpenFlowSwitch getMasterSwitch(Dpid dpid) {
208 return activeMasterSwitches.get(dpid);
209 }
210
211 @Override
212 public OpenFlowSwitch getEqualSwitch(Dpid dpid) {
213 return activeEqualSwitches.get(dpid);
214 }
215
216 @Override
Jian Li28247b52016-01-07 17:24:15 -0800217 public void monitorAllEvents(boolean monitor) {
218 this.monitorAllEvents = monitor;
219 }
220
221 @Override
tom7ef8ff92014-09-17 13:08:06 -0700222 public void addListener(OpenFlowSwitchListener listener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700223 if (!ofSwitchListener.contains(listener)) {
224 this.ofSwitchListener.add(listener);
tom7ef8ff92014-09-17 13:08:06 -0700225 }
226 }
227
228 @Override
229 public void removeListener(OpenFlowSwitchListener listener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700230 this.ofSwitchListener.remove(listener);
tom7ef8ff92014-09-17 13:08:06 -0700231 }
232
233 @Override
234 public void addPacketListener(int priority, PacketListener listener) {
235 ofPacketListener.put(priority, listener);
236 }
237
238 @Override
239 public void removePacketListener(PacketListener listener) {
240 ofPacketListener.values().remove(listener);
241 }
242
243 @Override
alshabibeec3a062014-09-17 18:01:26 -0700244 public void addEventListener(OpenFlowEventListener listener) {
245 ofEventListener.add(listener);
246 }
247
248 @Override
249 public void removeEventListener(OpenFlowEventListener listener) {
250 ofEventListener.remove(listener);
251 }
252
253 @Override
tom7ef8ff92014-09-17 13:08:06 -0700254 public void write(Dpid dpid, OFMessage msg) {
255 this.getSwitch(dpid).sendMsg(msg);
256 }
257
258 @Override
259 public void processPacket(Dpid dpid, OFMessage msg) {
sangho6a0bb172015-02-05 12:24:48 -0800260 Collection<OFFlowStatsEntry> flowStats;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700261 Collection<OFTableStatsEntry> tableStats;
sangho6a0bb172015-02-05 12:24:48 -0800262 Collection<OFGroupStatsEntry> groupStats;
263 Collection<OFGroupDescStatsEntry> groupDescStats;
sangho538108b2015-04-08 14:29:20 -0700264 Collection<OFPortStatsEntry> portStats;
sangho6a0bb172015-02-05 12:24:48 -0800265
tom7ef8ff92014-09-17 13:08:06 -0700266 switch (msg.getType()) {
267 case PORT_STATUS:
alshabib8f1cf4a2014-09-17 14:44:48 -0700268 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700269 l.portChanged(dpid, (OFPortStatus) msg);
270 }
271 break;
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700272 case FEATURES_REPLY:
273 for (OpenFlowSwitchListener l : ofSwitchListener) {
274 l.switchChanged(dpid);
275 }
276 break;
tom7ef8ff92014-09-17 13:08:06 -0700277 case PACKET_IN:
278 OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext
279 .packetContextFromPacketIn(this.getSwitch(dpid),
280 (OFPacketIn) msg);
281 for (PacketListener p : ofPacketListener.values()) {
282 p.handlePacket(pktCtx);
283 }
284 break;
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800285 // TODO: Consider using separate threadpool for sensitive messages.
286 // ie. Back to back error could cause us to starve.
287 case FLOW_REMOVED:
288 case ERROR:
Andrea Campanelladda93562016-03-02 11:08:12 -0800289 executorMsgs.execute(new OFMessageHandler(dpid, msg));
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800290 break;
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700291 case STATS_REPLY:
292 OFStatsReply reply = (OFStatsReply) msg;
sangho6a0bb172015-02-05 12:24:48 -0800293 switch (reply.getStatsType()) {
294 case PORT_DESC:
295 for (OpenFlowSwitchListener l : ofSwitchListener) {
296 l.switchChanged(dpid);
297 }
298 break;
299 case FLOW:
300 flowStats = publishFlowStats(dpid, (OFFlowStatsReply) reply);
301 if (flowStats != null) {
302 OFFlowStatsReply.Builder rep =
303 OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply();
304 rep.setEntries(Lists.newLinkedList(flowStats));
ssyoon9030fbcd92015-08-17 10:42:07 +0900305 rep.setXid(reply.getXid());
Andrea Campanelladda93562016-03-02 11:08:12 -0800306 executorMsgs.execute(new OFMessageHandler(dpid, rep.build()));
sangho6a0bb172015-02-05 12:24:48 -0800307 }
308 break;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700309 case TABLE:
310 tableStats = publishTableStats(dpid, (OFTableStatsReply) reply);
311 if (tableStats != null) {
312 OFTableStatsReply.Builder rep =
313 OFFactories.getFactory(msg.getVersion()).buildTableStatsReply();
314 rep.setEntries(Lists.newLinkedList(tableStats));
Andrea Campanelladda93562016-03-02 11:08:12 -0800315 executorMsgs.execute(new OFMessageHandler(dpid, rep.build()));
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700316 }
317 break;
sangho6a0bb172015-02-05 12:24:48 -0800318 case GROUP:
319 groupStats = publishGroupStats(dpid, (OFGroupStatsReply) reply);
320 if (groupStats != null) {
321 OFGroupStatsReply.Builder rep =
322 OFFactories.getFactory(msg.getVersion()).buildGroupStatsReply();
323 rep.setEntries(Lists.newLinkedList(groupStats));
324 rep.setXid(reply.getXid());
Andrea Campanelladda93562016-03-02 11:08:12 -0800325 executorMsgs.execute(new OFMessageHandler(dpid, rep.build()));
sangho6a0bb172015-02-05 12:24:48 -0800326 }
327 break;
328 case GROUP_DESC:
329 groupDescStats = publishGroupDescStats(dpid,
330 (OFGroupDescStatsReply) reply);
331 if (groupDescStats != null) {
332 OFGroupDescStatsReply.Builder rep =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700333 OFFactories.getFactory(msg.getVersion()).buildGroupDescStatsReply();
sangho6a0bb172015-02-05 12:24:48 -0800334 rep.setEntries(Lists.newLinkedList(groupDescStats));
335 rep.setXid(reply.getXid());
Andrea Campanelladda93562016-03-02 11:08:12 -0800336 executorMsgs.execute(new OFMessageHandler(dpid, rep.build()));
sangho6a0bb172015-02-05 12:24:48 -0800337 }
338 break;
sangho538108b2015-04-08 14:29:20 -0700339 case PORT:
Andrea Campanelladda93562016-03-02 11:08:12 -0800340 executorMsgs.execute(new OFMessageHandler(dpid, reply));
sangho538108b2015-04-08 14:29:20 -0700341 break;
alshabib5eb79392015-08-19 18:09:55 -0700342 case METER:
Andrea Campanelladda93562016-03-02 11:08:12 -0800343 executorMsgs.execute(new OFMessageHandler(dpid, reply));
alshabib5eb79392015-08-19 18:09:55 -0700344 break;
Marc De Leenheerb9311372015-07-09 11:36:49 -0700345 case EXPERIMENTER:
346 if (reply instanceof OFCalientFlowStatsReply) {
347 // Convert Calient flow statistics to regular flow stats
348 // TODO: parse remaining fields such as power levels etc. when we have proper monitoring API
349 OFFlowStatsReply.Builder fsr = getSwitch(dpid).factory().buildFlowStatsReply();
350 List<OFFlowStatsEntry> entries = new LinkedList<>();
351 for (OFCalientFlowStatsEntry entry : ((OFCalientFlowStatsReply) msg).getEntries()) {
352
353 // Single instruction, i.e., output to port
354 OFActionOutput action = OFFactories
355 .getFactory(msg.getVersion())
356 .actions()
357 .buildOutput()
358 .setPort(entry.getOutPort())
359 .build();
360 OFInstruction instruction = OFFactories
361 .getFactory(msg.getVersion())
362 .instructions()
363 .applyActions(Collections.singletonList(action));
364 OFFlowStatsEntry fs = getSwitch(dpid).factory().buildFlowStatsEntry()
365 .setMatch(entry.getMatch())
366 .setTableId(entry.getTableId())
367 .setDurationSec(entry.getDurationSec())
368 .setDurationNsec(entry.getDurationNsec())
369 .setPriority(entry.getPriority())
370 .setIdleTimeout(entry.getIdleTimeout())
371 .setHardTimeout(entry.getHardTimeout())
372 .setFlags(entry.getFlags())
373 .setCookie(entry.getCookie())
374 .setInstructions(Collections.singletonList(instruction))
375 .build();
376 entries.add(fs);
377 }
378 fsr.setEntries(entries);
379
380 flowStats = publishFlowStats(dpid, fsr.build());
381 if (flowStats != null) {
382 OFFlowStatsReply.Builder rep =
383 OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply();
384 rep.setEntries(Lists.newLinkedList(flowStats));
Andrea Campanelladda93562016-03-02 11:08:12 -0800385 executorMsgs.execute(new OFMessageHandler(dpid, rep.build()));
Marc De Leenheerb9311372015-07-09 11:36:49 -0700386 }
387 } else {
Andrea Campanelladda93562016-03-02 11:08:12 -0800388 executorMsgs.execute(new OFMessageHandler(dpid, reply));
Marc De Leenheerb9311372015-07-09 11:36:49 -0700389 }
390 break;
sangho6a0bb172015-02-05 12:24:48 -0800391 default:
alshabib5eb79392015-08-19 18:09:55 -0700392 log.warn("Discarding unknown stats reply type {}", reply.getStatsType());
Marc De Leenheerb9311372015-07-09 11:36:49 -0700393 break;
alshabib64def642014-12-02 23:27:37 -0800394 }
395 break;
alshabib8f1cf4a2014-09-17 14:44:48 -0700396 case BARRIER_REPLY:
Andrea Campanelladda93562016-03-02 11:08:12 -0800397 executorBarrier.execute(new OFMessageHandler(dpid, msg));
alshabib8f1cf4a2014-09-17 14:44:48 -0700398 break;
Marc De Leenheer631ffce2014-10-28 16:29:07 -0700399 case EXPERIMENTER:
Marc De Leenheerb9311372015-07-09 11:36:49 -0700400 long experimenter = ((OFExperimenter) msg).getExperimenter();
401 if (experimenter == 0x748771) {
402 // LINC-OE port stats
Marc De Leenheer631ffce2014-10-28 16:29:07 -0700403 OFCircuitPortStatus circuitPortStatus = (OFCircuitPortStatus) msg;
404 OFPortStatus.Builder portStatus = this.getSwitch(dpid).factory().buildPortStatus();
405 OFPortDesc.Builder portDesc = this.getSwitch(dpid).factory().buildPortDesc();
406 portDesc.setPortNo(circuitPortStatus.getPortNo())
407 .setHwAddr(circuitPortStatus.getHwAddr())
408 .setName(circuitPortStatus.getName())
409 .setConfig(circuitPortStatus.getConfig())
410 .setState(circuitPortStatus.getState());
411 portStatus.setReason(circuitPortStatus.getReason()).setDesc(portDesc.build());
412 for (OpenFlowSwitchListener l : ofSwitchListener) {
413 l.portChanged(dpid, portStatus.build());
414 }
415 } else {
416 log.warn("Handling experimenter type {} not yet implemented",
417 ((OFExperimenter) msg).getExperimenter(), msg);
418 }
419 break;
tom7ef8ff92014-09-17 13:08:06 -0700420 default:
421 log.warn("Handling message type {} not yet implemented {}",
422 msg.getType(), msg);
423 }
424 }
425
sangho6a0bb172015-02-05 12:24:48 -0800426 private synchronized Collection<OFFlowStatsEntry> publishFlowStats(Dpid dpid,
427 OFFlowStatsReply reply) {
alshabib64def642014-12-02 23:27:37 -0800428 //TODO: Get rid of synchronized
sangho6a0bb172015-02-05 12:24:48 -0800429 fullFlowStats.putAll(dpid, reply.getEntries());
alshabib64def642014-12-02 23:27:37 -0800430 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
sangho6a0bb172015-02-05 12:24:48 -0800431 return fullFlowStats.removeAll(dpid);
432 }
433 return null;
434 }
435
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700436 private synchronized Collection<OFTableStatsEntry> publishTableStats(Dpid dpid,
437 OFTableStatsReply reply) {
438 //TODO: Get rid of synchronized
439 fullTableStats.putAll(dpid, reply.getEntries());
440 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
441 return fullTableStats.removeAll(dpid);
442 }
443 return null;
444 }
445
sangho6a0bb172015-02-05 12:24:48 -0800446 private synchronized Collection<OFGroupStatsEntry> publishGroupStats(Dpid dpid,
447 OFGroupStatsReply reply) {
448 //TODO: Get rid of synchronized
449 fullGroupStats.putAll(dpid, reply.getEntries());
450 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
451 return fullGroupStats.removeAll(dpid);
452 }
453 return null;
454 }
455
456 private synchronized Collection<OFGroupDescStatsEntry> publishGroupDescStats(Dpid dpid,
457 OFGroupDescStatsReply reply) {
458 //TODO: Get rid of synchronized
459 fullGroupDescStats.putAll(dpid, reply.getEntries());
460 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
461 return fullGroupDescStats.removeAll(dpid);
alshabib64def642014-12-02 23:27:37 -0800462 }
463 return null;
464 }
465
sangho538108b2015-04-08 14:29:20 -0700466 private synchronized Collection<OFPortStatsEntry> publishPortStats(Dpid dpid,
467 OFPortStatsReply reply) {
468 fullPortStats.putAll(dpid, reply.getEntries());
469 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
470 return fullPortStats.removeAll(dpid);
471 }
472 return null;
473 }
474
tom7ef8ff92014-09-17 13:08:06 -0700475 @Override
476 public void setRole(Dpid dpid, RoleState role) {
Yuta HIGUCHI79cbd1c2014-10-02 16:57:57 -0700477 final OpenFlowSwitch sw = getSwitch(dpid);
478 if (sw == null) {
479 log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role);
480 return;
481 }
482 sw.setRole(role);
tom7ef8ff92014-09-17 13:08:06 -0700483 }
484
485 /**
486 * Implementation of an OpenFlow Agent which is responsible for
487 * keeping track of connected switches and the state in which
488 * they are.
489 */
490 public class OpenFlowSwitchAgent implements OpenFlowAgent {
491
492 private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class);
493 private final Lock switchLock = new ReentrantLock();
494
495 @Override
496 public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) {
alshabib9eab22f2014-10-20 17:17:31 -0700497
tom7ef8ff92014-09-17 13:08:06 -0700498 if (connectedSwitches.get(dpid) != null) {
499 log.error("Trying to add connectedSwitch but found a previous "
500 + "value for dpid: {}", dpid);
501 return false;
502 } else {
Yuta HIGUCHIeb3f30b2014-10-22 11:34:49 -0700503 log.info("Added switch {}", dpid);
tom7ef8ff92014-09-17 13:08:06 -0700504 connectedSwitches.put(dpid, sw);
alshabib8f1cf4a2014-09-17 14:44:48 -0700505 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700506 l.switchAdded(dpid);
507 }
508 return true;
509 }
510 }
511
512 @Override
513 public boolean validActivation(Dpid dpid) {
514 if (connectedSwitches.get(dpid) == null) {
515 log.error("Trying to activate switch but is not in "
516 + "connected switches: dpid {}. Aborting ..",
517 dpid);
518 return false;
519 }
520 if (activeMasterSwitches.get(dpid) != null ||
521 activeEqualSwitches.get(dpid) != null) {
522 log.error("Trying to activate switch but it is already "
523 + "activated: dpid {}. Found in activeMaster: {} "
Ray Milkey6bc43c22015-11-06 13:22:38 -0800524 + "Found in activeEqual: {}. Aborting ..",
525 dpid,
526 (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y',
527 (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y');
tom7ef8ff92014-09-17 13:08:06 -0700528 return false;
529 }
530 return true;
531 }
532
533
534 @Override
535 public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) {
536 switchLock.lock();
537 try {
538 if (!validActivation(dpid)) {
539 return false;
540 }
541 activeMasterSwitches.put(dpid, sw);
542 return true;
543 } finally {
544 switchLock.unlock();
545 }
546 }
547
548 @Override
549 public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) {
550 switchLock.lock();
551 try {
552 if (!validActivation(dpid)) {
553 return false;
554 }
555 activeEqualSwitches.put(dpid, sw);
556 log.info("Added Activated EQUAL Switch {}", dpid);
557 return true;
558 } finally {
559 switchLock.unlock();
560 }
561 }
562
563 @Override
564 public void transitionToMasterSwitch(Dpid dpid) {
565 switchLock.lock();
566 try {
567 if (activeMasterSwitches.containsKey(dpid)) {
568 return;
569 }
570 OpenFlowSwitch sw = activeEqualSwitches.remove(dpid);
571 if (sw == null) {
572 sw = getSwitch(dpid);
573 if (sw == null) {
574 log.error("Transition to master called on sw {}, but switch "
575 + "was not found in controller-cache", dpid);
576 return;
577 }
578 }
579 log.info("Transitioned switch {} to MASTER", dpid);
580 activeMasterSwitches.put(dpid, sw);
581 } finally {
582 switchLock.unlock();
583 }
584 }
585
586
587 @Override
588 public void transitionToEqualSwitch(Dpid dpid) {
589 switchLock.lock();
590 try {
591 if (activeEqualSwitches.containsKey(dpid)) {
592 return;
593 }
594 OpenFlowSwitch sw = activeMasterSwitches.remove(dpid);
595 if (sw == null) {
596 sw = getSwitch(dpid);
597 if (sw == null) {
598 log.error("Transition to equal called on sw {}, but switch "
599 + "was not found in controller-cache", dpid);
600 return;
601 }
602 }
603 log.info("Transitioned switch {} to EQUAL", dpid);
604 activeEqualSwitches.put(dpid, sw);
605 } finally {
606 switchLock.unlock();
607 }
608
609 }
610
611 @Override
612 public void removeConnectedSwitch(Dpid dpid) {
613 connectedSwitches.remove(dpid);
614 OpenFlowSwitch sw = activeMasterSwitches.remove(dpid);
615 if (sw == null) {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700616 log.debug("sw was null for {}", dpid);
tom7ef8ff92014-09-17 13:08:06 -0700617 sw = activeEqualSwitches.remove(dpid);
618 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700619 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700620 l.switchRemoved(dpid);
621 }
622 }
623
624 @Override
625 public void processMessage(Dpid dpid, OFMessage m) {
626 processPacket(dpid, m);
627 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700628
629 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700630 public void returnRoleReply(Dpid dpid, RoleState requested, RoleState response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700631 for (OpenFlowSwitchListener l : ofSwitchListener) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700632 l.receivedRoleReply(dpid, requested, response);
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700633 }
634 }
tom7ef8ff92014-09-17 13:08:06 -0700635 }
636
Jian Li152b8852015-12-07 14:47:25 -0800637 /**
Jian Li2266bff2016-04-21 11:01:25 -0700638 * OpenFlow message handler.
Jian Li152b8852015-12-07 14:47:25 -0800639 */
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800640 protected final class OFMessageHandler implements Runnable {
alshabib8f1cf4a2014-09-17 14:44:48 -0700641
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800642 protected final OFMessage msg;
643 protected final Dpid dpid;
alshabib8f1cf4a2014-09-17 14:44:48 -0700644
645 public OFMessageHandler(Dpid dpid, OFMessage msg) {
646 this.msg = msg;
647 this.dpid = dpid;
648 }
649
650 @Override
651 public void run() {
alshabibeec3a062014-09-17 18:01:26 -0700652 for (OpenFlowEventListener listener : ofEventListener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700653 listener.handleMessage(dpid, msg);
654 }
655 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700656 }
tom7ef8ff92014-09-17 13:08:06 -0700657}