blob: eb90c265aa4ddc4088b97a4783f19c24cf1c16ae [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -070051import org.projectfloodlight.openflow.protocol.OFTableStatsEntry;
52import org.projectfloodlight.openflow.protocol.OFTableStatsReply;
sangho6a0bb172015-02-05 12:24:48 -080053import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry;
54import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
55import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry;
56import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
tom7ef8ff92014-09-17 13:08:06 -070057import org.projectfloodlight.openflow.protocol.OFMessage;
58import org.projectfloodlight.openflow.protocol.OFPacketIn;
Marc De Leenheer631ffce2014-10-28 16:29:07 -070059import org.projectfloodlight.openflow.protocol.OFPortDesc;
sangho538108b2015-04-08 14:29:20 -070060import org.projectfloodlight.openflow.protocol.OFPortStatsEntry;
61import org.projectfloodlight.openflow.protocol.OFPortStatsReply;
tom7ef8ff92014-09-17 13:08:06 -070062import org.projectfloodlight.openflow.protocol.OFPortStatus;
Ayaka Koshibe38594c22014-10-22 13:36:12 -070063import org.projectfloodlight.openflow.protocol.OFStatsReply;
alshabib64def642014-12-02 23:27:37 -080064import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags;
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;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080082import static org.onlab.util.Tools.groupedThreads;
tom7ef8ff92014-09-17 13:08:06 -070083
84@Component(immediate = true)
85@Service
86public class OpenFlowControllerImpl implements OpenFlowController {
Charles Chanecfdfb72015-11-24 19:05:50 -080087 private static final String APP_ID = "org.onosproject.openflow-base";
Brian O'Connorff278502015-09-22 14:49:52 -070088 private static final String DEFAULT_OFPORT = "6633,6653";
Charles Chan3b00e1b2015-08-26 23:12:52 +080089 private static final int DEFAULT_WORKER_THREADS = 16;
tom7ef8ff92014-09-17 13:08:06 -070090
91 private static final Logger log =
92 LoggerFactory.getLogger(OpenFlowControllerImpl.class);
93
alshabibb452fd72015-04-22 20:46:20 -070094 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanecfdfb72015-11-24 19:05:50 -080095 protected CoreService coreService;
96
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibb452fd72015-04-22 20:46:20 -070098 protected DriverService driverService;
99
Thomas Vachuska3358af22015-05-19 18:40:34 -0700100 // References exists merely for sequencing purpose to assure drivers are loaded
101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected DefaultDriverProviderService defaultDriverProviderService;
103
Charles Chan3b00e1b2015-08-26 23:12:52 +0800104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected ComponentConfigService cfgService;
106
Brian O'Connore2a399e2015-09-22 15:32:50 -0700107 @Property(name = "openflowPorts", value = DEFAULT_OFPORT,
108 label = "Port numbers (comma separated) used by OpenFlow protocol; default is 6633,6653")
109 private String openflowPorts = DEFAULT_OFPORT;
Charles Chan3b00e1b2015-08-26 23:12:52 +0800110
111 @Property(name = "workerThreads", intValue = DEFAULT_WORKER_THREADS,
112 label = "Number of controller worker threads; default is 16")
113 private int workerThreads = DEFAULT_WORKER_THREADS;
114
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800115 protected ExecutorService executorMsgs =
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800116 Executors.newFixedThreadPool(32, groupedThreads("onos/of", "event-stats-%d"));
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800117
Jian Li28247b52016-01-07 17:24:15 -0800118 protected ExecutorService executorPacketIn =
119 Executors.newCachedThreadPool(groupedThreads("onos/of", "event-pkt-in-stats-%d"));
120
121 protected ExecutorService executorFlowRemoved =
122 Executors.newCachedThreadPool(groupedThreads("onos/of", "event-flow-removed-stats-%d"));
123
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800124 private final ExecutorService executorBarrier =
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800125 Executors.newFixedThreadPool(4, groupedThreads("onos/of", "event-barrier-%d"));
alshabib8f1cf4a2014-09-17 14:44:48 -0700126
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800127 protected ConcurrentMap<Dpid, OpenFlowSwitch> connectedSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700128 new ConcurrentHashMap<>();
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800129 protected ConcurrentMap<Dpid, OpenFlowSwitch> activeMasterSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700130 new ConcurrentHashMap<>();
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800131 protected ConcurrentMap<Dpid, OpenFlowSwitch> activeEqualSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700132 new ConcurrentHashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700133
134 protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent();
Jonathan Hart6d44d192015-05-11 18:01:19 -0700135 protected Set<OpenFlowSwitchListener> ofSwitchListener = new CopyOnWriteArraySet<>();
tom7ef8ff92014-09-17 13:08:06 -0700136
137 protected Multimap<Integer, PacketListener> ofPacketListener =
138 ArrayListMultimap.create();
139
Jonathan Hart6d44d192015-05-11 18:01:19 -0700140 protected Set<OpenFlowEventListener> ofEventListener = new CopyOnWriteArraySet<>();
tom7ef8ff92014-09-17 13:08:06 -0700141
Jian Li28247b52016-01-07 17:24:15 -0800142 protected boolean monitorAllEvents = false;
143
sangho6a0bb172015-02-05 12:24:48 -0800144 protected Multimap<Dpid, OFFlowStatsEntry> fullFlowStats =
145 ArrayListMultimap.create();
146
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700147 protected Multimap<Dpid, OFTableStatsEntry> fullTableStats =
148 ArrayListMultimap.create();
149
sangho6a0bb172015-02-05 12:24:48 -0800150 protected Multimap<Dpid, OFGroupStatsEntry> fullGroupStats =
151 ArrayListMultimap.create();
152
153 protected Multimap<Dpid, OFGroupDescStatsEntry> fullGroupDescStats =
alshabib64def642014-12-02 23:27:37 -0800154 ArrayListMultimap.create();
155
sangho538108b2015-04-08 14:29:20 -0700156 protected Multimap<Dpid, OFPortStatsEntry> fullPortStats =
157 ArrayListMultimap.create();
158
tom7ef8ff92014-09-17 13:08:06 -0700159 private final Controller ctrl = new Controller();
160
161 @Activate
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800162 public void activate(ComponentContext context) {
Charles Chanecfdfb72015-11-24 19:05:50 -0800163 coreService.registerApplication(APP_ID, this::preDeactivate);
Charles Chan3b00e1b2015-08-26 23:12:52 +0800164 cfgService.registerProperties(getClass());
Brian O'Connorff278502015-09-22 14:49:52 -0700165 ctrl.setConfigParams(context.getProperties());
alshabibb452fd72015-04-22 20:46:20 -0700166 ctrl.start(agent, driverService);
tom7ef8ff92014-09-17 13:08:06 -0700167 }
168
Charles Chanecfdfb72015-11-24 19:05:50 -0800169 private void preDeactivate() {
170 // Close listening channel and all OF channels before deactivating
171 ctrl.stop();
172 connectedSwitches.values().forEach(OpenFlowSwitch::disconnectSwitch);
173 }
174
tom7ef8ff92014-09-17 13:08:06 -0700175 @Deactivate
176 public void deactivate() {
Charles Chan3b00e1b2015-08-26 23:12:52 +0800177 cfgService.unregisterProperties(getClass(), false);
Charles Chanecfdfb72015-11-24 19:05:50 -0800178 connectedSwitches.clear();
179 activeMasterSwitches.clear();
180 activeEqualSwitches.clear();
tom7ef8ff92014-09-17 13:08:06 -0700181 }
182
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800183 @Modified
184 public void modified(ComponentContext context) {
Charles Chan3b00e1b2015-08-26 23:12:52 +0800185 ctrl.stop();
Brian O'Connorff278502015-09-22 14:49:52 -0700186 ctrl.setConfigParams(context.getProperties());
Charles Chan3b00e1b2015-08-26 23:12:52 +0800187 ctrl.start(agent, driverService);
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800188 }
189
tom7ef8ff92014-09-17 13:08:06 -0700190 @Override
191 public Iterable<OpenFlowSwitch> getSwitches() {
192 return connectedSwitches.values();
193 }
194
195 @Override
196 public Iterable<OpenFlowSwitch> getMasterSwitches() {
197 return activeMasterSwitches.values();
198 }
199
200 @Override
201 public Iterable<OpenFlowSwitch> getEqualSwitches() {
202 return activeEqualSwitches.values();
203 }
204
205 @Override
206 public OpenFlowSwitch getSwitch(Dpid dpid) {
207 return connectedSwitches.get(dpid);
208 }
209
210 @Override
211 public OpenFlowSwitch getMasterSwitch(Dpid dpid) {
212 return activeMasterSwitches.get(dpid);
213 }
214
215 @Override
216 public OpenFlowSwitch getEqualSwitch(Dpid dpid) {
217 return activeEqualSwitches.get(dpid);
218 }
219
220 @Override
Jian Li28247b52016-01-07 17:24:15 -0800221 public void monitorAllEvents(boolean monitor) {
222 this.monitorAllEvents = monitor;
223 }
224
225 @Override
tom7ef8ff92014-09-17 13:08:06 -0700226 public void addListener(OpenFlowSwitchListener listener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700227 if (!ofSwitchListener.contains(listener)) {
228 this.ofSwitchListener.add(listener);
tom7ef8ff92014-09-17 13:08:06 -0700229 }
230 }
231
232 @Override
233 public void removeListener(OpenFlowSwitchListener listener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700234 this.ofSwitchListener.remove(listener);
tom7ef8ff92014-09-17 13:08:06 -0700235 }
236
237 @Override
238 public void addPacketListener(int priority, PacketListener listener) {
239 ofPacketListener.put(priority, listener);
240 }
241
242 @Override
243 public void removePacketListener(PacketListener listener) {
244 ofPacketListener.values().remove(listener);
245 }
246
247 @Override
alshabibeec3a062014-09-17 18:01:26 -0700248 public void addEventListener(OpenFlowEventListener listener) {
249 ofEventListener.add(listener);
250 }
251
252 @Override
253 public void removeEventListener(OpenFlowEventListener listener) {
254 ofEventListener.remove(listener);
255 }
256
257 @Override
tom7ef8ff92014-09-17 13:08:06 -0700258 public void write(Dpid dpid, OFMessage msg) {
259 this.getSwitch(dpid).sendMsg(msg);
260 }
261
262 @Override
263 public void processPacket(Dpid dpid, OFMessage msg) {
sangho6a0bb172015-02-05 12:24:48 -0800264 Collection<OFFlowStatsEntry> flowStats;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700265 Collection<OFTableStatsEntry> tableStats;
sangho6a0bb172015-02-05 12:24:48 -0800266 Collection<OFGroupStatsEntry> groupStats;
267 Collection<OFGroupDescStatsEntry> groupDescStats;
sangho538108b2015-04-08 14:29:20 -0700268 Collection<OFPortStatsEntry> portStats;
sangho6a0bb172015-02-05 12:24:48 -0800269
tom7ef8ff92014-09-17 13:08:06 -0700270 switch (msg.getType()) {
271 case PORT_STATUS:
alshabib8f1cf4a2014-09-17 14:44:48 -0700272 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700273 l.portChanged(dpid, (OFPortStatus) msg);
274 }
275 break;
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700276 case FEATURES_REPLY:
277 for (OpenFlowSwitchListener l : ofSwitchListener) {
278 l.switchChanged(dpid);
279 }
280 break;
tom7ef8ff92014-09-17 13:08:06 -0700281 case PACKET_IN:
282 OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext
283 .packetContextFromPacketIn(this.getSwitch(dpid),
284 (OFPacketIn) msg);
285 for (PacketListener p : ofPacketListener.values()) {
286 p.handlePacket(pktCtx);
287 }
Jian Li28247b52016-01-07 17:24:15 -0800288 if (monitorAllEvents) {
289 executorPacketIn.submit(new OFMessageHandler(dpid, msg));
290 }
tom7ef8ff92014-09-17 13:08:06 -0700291 break;
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800292 // TODO: Consider using separate threadpool for sensitive messages.
293 // ie. Back to back error could cause us to starve.
294 case FLOW_REMOVED:
Jian Li28247b52016-01-07 17:24:15 -0800295 if (monitorAllEvents) {
296 executorFlowRemoved.submit(new OFMessageHandler(dpid, msg));
297 break;
298 }
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800299 case ERROR:
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800300 log.debug("Received error message from {}: {}", dpid, msg);
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800301 executorMsgs.submit(new OFMessageHandler(dpid, msg));
302 break;
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700303 case STATS_REPLY:
304 OFStatsReply reply = (OFStatsReply) msg;
sangho6a0bb172015-02-05 12:24:48 -0800305 switch (reply.getStatsType()) {
306 case PORT_DESC:
307 for (OpenFlowSwitchListener l : ofSwitchListener) {
308 l.switchChanged(dpid);
309 }
310 break;
311 case FLOW:
312 flowStats = publishFlowStats(dpid, (OFFlowStatsReply) reply);
313 if (flowStats != null) {
314 OFFlowStatsReply.Builder rep =
315 OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply();
316 rep.setEntries(Lists.newLinkedList(flowStats));
ssyoon9030fbcd92015-08-17 10:42:07 +0900317 rep.setXid(reply.getXid());
sangho6a0bb172015-02-05 12:24:48 -0800318 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
319 }
320 break;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700321 case TABLE:
322 tableStats = publishTableStats(dpid, (OFTableStatsReply) reply);
323 if (tableStats != null) {
324 OFTableStatsReply.Builder rep =
325 OFFactories.getFactory(msg.getVersion()).buildTableStatsReply();
326 rep.setEntries(Lists.newLinkedList(tableStats));
327 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
328 }
329 break;
sangho6a0bb172015-02-05 12:24:48 -0800330 case GROUP:
331 groupStats = publishGroupStats(dpid, (OFGroupStatsReply) reply);
332 if (groupStats != null) {
333 OFGroupStatsReply.Builder rep =
334 OFFactories.getFactory(msg.getVersion()).buildGroupStatsReply();
335 rep.setEntries(Lists.newLinkedList(groupStats));
336 rep.setXid(reply.getXid());
337 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
338 }
339 break;
340 case GROUP_DESC:
341 groupDescStats = publishGroupDescStats(dpid,
342 (OFGroupDescStatsReply) reply);
343 if (groupDescStats != null) {
344 OFGroupDescStatsReply.Builder rep =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700345 OFFactories.getFactory(msg.getVersion()).buildGroupDescStatsReply();
sangho6a0bb172015-02-05 12:24:48 -0800346 rep.setEntries(Lists.newLinkedList(groupDescStats));
347 rep.setXid(reply.getXid());
348 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
349 }
350 break;
sangho538108b2015-04-08 14:29:20 -0700351 case PORT:
352 executorMsgs.submit(new OFMessageHandler(dpid, reply));
353 break;
alshabib5eb79392015-08-19 18:09:55 -0700354 case METER:
355 executorMsgs.submit(new OFMessageHandler(dpid, reply));
356 break;
Marc De Leenheerb9311372015-07-09 11:36:49 -0700357 case EXPERIMENTER:
358 if (reply instanceof OFCalientFlowStatsReply) {
359 // Convert Calient flow statistics to regular flow stats
360 // TODO: parse remaining fields such as power levels etc. when we have proper monitoring API
361 OFFlowStatsReply.Builder fsr = getSwitch(dpid).factory().buildFlowStatsReply();
362 List<OFFlowStatsEntry> entries = new LinkedList<>();
363 for (OFCalientFlowStatsEntry entry : ((OFCalientFlowStatsReply) msg).getEntries()) {
364
365 // Single instruction, i.e., output to port
366 OFActionOutput action = OFFactories
367 .getFactory(msg.getVersion())
368 .actions()
369 .buildOutput()
370 .setPort(entry.getOutPort())
371 .build();
372 OFInstruction instruction = OFFactories
373 .getFactory(msg.getVersion())
374 .instructions()
375 .applyActions(Collections.singletonList(action));
376 OFFlowStatsEntry fs = getSwitch(dpid).factory().buildFlowStatsEntry()
377 .setMatch(entry.getMatch())
378 .setTableId(entry.getTableId())
379 .setDurationSec(entry.getDurationSec())
380 .setDurationNsec(entry.getDurationNsec())
381 .setPriority(entry.getPriority())
382 .setIdleTimeout(entry.getIdleTimeout())
383 .setHardTimeout(entry.getHardTimeout())
384 .setFlags(entry.getFlags())
385 .setCookie(entry.getCookie())
386 .setInstructions(Collections.singletonList(instruction))
387 .build();
388 entries.add(fs);
389 }
390 fsr.setEntries(entries);
391
392 flowStats = publishFlowStats(dpid, fsr.build());
393 if (flowStats != null) {
394 OFFlowStatsReply.Builder rep =
395 OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply();
396 rep.setEntries(Lists.newLinkedList(flowStats));
397 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
398 }
399 } else {
Michael Bunimovich1b45e1f2015-08-27 07:49:25 -0400400 executorMsgs.submit(new OFMessageHandler(dpid, reply));
Marc De Leenheerb9311372015-07-09 11:36:49 -0700401 }
402 break;
sangho6a0bb172015-02-05 12:24:48 -0800403 default:
alshabib5eb79392015-08-19 18:09:55 -0700404 log.warn("Discarding unknown stats reply type {}", reply.getStatsType());
Marc De Leenheerb9311372015-07-09 11:36:49 -0700405 break;
alshabib64def642014-12-02 23:27:37 -0800406 }
407 break;
alshabib8f1cf4a2014-09-17 14:44:48 -0700408 case BARRIER_REPLY:
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800409 executorBarrier.submit(new OFMessageHandler(dpid, msg));
alshabib8f1cf4a2014-09-17 14:44:48 -0700410 break;
Marc De Leenheer631ffce2014-10-28 16:29:07 -0700411 case EXPERIMENTER:
Marc De Leenheerb9311372015-07-09 11:36:49 -0700412 long experimenter = ((OFExperimenter) msg).getExperimenter();
413 if (experimenter == 0x748771) {
414 // LINC-OE port stats
Marc De Leenheer631ffce2014-10-28 16:29:07 -0700415 OFCircuitPortStatus circuitPortStatus = (OFCircuitPortStatus) msg;
416 OFPortStatus.Builder portStatus = this.getSwitch(dpid).factory().buildPortStatus();
417 OFPortDesc.Builder portDesc = this.getSwitch(dpid).factory().buildPortDesc();
418 portDesc.setPortNo(circuitPortStatus.getPortNo())
419 .setHwAddr(circuitPortStatus.getHwAddr())
420 .setName(circuitPortStatus.getName())
421 .setConfig(circuitPortStatus.getConfig())
422 .setState(circuitPortStatus.getState());
423 portStatus.setReason(circuitPortStatus.getReason()).setDesc(portDesc.build());
424 for (OpenFlowSwitchListener l : ofSwitchListener) {
425 l.portChanged(dpid, portStatus.build());
426 }
427 } else {
428 log.warn("Handling experimenter type {} not yet implemented",
429 ((OFExperimenter) msg).getExperimenter(), msg);
430 }
431 break;
tom7ef8ff92014-09-17 13:08:06 -0700432 default:
433 log.warn("Handling message type {} not yet implemented {}",
434 msg.getType(), msg);
435 }
436 }
437
sangho6a0bb172015-02-05 12:24:48 -0800438 private synchronized Collection<OFFlowStatsEntry> publishFlowStats(Dpid dpid,
439 OFFlowStatsReply reply) {
alshabib64def642014-12-02 23:27:37 -0800440 //TODO: Get rid of synchronized
sangho6a0bb172015-02-05 12:24:48 -0800441 fullFlowStats.putAll(dpid, reply.getEntries());
alshabib64def642014-12-02 23:27:37 -0800442 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
sangho6a0bb172015-02-05 12:24:48 -0800443 return fullFlowStats.removeAll(dpid);
444 }
445 return null;
446 }
447
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700448 private synchronized Collection<OFTableStatsEntry> publishTableStats(Dpid dpid,
449 OFTableStatsReply reply) {
450 //TODO: Get rid of synchronized
451 fullTableStats.putAll(dpid, reply.getEntries());
452 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
453 return fullTableStats.removeAll(dpid);
454 }
455 return null;
456 }
457
sangho6a0bb172015-02-05 12:24:48 -0800458 private synchronized Collection<OFGroupStatsEntry> publishGroupStats(Dpid dpid,
459 OFGroupStatsReply reply) {
460 //TODO: Get rid of synchronized
461 fullGroupStats.putAll(dpid, reply.getEntries());
462 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
463 return fullGroupStats.removeAll(dpid);
464 }
465 return null;
466 }
467
468 private synchronized Collection<OFGroupDescStatsEntry> publishGroupDescStats(Dpid dpid,
469 OFGroupDescStatsReply reply) {
470 //TODO: Get rid of synchronized
471 fullGroupDescStats.putAll(dpid, reply.getEntries());
472 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
473 return fullGroupDescStats.removeAll(dpid);
alshabib64def642014-12-02 23:27:37 -0800474 }
475 return null;
476 }
477
sangho538108b2015-04-08 14:29:20 -0700478 private synchronized Collection<OFPortStatsEntry> publishPortStats(Dpid dpid,
479 OFPortStatsReply reply) {
480 fullPortStats.putAll(dpid, reply.getEntries());
481 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
482 return fullPortStats.removeAll(dpid);
483 }
484 return null;
485 }
486
tom7ef8ff92014-09-17 13:08:06 -0700487 @Override
488 public void setRole(Dpid dpid, RoleState role) {
Yuta HIGUCHI79cbd1c2014-10-02 16:57:57 -0700489 final OpenFlowSwitch sw = getSwitch(dpid);
490 if (sw == null) {
491 log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role);
492 return;
493 }
494 sw.setRole(role);
tom7ef8ff92014-09-17 13:08:06 -0700495 }
496
497 /**
498 * Implementation of an OpenFlow Agent which is responsible for
499 * keeping track of connected switches and the state in which
500 * they are.
501 */
502 public class OpenFlowSwitchAgent implements OpenFlowAgent {
503
504 private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class);
505 private final Lock switchLock = new ReentrantLock();
506
507 @Override
508 public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) {
alshabib9eab22f2014-10-20 17:17:31 -0700509
tom7ef8ff92014-09-17 13:08:06 -0700510 if (connectedSwitches.get(dpid) != null) {
511 log.error("Trying to add connectedSwitch but found a previous "
512 + "value for dpid: {}", dpid);
513 return false;
514 } else {
Yuta HIGUCHIeb3f30b2014-10-22 11:34:49 -0700515 log.info("Added switch {}", dpid);
tom7ef8ff92014-09-17 13:08:06 -0700516 connectedSwitches.put(dpid, sw);
alshabib8f1cf4a2014-09-17 14:44:48 -0700517 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700518 l.switchAdded(dpid);
519 }
520 return true;
521 }
522 }
523
524 @Override
525 public boolean validActivation(Dpid dpid) {
526 if (connectedSwitches.get(dpid) == null) {
527 log.error("Trying to activate switch but is not in "
528 + "connected switches: dpid {}. Aborting ..",
529 dpid);
530 return false;
531 }
532 if (activeMasterSwitches.get(dpid) != null ||
533 activeEqualSwitches.get(dpid) != null) {
534 log.error("Trying to activate switch but it is already "
535 + "activated: dpid {}. Found in activeMaster: {} "
Ray Milkey6bc43c22015-11-06 13:22:38 -0800536 + "Found in activeEqual: {}. Aborting ..",
537 dpid,
538 (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y',
539 (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y');
tom7ef8ff92014-09-17 13:08:06 -0700540 return false;
541 }
542 return true;
543 }
544
545
546 @Override
547 public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) {
548 switchLock.lock();
549 try {
550 if (!validActivation(dpid)) {
551 return false;
552 }
553 activeMasterSwitches.put(dpid, sw);
554 return true;
555 } finally {
556 switchLock.unlock();
557 }
558 }
559
560 @Override
561 public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) {
562 switchLock.lock();
563 try {
564 if (!validActivation(dpid)) {
565 return false;
566 }
567 activeEqualSwitches.put(dpid, sw);
568 log.info("Added Activated EQUAL Switch {}", dpid);
569 return true;
570 } finally {
571 switchLock.unlock();
572 }
573 }
574
575 @Override
576 public void transitionToMasterSwitch(Dpid dpid) {
577 switchLock.lock();
578 try {
579 if (activeMasterSwitches.containsKey(dpid)) {
580 return;
581 }
582 OpenFlowSwitch sw = activeEqualSwitches.remove(dpid);
583 if (sw == null) {
584 sw = getSwitch(dpid);
585 if (sw == null) {
586 log.error("Transition to master called on sw {}, but switch "
587 + "was not found in controller-cache", dpid);
588 return;
589 }
590 }
591 log.info("Transitioned switch {} to MASTER", dpid);
592 activeMasterSwitches.put(dpid, sw);
593 } finally {
594 switchLock.unlock();
595 }
596 }
597
598
599 @Override
600 public void transitionToEqualSwitch(Dpid dpid) {
601 switchLock.lock();
602 try {
603 if (activeEqualSwitches.containsKey(dpid)) {
604 return;
605 }
606 OpenFlowSwitch sw = activeMasterSwitches.remove(dpid);
607 if (sw == null) {
608 sw = getSwitch(dpid);
609 if (sw == null) {
610 log.error("Transition to equal called on sw {}, but switch "
611 + "was not found in controller-cache", dpid);
612 return;
613 }
614 }
615 log.info("Transitioned switch {} to EQUAL", dpid);
616 activeEqualSwitches.put(dpid, sw);
617 } finally {
618 switchLock.unlock();
619 }
620
621 }
622
623 @Override
624 public void removeConnectedSwitch(Dpid dpid) {
625 connectedSwitches.remove(dpid);
626 OpenFlowSwitch sw = activeMasterSwitches.remove(dpid);
627 if (sw == null) {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700628 log.debug("sw was null for {}", dpid);
tom7ef8ff92014-09-17 13:08:06 -0700629 sw = activeEqualSwitches.remove(dpid);
630 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700631 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700632 l.switchRemoved(dpid);
633 }
634 }
635
636 @Override
637 public void processMessage(Dpid dpid, OFMessage m) {
638 processPacket(dpid, m);
639 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700640
641 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700642 public void returnRoleReply(Dpid dpid, RoleState requested, RoleState response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700643 for (OpenFlowSwitchListener l : ofSwitchListener) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700644 l.receivedRoleReply(dpid, requested, response);
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700645 }
646 }
tom7ef8ff92014-09-17 13:08:06 -0700647 }
648
Jian Li152b8852015-12-07 14:47:25 -0800649 /**
650 * OpenFlow message handler for incoming control messages.
651 */
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800652 protected final class OFMessageHandler implements Runnable {
alshabib8f1cf4a2014-09-17 14:44:48 -0700653
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800654 protected final OFMessage msg;
655 protected final Dpid dpid;
alshabib8f1cf4a2014-09-17 14:44:48 -0700656
657 public OFMessageHandler(Dpid dpid, OFMessage msg) {
658 this.msg = msg;
659 this.dpid = dpid;
660 }
661
662 @Override
663 public void run() {
alshabibeec3a062014-09-17 18:01:26 -0700664 for (OpenFlowEventListener listener : ofEventListener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700665 listener.handleMessage(dpid, msg);
666 }
667 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700668 }
tom7ef8ff92014-09-17 13:08:06 -0700669}