blob: 5bca54efac15f59ee6289994279e6f748e8a64e2 [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
118 private final ExecutorService executorBarrier =
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800119 Executors.newFixedThreadPool(4, groupedThreads("onos/of", "event-barrier-%d"));
alshabib8f1cf4a2014-09-17 14:44:48 -0700120
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800121 protected ConcurrentMap<Dpid, OpenFlowSwitch> connectedSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700122 new ConcurrentHashMap<>();
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800123 protected ConcurrentMap<Dpid, OpenFlowSwitch> activeMasterSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700124 new ConcurrentHashMap<>();
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800125 protected ConcurrentMap<Dpid, OpenFlowSwitch> activeEqualSwitches =
Jonathan Hart6d44d192015-05-11 18:01:19 -0700126 new ConcurrentHashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700127
128 protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent();
Jonathan Hart6d44d192015-05-11 18:01:19 -0700129 protected Set<OpenFlowSwitchListener> ofSwitchListener = new CopyOnWriteArraySet<>();
tom7ef8ff92014-09-17 13:08:06 -0700130
131 protected Multimap<Integer, PacketListener> ofPacketListener =
132 ArrayListMultimap.create();
133
Jonathan Hart6d44d192015-05-11 18:01:19 -0700134 protected Set<OpenFlowEventListener> ofEventListener = new CopyOnWriteArraySet<>();
tom7ef8ff92014-09-17 13:08:06 -0700135
sangho6a0bb172015-02-05 12:24:48 -0800136 protected Multimap<Dpid, OFFlowStatsEntry> fullFlowStats =
137 ArrayListMultimap.create();
138
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700139 protected Multimap<Dpid, OFTableStatsEntry> fullTableStats =
140 ArrayListMultimap.create();
141
sangho6a0bb172015-02-05 12:24:48 -0800142 protected Multimap<Dpid, OFGroupStatsEntry> fullGroupStats =
143 ArrayListMultimap.create();
144
145 protected Multimap<Dpid, OFGroupDescStatsEntry> fullGroupDescStats =
alshabib64def642014-12-02 23:27:37 -0800146 ArrayListMultimap.create();
147
sangho538108b2015-04-08 14:29:20 -0700148 protected Multimap<Dpid, OFPortStatsEntry> fullPortStats =
149 ArrayListMultimap.create();
150
tom7ef8ff92014-09-17 13:08:06 -0700151 private final Controller ctrl = new Controller();
152
153 @Activate
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800154 public void activate(ComponentContext context) {
Charles Chanecfdfb72015-11-24 19:05:50 -0800155 coreService.registerApplication(APP_ID, this::preDeactivate);
Charles Chan3b00e1b2015-08-26 23:12:52 +0800156 cfgService.registerProperties(getClass());
Brian O'Connorff278502015-09-22 14:49:52 -0700157 ctrl.setConfigParams(context.getProperties());
alshabibb452fd72015-04-22 20:46:20 -0700158 ctrl.start(agent, driverService);
tom7ef8ff92014-09-17 13:08:06 -0700159 }
160
Charles Chanecfdfb72015-11-24 19:05:50 -0800161 private void preDeactivate() {
162 // Close listening channel and all OF channels before deactivating
163 ctrl.stop();
164 connectedSwitches.values().forEach(OpenFlowSwitch::disconnectSwitch);
165 }
166
tom7ef8ff92014-09-17 13:08:06 -0700167 @Deactivate
168 public void deactivate() {
Charles Chan3b00e1b2015-08-26 23:12:52 +0800169 cfgService.unregisterProperties(getClass(), false);
Charles Chanecfdfb72015-11-24 19:05:50 -0800170 connectedSwitches.clear();
171 activeMasterSwitches.clear();
172 activeEqualSwitches.clear();
tom7ef8ff92014-09-17 13:08:06 -0700173 }
174
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800175 @Modified
176 public void modified(ComponentContext context) {
Charles Chan3b00e1b2015-08-26 23:12:52 +0800177 ctrl.stop();
Brian O'Connorff278502015-09-22 14:49:52 -0700178 ctrl.setConfigParams(context.getProperties());
Charles Chan3b00e1b2015-08-26 23:12:52 +0800179 ctrl.start(agent, driverService);
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800180 }
181
tom7ef8ff92014-09-17 13:08:06 -0700182 @Override
183 public Iterable<OpenFlowSwitch> getSwitches() {
184 return connectedSwitches.values();
185 }
186
187 @Override
188 public Iterable<OpenFlowSwitch> getMasterSwitches() {
189 return activeMasterSwitches.values();
190 }
191
192 @Override
193 public Iterable<OpenFlowSwitch> getEqualSwitches() {
194 return activeEqualSwitches.values();
195 }
196
197 @Override
198 public OpenFlowSwitch getSwitch(Dpid dpid) {
199 return connectedSwitches.get(dpid);
200 }
201
202 @Override
203 public OpenFlowSwitch getMasterSwitch(Dpid dpid) {
204 return activeMasterSwitches.get(dpid);
205 }
206
207 @Override
208 public OpenFlowSwitch getEqualSwitch(Dpid dpid) {
209 return activeEqualSwitches.get(dpid);
210 }
211
212 @Override
213 public void addListener(OpenFlowSwitchListener listener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700214 if (!ofSwitchListener.contains(listener)) {
215 this.ofSwitchListener.add(listener);
tom7ef8ff92014-09-17 13:08:06 -0700216 }
217 }
218
219 @Override
220 public void removeListener(OpenFlowSwitchListener listener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700221 this.ofSwitchListener.remove(listener);
tom7ef8ff92014-09-17 13:08:06 -0700222 }
223
224 @Override
225 public void addPacketListener(int priority, PacketListener listener) {
226 ofPacketListener.put(priority, listener);
227 }
228
229 @Override
230 public void removePacketListener(PacketListener listener) {
231 ofPacketListener.values().remove(listener);
232 }
233
234 @Override
alshabibeec3a062014-09-17 18:01:26 -0700235 public void addEventListener(OpenFlowEventListener listener) {
236 ofEventListener.add(listener);
237 }
238
239 @Override
240 public void removeEventListener(OpenFlowEventListener listener) {
241 ofEventListener.remove(listener);
242 }
243
244 @Override
tom7ef8ff92014-09-17 13:08:06 -0700245 public void write(Dpid dpid, OFMessage msg) {
246 this.getSwitch(dpid).sendMsg(msg);
247 }
248
249 @Override
250 public void processPacket(Dpid dpid, OFMessage msg) {
sangho6a0bb172015-02-05 12:24:48 -0800251 Collection<OFFlowStatsEntry> flowStats;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700252 Collection<OFTableStatsEntry> tableStats;
sangho6a0bb172015-02-05 12:24:48 -0800253 Collection<OFGroupStatsEntry> groupStats;
254 Collection<OFGroupDescStatsEntry> groupDescStats;
sangho538108b2015-04-08 14:29:20 -0700255 Collection<OFPortStatsEntry> portStats;
sangho6a0bb172015-02-05 12:24:48 -0800256
tom7ef8ff92014-09-17 13:08:06 -0700257 switch (msg.getType()) {
258 case PORT_STATUS:
alshabib8f1cf4a2014-09-17 14:44:48 -0700259 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700260 l.portChanged(dpid, (OFPortStatus) msg);
261 }
262 break;
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700263 case FEATURES_REPLY:
264 for (OpenFlowSwitchListener l : ofSwitchListener) {
265 l.switchChanged(dpid);
266 }
267 break;
tom7ef8ff92014-09-17 13:08:06 -0700268 case PACKET_IN:
269 OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext
270 .packetContextFromPacketIn(this.getSwitch(dpid),
271 (OFPacketIn) msg);
272 for (PacketListener p : ofPacketListener.values()) {
273 p.handlePacket(pktCtx);
274 }
Jian Li152b8852015-12-07 14:47:25 -0800275 executorMsgs.submit(new OFMessageHandler(dpid, msg));
tom7ef8ff92014-09-17 13:08:06 -0700276 break;
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800277 // TODO: Consider using separate threadpool for sensitive messages.
278 // ie. Back to back error could cause us to starve.
279 case FLOW_REMOVED:
Jian Li152b8852015-12-07 14:47:25 -0800280 executorMsgs.submit(new OFMessageHandler(dpid, msg));
281 break;
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800282 case ERROR:
HIGUCHI Yuta1979f552015-12-28 21:24:26 -0800283 log.debug("Received error message from {}: {}", dpid, msg);
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800284 executorMsgs.submit(new OFMessageHandler(dpid, msg));
285 break;
Ayaka Koshibe38594c22014-10-22 13:36:12 -0700286 case STATS_REPLY:
287 OFStatsReply reply = (OFStatsReply) msg;
sangho6a0bb172015-02-05 12:24:48 -0800288 switch (reply.getStatsType()) {
289 case PORT_DESC:
290 for (OpenFlowSwitchListener l : ofSwitchListener) {
291 l.switchChanged(dpid);
292 }
293 break;
294 case FLOW:
295 flowStats = publishFlowStats(dpid, (OFFlowStatsReply) reply);
296 if (flowStats != null) {
297 OFFlowStatsReply.Builder rep =
298 OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply();
299 rep.setEntries(Lists.newLinkedList(flowStats));
ssyoon9030fbcd92015-08-17 10:42:07 +0900300 rep.setXid(reply.getXid());
sangho6a0bb172015-02-05 12:24:48 -0800301 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
302 }
303 break;
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700304 case TABLE:
305 tableStats = publishTableStats(dpid, (OFTableStatsReply) reply);
306 if (tableStats != null) {
307 OFTableStatsReply.Builder rep =
308 OFFactories.getFactory(msg.getVersion()).buildTableStatsReply();
309 rep.setEntries(Lists.newLinkedList(tableStats));
310 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
311 }
312 break;
sangho6a0bb172015-02-05 12:24:48 -0800313 case GROUP:
314 groupStats = publishGroupStats(dpid, (OFGroupStatsReply) reply);
315 if (groupStats != null) {
316 OFGroupStatsReply.Builder rep =
317 OFFactories.getFactory(msg.getVersion()).buildGroupStatsReply();
318 rep.setEntries(Lists.newLinkedList(groupStats));
319 rep.setXid(reply.getXid());
320 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
321 }
322 break;
323 case GROUP_DESC:
324 groupDescStats = publishGroupDescStats(dpid,
325 (OFGroupDescStatsReply) reply);
326 if (groupDescStats != null) {
327 OFGroupDescStatsReply.Builder rep =
Marc De Leenheerb9311372015-07-09 11:36:49 -0700328 OFFactories.getFactory(msg.getVersion()).buildGroupDescStatsReply();
sangho6a0bb172015-02-05 12:24:48 -0800329 rep.setEntries(Lists.newLinkedList(groupDescStats));
330 rep.setXid(reply.getXid());
331 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
332 }
333 break;
sangho538108b2015-04-08 14:29:20 -0700334 case PORT:
335 executorMsgs.submit(new OFMessageHandler(dpid, reply));
336 break;
alshabib5eb79392015-08-19 18:09:55 -0700337 case METER:
338 executorMsgs.submit(new OFMessageHandler(dpid, reply));
339 break;
Marc De Leenheerb9311372015-07-09 11:36:49 -0700340 case EXPERIMENTER:
341 if (reply instanceof OFCalientFlowStatsReply) {
342 // Convert Calient flow statistics to regular flow stats
343 // TODO: parse remaining fields such as power levels etc. when we have proper monitoring API
344 OFFlowStatsReply.Builder fsr = getSwitch(dpid).factory().buildFlowStatsReply();
345 List<OFFlowStatsEntry> entries = new LinkedList<>();
346 for (OFCalientFlowStatsEntry entry : ((OFCalientFlowStatsReply) msg).getEntries()) {
347
348 // Single instruction, i.e., output to port
349 OFActionOutput action = OFFactories
350 .getFactory(msg.getVersion())
351 .actions()
352 .buildOutput()
353 .setPort(entry.getOutPort())
354 .build();
355 OFInstruction instruction = OFFactories
356 .getFactory(msg.getVersion())
357 .instructions()
358 .applyActions(Collections.singletonList(action));
359 OFFlowStatsEntry fs = getSwitch(dpid).factory().buildFlowStatsEntry()
360 .setMatch(entry.getMatch())
361 .setTableId(entry.getTableId())
362 .setDurationSec(entry.getDurationSec())
363 .setDurationNsec(entry.getDurationNsec())
364 .setPriority(entry.getPriority())
365 .setIdleTimeout(entry.getIdleTimeout())
366 .setHardTimeout(entry.getHardTimeout())
367 .setFlags(entry.getFlags())
368 .setCookie(entry.getCookie())
369 .setInstructions(Collections.singletonList(instruction))
370 .build();
371 entries.add(fs);
372 }
373 fsr.setEntries(entries);
374
375 flowStats = publishFlowStats(dpid, fsr.build());
376 if (flowStats != null) {
377 OFFlowStatsReply.Builder rep =
378 OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply();
379 rep.setEntries(Lists.newLinkedList(flowStats));
380 executorMsgs.submit(new OFMessageHandler(dpid, rep.build()));
381 }
382 } else {
Michael Bunimovich1b45e1f2015-08-27 07:49:25 -0400383 executorMsgs.submit(new OFMessageHandler(dpid, reply));
Marc De Leenheerb9311372015-07-09 11:36:49 -0700384 }
385 break;
sangho6a0bb172015-02-05 12:24:48 -0800386 default:
alshabib5eb79392015-08-19 18:09:55 -0700387 log.warn("Discarding unknown stats reply type {}", reply.getStatsType());
Marc De Leenheerb9311372015-07-09 11:36:49 -0700388 break;
alshabib64def642014-12-02 23:27:37 -0800389 }
390 break;
alshabib8f1cf4a2014-09-17 14:44:48 -0700391 case BARRIER_REPLY:
Pavlin Radoslavov369c6432014-12-03 16:25:14 -0800392 executorBarrier.submit(new OFMessageHandler(dpid, msg));
alshabib8f1cf4a2014-09-17 14:44:48 -0700393 break;
Marc De Leenheer631ffce2014-10-28 16:29:07 -0700394 case EXPERIMENTER:
Marc De Leenheerb9311372015-07-09 11:36:49 -0700395 long experimenter = ((OFExperimenter) msg).getExperimenter();
396 if (experimenter == 0x748771) {
397 // LINC-OE port stats
Marc De Leenheer631ffce2014-10-28 16:29:07 -0700398 OFCircuitPortStatus circuitPortStatus = (OFCircuitPortStatus) msg;
399 OFPortStatus.Builder portStatus = this.getSwitch(dpid).factory().buildPortStatus();
400 OFPortDesc.Builder portDesc = this.getSwitch(dpid).factory().buildPortDesc();
401 portDesc.setPortNo(circuitPortStatus.getPortNo())
402 .setHwAddr(circuitPortStatus.getHwAddr())
403 .setName(circuitPortStatus.getName())
404 .setConfig(circuitPortStatus.getConfig())
405 .setState(circuitPortStatus.getState());
406 portStatus.setReason(circuitPortStatus.getReason()).setDesc(portDesc.build());
407 for (OpenFlowSwitchListener l : ofSwitchListener) {
408 l.portChanged(dpid, portStatus.build());
409 }
410 } else {
411 log.warn("Handling experimenter type {} not yet implemented",
412 ((OFExperimenter) msg).getExperimenter(), msg);
413 }
414 break;
tom7ef8ff92014-09-17 13:08:06 -0700415 default:
416 log.warn("Handling message type {} not yet implemented {}",
417 msg.getType(), msg);
418 }
419 }
420
sangho6a0bb172015-02-05 12:24:48 -0800421 private synchronized Collection<OFFlowStatsEntry> publishFlowStats(Dpid dpid,
422 OFFlowStatsReply reply) {
alshabib64def642014-12-02 23:27:37 -0800423 //TODO: Get rid of synchronized
sangho6a0bb172015-02-05 12:24:48 -0800424 fullFlowStats.putAll(dpid, reply.getEntries());
alshabib64def642014-12-02 23:27:37 -0800425 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
sangho6a0bb172015-02-05 12:24:48 -0800426 return fullFlowStats.removeAll(dpid);
427 }
428 return null;
429 }
430
Srikanth Vavilapalli95810f52015-09-14 15:49:56 -0700431 private synchronized Collection<OFTableStatsEntry> publishTableStats(Dpid dpid,
432 OFTableStatsReply reply) {
433 //TODO: Get rid of synchronized
434 fullTableStats.putAll(dpid, reply.getEntries());
435 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
436 return fullTableStats.removeAll(dpid);
437 }
438 return null;
439 }
440
sangho6a0bb172015-02-05 12:24:48 -0800441 private synchronized Collection<OFGroupStatsEntry> publishGroupStats(Dpid dpid,
442 OFGroupStatsReply reply) {
443 //TODO: Get rid of synchronized
444 fullGroupStats.putAll(dpid, reply.getEntries());
445 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
446 return fullGroupStats.removeAll(dpid);
447 }
448 return null;
449 }
450
451 private synchronized Collection<OFGroupDescStatsEntry> publishGroupDescStats(Dpid dpid,
452 OFGroupDescStatsReply reply) {
453 //TODO: Get rid of synchronized
454 fullGroupDescStats.putAll(dpid, reply.getEntries());
455 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
456 return fullGroupDescStats.removeAll(dpid);
alshabib64def642014-12-02 23:27:37 -0800457 }
458 return null;
459 }
460
sangho538108b2015-04-08 14:29:20 -0700461 private synchronized Collection<OFPortStatsEntry> publishPortStats(Dpid dpid,
462 OFPortStatsReply reply) {
463 fullPortStats.putAll(dpid, reply.getEntries());
464 if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) {
465 return fullPortStats.removeAll(dpid);
466 }
467 return null;
468 }
469
tom7ef8ff92014-09-17 13:08:06 -0700470 @Override
471 public void setRole(Dpid dpid, RoleState role) {
Yuta HIGUCHI79cbd1c2014-10-02 16:57:57 -0700472 final OpenFlowSwitch sw = getSwitch(dpid);
473 if (sw == null) {
474 log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role);
475 return;
476 }
477 sw.setRole(role);
tom7ef8ff92014-09-17 13:08:06 -0700478 }
479
480 /**
481 * Implementation of an OpenFlow Agent which is responsible for
482 * keeping track of connected switches and the state in which
483 * they are.
484 */
485 public class OpenFlowSwitchAgent implements OpenFlowAgent {
486
487 private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class);
488 private final Lock switchLock = new ReentrantLock();
489
490 @Override
491 public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) {
alshabib9eab22f2014-10-20 17:17:31 -0700492
tom7ef8ff92014-09-17 13:08:06 -0700493 if (connectedSwitches.get(dpid) != null) {
494 log.error("Trying to add connectedSwitch but found a previous "
495 + "value for dpid: {}", dpid);
496 return false;
497 } else {
Yuta HIGUCHIeb3f30b2014-10-22 11:34:49 -0700498 log.info("Added switch {}", dpid);
tom7ef8ff92014-09-17 13:08:06 -0700499 connectedSwitches.put(dpid, sw);
alshabib8f1cf4a2014-09-17 14:44:48 -0700500 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700501 l.switchAdded(dpid);
502 }
503 return true;
504 }
505 }
506
507 @Override
508 public boolean validActivation(Dpid dpid) {
509 if (connectedSwitches.get(dpid) == null) {
510 log.error("Trying to activate switch but is not in "
511 + "connected switches: dpid {}. Aborting ..",
512 dpid);
513 return false;
514 }
515 if (activeMasterSwitches.get(dpid) != null ||
516 activeEqualSwitches.get(dpid) != null) {
517 log.error("Trying to activate switch but it is already "
518 + "activated: dpid {}. Found in activeMaster: {} "
Ray Milkey6bc43c22015-11-06 13:22:38 -0800519 + "Found in activeEqual: {}. Aborting ..",
520 dpid,
521 (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y',
522 (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y');
tom7ef8ff92014-09-17 13:08:06 -0700523 return false;
524 }
525 return true;
526 }
527
528
529 @Override
530 public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) {
531 switchLock.lock();
532 try {
533 if (!validActivation(dpid)) {
534 return false;
535 }
536 activeMasterSwitches.put(dpid, sw);
537 return true;
538 } finally {
539 switchLock.unlock();
540 }
541 }
542
543 @Override
544 public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) {
545 switchLock.lock();
546 try {
547 if (!validActivation(dpid)) {
548 return false;
549 }
550 activeEqualSwitches.put(dpid, sw);
551 log.info("Added Activated EQUAL Switch {}", dpid);
552 return true;
553 } finally {
554 switchLock.unlock();
555 }
556 }
557
558 @Override
559 public void transitionToMasterSwitch(Dpid dpid) {
560 switchLock.lock();
561 try {
562 if (activeMasterSwitches.containsKey(dpid)) {
563 return;
564 }
565 OpenFlowSwitch sw = activeEqualSwitches.remove(dpid);
566 if (sw == null) {
567 sw = getSwitch(dpid);
568 if (sw == null) {
569 log.error("Transition to master called on sw {}, but switch "
570 + "was not found in controller-cache", dpid);
571 return;
572 }
573 }
574 log.info("Transitioned switch {} to MASTER", dpid);
575 activeMasterSwitches.put(dpid, sw);
576 } finally {
577 switchLock.unlock();
578 }
579 }
580
581
582 @Override
583 public void transitionToEqualSwitch(Dpid dpid) {
584 switchLock.lock();
585 try {
586 if (activeEqualSwitches.containsKey(dpid)) {
587 return;
588 }
589 OpenFlowSwitch sw = activeMasterSwitches.remove(dpid);
590 if (sw == null) {
591 sw = getSwitch(dpid);
592 if (sw == null) {
593 log.error("Transition to equal called on sw {}, but switch "
594 + "was not found in controller-cache", dpid);
595 return;
596 }
597 }
598 log.info("Transitioned switch {} to EQUAL", dpid);
599 activeEqualSwitches.put(dpid, sw);
600 } finally {
601 switchLock.unlock();
602 }
603
604 }
605
606 @Override
607 public void removeConnectedSwitch(Dpid dpid) {
608 connectedSwitches.remove(dpid);
609 OpenFlowSwitch sw = activeMasterSwitches.remove(dpid);
610 if (sw == null) {
Thomas Vachuska3358af22015-05-19 18:40:34 -0700611 log.debug("sw was null for {}", dpid);
tom7ef8ff92014-09-17 13:08:06 -0700612 sw = activeEqualSwitches.remove(dpid);
613 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700614 for (OpenFlowSwitchListener l : ofSwitchListener) {
tom7ef8ff92014-09-17 13:08:06 -0700615 l.switchRemoved(dpid);
616 }
617 }
618
619 @Override
620 public void processMessage(Dpid dpid, OFMessage m) {
621 processPacket(dpid, m);
622 }
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700623
624 @Override
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700625 public void returnRoleReply(Dpid dpid, RoleState requested, RoleState response) {
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700626 for (OpenFlowSwitchListener l : ofSwitchListener) {
Ayaka Koshibe3ef2b0d2014-10-31 13:58:27 -0700627 l.receivedRoleReply(dpid, requested, response);
Ayaka Koshibeab91cc42014-09-25 10:20:52 -0700628 }
629 }
tom7ef8ff92014-09-17 13:08:06 -0700630 }
631
Jian Li152b8852015-12-07 14:47:25 -0800632 /**
633 * OpenFlow message handler for incoming control messages.
634 */
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800635 protected final class OFMessageHandler implements Runnable {
alshabib8f1cf4a2014-09-17 14:44:48 -0700636
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800637 protected final OFMessage msg;
638 protected final Dpid dpid;
alshabib8f1cf4a2014-09-17 14:44:48 -0700639
640 public OFMessageHandler(Dpid dpid, OFMessage msg) {
641 this.msg = msg;
642 this.dpid = dpid;
643 }
644
645 @Override
646 public void run() {
alshabibeec3a062014-09-17 18:01:26 -0700647 for (OpenFlowEventListener listener : ofEventListener) {
alshabib8f1cf4a2014-09-17 14:44:48 -0700648 listener.handleMessage(dpid, msg);
649 }
650 }
alshabib8f1cf4a2014-09-17 14:44:48 -0700651 }
tom7ef8ff92014-09-17 13:08:06 -0700652}