Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ArrayListMultimap; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Multimap; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Modified; |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Property; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Service; |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 29 | import org.onosproject.cfg.ComponentConfigService; |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 30 | import org.onosproject.net.driver.DefaultDriverProviderService; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 31 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.openflow.controller.DefaultOpenFlowPacketContext; |
| 33 | import org.onosproject.openflow.controller.Dpid; |
| 34 | import org.onosproject.openflow.controller.OpenFlowController; |
| 35 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 36 | import org.onosproject.openflow.controller.OpenFlowPacketContext; |
| 37 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 38 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 39 | import org.onosproject.openflow.controller.PacketListener; |
| 40 | import org.onosproject.openflow.controller.RoleState; |
| 41 | import org.onosproject.openflow.controller.driver.OpenFlowAgent; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 42 | import org.osgi.service.component.ComponentContext; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 43 | import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsEntry; |
| 44 | import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsReply; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 45 | import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus; |
| 46 | import org.projectfloodlight.openflow.protocol.OFExperimenter; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 48 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 49 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 50 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry; |
| 51 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
| 52 | import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry; |
| 53 | import org.projectfloodlight.openflow.protocol.OFGroupStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 54 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 55 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 56 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 57 | import org.projectfloodlight.openflow.protocol.OFPortStatsEntry; |
| 58 | import org.projectfloodlight.openflow.protocol.OFPortStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 60 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
| 63 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 64 | import org.slf4j.Logger; |
| 65 | import org.slf4j.LoggerFactory; |
| 66 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 67 | import java.util.Collection; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 68 | import java.util.Collections; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 69 | import java.util.LinkedList; |
| 70 | import java.util.List; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 71 | import java.util.Set; |
| 72 | import java.util.concurrent.ConcurrentHashMap; |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 73 | import java.util.concurrent.CopyOnWriteArraySet; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 74 | import java.util.concurrent.ExecutorService; |
| 75 | import java.util.concurrent.Executors; |
| 76 | import java.util.concurrent.locks.Lock; |
| 77 | import java.util.concurrent.locks.ReentrantLock; |
| 78 | |
| 79 | import static org.onlab.util.Tools.groupedThreads; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 80 | |
| 81 | @Component(immediate = true) |
| 82 | @Service |
| 83 | public class OpenFlowControllerImpl implements OpenFlowController { |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 84 | private static final String DEFAULT_OFPORT = "6633,6653"; |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 85 | private static final int DEFAULT_WORKER_THREADS = 16; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 86 | |
| 87 | private static final Logger log = |
| 88 | LoggerFactory.getLogger(OpenFlowControllerImpl.class); |
| 89 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected DriverService driverService; |
| 92 | |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 93 | // References exists merely for sequencing purpose to assure drivers are loaded |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected DefaultDriverProviderService defaultDriverProviderService; |
| 96 | |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected ComponentConfigService cfgService; |
| 99 | |
Brian O'Connor | e2a399e | 2015-09-22 15:32:50 -0700 | [diff] [blame] | 100 | @Property(name = "openflowPorts", value = DEFAULT_OFPORT, |
| 101 | label = "Port numbers (comma separated) used by OpenFlow protocol; default is 6633,6653") |
| 102 | private String openflowPorts = DEFAULT_OFPORT; |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 103 | |
| 104 | @Property(name = "workerThreads", intValue = DEFAULT_WORKER_THREADS, |
| 105 | label = "Number of controller worker threads; default is 16") |
| 106 | private int workerThreads = DEFAULT_WORKER_THREADS; |
| 107 | |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 108 | private final ExecutorService executorMsgs = |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 109 | Executors.newFixedThreadPool(32, groupedThreads("onos/of", "event-stats-%d")); |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 110 | |
| 111 | private final ExecutorService executorBarrier = |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 112 | Executors.newFixedThreadPool(4, groupedThreads("onos/of", "event-barrier-%d")); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 113 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 114 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> connectedSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 115 | new ConcurrentHashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 116 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeMasterSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 117 | new ConcurrentHashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 118 | protected ConcurrentHashMap<Dpid, OpenFlowSwitch> activeEqualSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 119 | new ConcurrentHashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 120 | |
| 121 | protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent(); |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 122 | protected Set<OpenFlowSwitchListener> ofSwitchListener = new CopyOnWriteArraySet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 123 | |
| 124 | protected Multimap<Integer, PacketListener> ofPacketListener = |
| 125 | ArrayListMultimap.create(); |
| 126 | |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 127 | protected Set<OpenFlowEventListener> ofEventListener = new CopyOnWriteArraySet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 128 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 129 | protected Multimap<Dpid, OFFlowStatsEntry> fullFlowStats = |
| 130 | ArrayListMultimap.create(); |
| 131 | |
| 132 | protected Multimap<Dpid, OFGroupStatsEntry> fullGroupStats = |
| 133 | ArrayListMultimap.create(); |
| 134 | |
| 135 | protected Multimap<Dpid, OFGroupDescStatsEntry> fullGroupDescStats = |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 136 | ArrayListMultimap.create(); |
| 137 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 138 | protected Multimap<Dpid, OFPortStatsEntry> fullPortStats = |
| 139 | ArrayListMultimap.create(); |
| 140 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 141 | private final Controller ctrl = new Controller(); |
| 142 | |
| 143 | @Activate |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 144 | public void activate(ComponentContext context) { |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 145 | cfgService.registerProperties(getClass()); |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 146 | ctrl.setConfigParams(context.getProperties()); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 147 | ctrl.start(agent, driverService); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Deactivate |
| 151 | public void deactivate() { |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 152 | cfgService.unregisterProperties(getClass(), false); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 153 | ctrl.stop(); |
| 154 | } |
| 155 | |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 156 | @Modified |
| 157 | public void modified(ComponentContext context) { |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 158 | ctrl.stop(); |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 159 | ctrl.setConfigParams(context.getProperties()); |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 160 | ctrl.start(agent, driverService); |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 161 | } |
| 162 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 163 | @Override |
| 164 | public Iterable<OpenFlowSwitch> getSwitches() { |
| 165 | return connectedSwitches.values(); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public Iterable<OpenFlowSwitch> getMasterSwitches() { |
| 170 | return activeMasterSwitches.values(); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public Iterable<OpenFlowSwitch> getEqualSwitches() { |
| 175 | return activeEqualSwitches.values(); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public OpenFlowSwitch getSwitch(Dpid dpid) { |
| 180 | return connectedSwitches.get(dpid); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public OpenFlowSwitch getMasterSwitch(Dpid dpid) { |
| 185 | return activeMasterSwitches.get(dpid); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public OpenFlowSwitch getEqualSwitch(Dpid dpid) { |
| 190 | return activeEqualSwitches.get(dpid); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void addListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 195 | if (!ofSwitchListener.contains(listener)) { |
| 196 | this.ofSwitchListener.add(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void removeListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 202 | this.ofSwitchListener.remove(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void addPacketListener(int priority, PacketListener listener) { |
| 207 | ofPacketListener.put(priority, listener); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void removePacketListener(PacketListener listener) { |
| 212 | ofPacketListener.values().remove(listener); |
| 213 | } |
| 214 | |
| 215 | @Override |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 216 | public void addEventListener(OpenFlowEventListener listener) { |
| 217 | ofEventListener.add(listener); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void removeEventListener(OpenFlowEventListener listener) { |
| 222 | ofEventListener.remove(listener); |
| 223 | } |
| 224 | |
| 225 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 226 | public void write(Dpid dpid, OFMessage msg) { |
| 227 | this.getSwitch(dpid).sendMsg(msg); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public void processPacket(Dpid dpid, OFMessage msg) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 232 | Collection<OFFlowStatsEntry> flowStats; |
| 233 | Collection<OFGroupStatsEntry> groupStats; |
| 234 | Collection<OFGroupDescStatsEntry> groupDescStats; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 235 | Collection<OFPortStatsEntry> portStats; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 236 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 237 | switch (msg.getType()) { |
| 238 | case PORT_STATUS: |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 239 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 240 | l.portChanged(dpid, (OFPortStatus) msg); |
| 241 | } |
| 242 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 243 | case FEATURES_REPLY: |
| 244 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 245 | l.switchChanged(dpid); |
| 246 | } |
| 247 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 248 | case PACKET_IN: |
| 249 | OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext |
| 250 | .packetContextFromPacketIn(this.getSwitch(dpid), |
| 251 | (OFPacketIn) msg); |
| 252 | for (PacketListener p : ofPacketListener.values()) { |
| 253 | p.handlePacket(pktCtx); |
| 254 | } |
| 255 | break; |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 256 | // TODO: Consider using separate threadpool for sensitive messages. |
| 257 | // ie. Back to back error could cause us to starve. |
| 258 | case FLOW_REMOVED: |
| 259 | case ERROR: |
| 260 | executorMsgs.submit(new OFMessageHandler(dpid, msg)); |
| 261 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 262 | case STATS_REPLY: |
| 263 | OFStatsReply reply = (OFStatsReply) msg; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 264 | switch (reply.getStatsType()) { |
| 265 | case PORT_DESC: |
| 266 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 267 | l.switchChanged(dpid); |
| 268 | } |
| 269 | break; |
| 270 | case FLOW: |
| 271 | flowStats = publishFlowStats(dpid, (OFFlowStatsReply) reply); |
| 272 | if (flowStats != null) { |
| 273 | OFFlowStatsReply.Builder rep = |
| 274 | OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply(); |
| 275 | rep.setEntries(Lists.newLinkedList(flowStats)); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame^] | 276 | rep.setXid(reply.getXid()); |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 277 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 278 | } |
| 279 | break; |
| 280 | case GROUP: |
| 281 | groupStats = publishGroupStats(dpid, (OFGroupStatsReply) reply); |
| 282 | if (groupStats != null) { |
| 283 | OFGroupStatsReply.Builder rep = |
| 284 | OFFactories.getFactory(msg.getVersion()).buildGroupStatsReply(); |
| 285 | rep.setEntries(Lists.newLinkedList(groupStats)); |
| 286 | rep.setXid(reply.getXid()); |
| 287 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 288 | } |
| 289 | break; |
| 290 | case GROUP_DESC: |
| 291 | groupDescStats = publishGroupDescStats(dpid, |
| 292 | (OFGroupDescStatsReply) reply); |
| 293 | if (groupDescStats != null) { |
| 294 | OFGroupDescStatsReply.Builder rep = |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 295 | OFFactories.getFactory(msg.getVersion()).buildGroupDescStatsReply(); |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 296 | rep.setEntries(Lists.newLinkedList(groupDescStats)); |
| 297 | rep.setXid(reply.getXid()); |
| 298 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 299 | } |
| 300 | break; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 301 | case PORT: |
| 302 | executorMsgs.submit(new OFMessageHandler(dpid, reply)); |
| 303 | break; |
alshabib | 5eb7939 | 2015-08-19 18:09:55 -0700 | [diff] [blame] | 304 | case METER: |
| 305 | executorMsgs.submit(new OFMessageHandler(dpid, reply)); |
| 306 | break; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 307 | case EXPERIMENTER: |
| 308 | if (reply instanceof OFCalientFlowStatsReply) { |
| 309 | // Convert Calient flow statistics to regular flow stats |
| 310 | // TODO: parse remaining fields such as power levels etc. when we have proper monitoring API |
| 311 | OFFlowStatsReply.Builder fsr = getSwitch(dpid).factory().buildFlowStatsReply(); |
| 312 | List<OFFlowStatsEntry> entries = new LinkedList<>(); |
| 313 | for (OFCalientFlowStatsEntry entry : ((OFCalientFlowStatsReply) msg).getEntries()) { |
| 314 | |
| 315 | // Single instruction, i.e., output to port |
| 316 | OFActionOutput action = OFFactories |
| 317 | .getFactory(msg.getVersion()) |
| 318 | .actions() |
| 319 | .buildOutput() |
| 320 | .setPort(entry.getOutPort()) |
| 321 | .build(); |
| 322 | OFInstruction instruction = OFFactories |
| 323 | .getFactory(msg.getVersion()) |
| 324 | .instructions() |
| 325 | .applyActions(Collections.singletonList(action)); |
| 326 | OFFlowStatsEntry fs = getSwitch(dpid).factory().buildFlowStatsEntry() |
| 327 | .setMatch(entry.getMatch()) |
| 328 | .setTableId(entry.getTableId()) |
| 329 | .setDurationSec(entry.getDurationSec()) |
| 330 | .setDurationNsec(entry.getDurationNsec()) |
| 331 | .setPriority(entry.getPriority()) |
| 332 | .setIdleTimeout(entry.getIdleTimeout()) |
| 333 | .setHardTimeout(entry.getHardTimeout()) |
| 334 | .setFlags(entry.getFlags()) |
| 335 | .setCookie(entry.getCookie()) |
| 336 | .setInstructions(Collections.singletonList(instruction)) |
| 337 | .build(); |
| 338 | entries.add(fs); |
| 339 | } |
| 340 | fsr.setEntries(entries); |
| 341 | |
| 342 | flowStats = publishFlowStats(dpid, fsr.build()); |
| 343 | if (flowStats != null) { |
| 344 | OFFlowStatsReply.Builder rep = |
| 345 | OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply(); |
| 346 | rep.setEntries(Lists.newLinkedList(flowStats)); |
| 347 | executorMsgs.submit(new OFMessageHandler(dpid, rep.build())); |
| 348 | } |
| 349 | } else { |
Michael Bunimovich | 1b45e1f | 2015-08-27 07:49:25 -0400 | [diff] [blame] | 350 | executorMsgs.submit(new OFMessageHandler(dpid, reply)); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 351 | } |
| 352 | break; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 353 | default: |
alshabib | 5eb7939 | 2015-08-19 18:09:55 -0700 | [diff] [blame] | 354 | log.warn("Discarding unknown stats reply type {}", reply.getStatsType()); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 355 | break; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 356 | } |
| 357 | break; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 358 | case BARRIER_REPLY: |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 359 | executorBarrier.submit(new OFMessageHandler(dpid, msg)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 360 | break; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 361 | case EXPERIMENTER: |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 362 | long experimenter = ((OFExperimenter) msg).getExperimenter(); |
| 363 | if (experimenter == 0x748771) { |
| 364 | // LINC-OE port stats |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 365 | OFCircuitPortStatus circuitPortStatus = (OFCircuitPortStatus) msg; |
| 366 | OFPortStatus.Builder portStatus = this.getSwitch(dpid).factory().buildPortStatus(); |
| 367 | OFPortDesc.Builder portDesc = this.getSwitch(dpid).factory().buildPortDesc(); |
| 368 | portDesc.setPortNo(circuitPortStatus.getPortNo()) |
| 369 | .setHwAddr(circuitPortStatus.getHwAddr()) |
| 370 | .setName(circuitPortStatus.getName()) |
| 371 | .setConfig(circuitPortStatus.getConfig()) |
| 372 | .setState(circuitPortStatus.getState()); |
| 373 | portStatus.setReason(circuitPortStatus.getReason()).setDesc(portDesc.build()); |
| 374 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 375 | l.portChanged(dpid, portStatus.build()); |
| 376 | } |
| 377 | } else { |
| 378 | log.warn("Handling experimenter type {} not yet implemented", |
| 379 | ((OFExperimenter) msg).getExperimenter(), msg); |
| 380 | } |
| 381 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 382 | default: |
| 383 | log.warn("Handling message type {} not yet implemented {}", |
| 384 | msg.getType(), msg); |
| 385 | } |
| 386 | } |
| 387 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 388 | private synchronized Collection<OFFlowStatsEntry> publishFlowStats(Dpid dpid, |
| 389 | OFFlowStatsReply reply) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 390 | //TODO: Get rid of synchronized |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 391 | fullFlowStats.putAll(dpid, reply.getEntries()); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 392 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 393 | return fullFlowStats.removeAll(dpid); |
| 394 | } |
| 395 | return null; |
| 396 | } |
| 397 | |
| 398 | private synchronized Collection<OFGroupStatsEntry> publishGroupStats(Dpid dpid, |
| 399 | OFGroupStatsReply reply) { |
| 400 | //TODO: Get rid of synchronized |
| 401 | fullGroupStats.putAll(dpid, reply.getEntries()); |
| 402 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 403 | return fullGroupStats.removeAll(dpid); |
| 404 | } |
| 405 | return null; |
| 406 | } |
| 407 | |
| 408 | private synchronized Collection<OFGroupDescStatsEntry> publishGroupDescStats(Dpid dpid, |
| 409 | OFGroupDescStatsReply reply) { |
| 410 | //TODO: Get rid of synchronized |
| 411 | fullGroupDescStats.putAll(dpid, reply.getEntries()); |
| 412 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 413 | return fullGroupDescStats.removeAll(dpid); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 414 | } |
| 415 | return null; |
| 416 | } |
| 417 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 418 | private synchronized Collection<OFPortStatsEntry> publishPortStats(Dpid dpid, |
| 419 | OFPortStatsReply reply) { |
| 420 | fullPortStats.putAll(dpid, reply.getEntries()); |
| 421 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 422 | return fullPortStats.removeAll(dpid); |
| 423 | } |
| 424 | return null; |
| 425 | } |
| 426 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 427 | @Override |
| 428 | public void setRole(Dpid dpid, RoleState role) { |
Yuta HIGUCHI | 79cbd1c | 2014-10-02 16:57:57 -0700 | [diff] [blame] | 429 | final OpenFlowSwitch sw = getSwitch(dpid); |
| 430 | if (sw == null) { |
| 431 | log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role); |
| 432 | return; |
| 433 | } |
| 434 | sw.setRole(role); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Implementation of an OpenFlow Agent which is responsible for |
| 439 | * keeping track of connected switches and the state in which |
| 440 | * they are. |
| 441 | */ |
| 442 | public class OpenFlowSwitchAgent implements OpenFlowAgent { |
| 443 | |
| 444 | private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class); |
| 445 | private final Lock switchLock = new ReentrantLock(); |
| 446 | |
| 447 | @Override |
| 448 | public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) { |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 449 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 450 | if (connectedSwitches.get(dpid) != null) { |
| 451 | log.error("Trying to add connectedSwitch but found a previous " |
| 452 | + "value for dpid: {}", dpid); |
| 453 | return false; |
| 454 | } else { |
Yuta HIGUCHI | eb3f30b | 2014-10-22 11:34:49 -0700 | [diff] [blame] | 455 | log.info("Added switch {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 456 | connectedSwitches.put(dpid, sw); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 457 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 458 | l.switchAdded(dpid); |
| 459 | } |
| 460 | return true; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public boolean validActivation(Dpid dpid) { |
| 466 | if (connectedSwitches.get(dpid) == null) { |
| 467 | log.error("Trying to activate switch but is not in " |
| 468 | + "connected switches: dpid {}. Aborting ..", |
| 469 | dpid); |
| 470 | return false; |
| 471 | } |
| 472 | if (activeMasterSwitches.get(dpid) != null || |
| 473 | activeEqualSwitches.get(dpid) != null) { |
| 474 | log.error("Trying to activate switch but it is already " |
| 475 | + "activated: dpid {}. Found in activeMaster: {} " |
| 476 | + "Found in activeEqual: {}. Aborting ..", new Object[]{ |
| 477 | dpid, |
| 478 | (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y', |
| 479 | (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'}); |
| 480 | return false; |
| 481 | } |
| 482 | return true; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | @Override |
| 487 | public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 488 | switchLock.lock(); |
| 489 | try { |
| 490 | if (!validActivation(dpid)) { |
| 491 | return false; |
| 492 | } |
| 493 | activeMasterSwitches.put(dpid, sw); |
| 494 | return true; |
| 495 | } finally { |
| 496 | switchLock.unlock(); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 502 | switchLock.lock(); |
| 503 | try { |
| 504 | if (!validActivation(dpid)) { |
| 505 | return false; |
| 506 | } |
| 507 | activeEqualSwitches.put(dpid, sw); |
| 508 | log.info("Added Activated EQUAL Switch {}", dpid); |
| 509 | return true; |
| 510 | } finally { |
| 511 | switchLock.unlock(); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public void transitionToMasterSwitch(Dpid dpid) { |
| 517 | switchLock.lock(); |
| 518 | try { |
| 519 | if (activeMasterSwitches.containsKey(dpid)) { |
| 520 | return; |
| 521 | } |
| 522 | OpenFlowSwitch sw = activeEqualSwitches.remove(dpid); |
| 523 | if (sw == null) { |
| 524 | sw = getSwitch(dpid); |
| 525 | if (sw == null) { |
| 526 | log.error("Transition to master called on sw {}, but switch " |
| 527 | + "was not found in controller-cache", dpid); |
| 528 | return; |
| 529 | } |
| 530 | } |
| 531 | log.info("Transitioned switch {} to MASTER", dpid); |
| 532 | activeMasterSwitches.put(dpid, sw); |
| 533 | } finally { |
| 534 | switchLock.unlock(); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | |
| 539 | @Override |
| 540 | public void transitionToEqualSwitch(Dpid dpid) { |
| 541 | switchLock.lock(); |
| 542 | try { |
| 543 | if (activeEqualSwitches.containsKey(dpid)) { |
| 544 | return; |
| 545 | } |
| 546 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 547 | if (sw == null) { |
| 548 | sw = getSwitch(dpid); |
| 549 | if (sw == null) { |
| 550 | log.error("Transition to equal called on sw {}, but switch " |
| 551 | + "was not found in controller-cache", dpid); |
| 552 | return; |
| 553 | } |
| 554 | } |
| 555 | log.info("Transitioned switch {} to EQUAL", dpid); |
| 556 | activeEqualSwitches.put(dpid, sw); |
| 557 | } finally { |
| 558 | switchLock.unlock(); |
| 559 | } |
| 560 | |
| 561 | } |
| 562 | |
| 563 | @Override |
| 564 | public void removeConnectedSwitch(Dpid dpid) { |
| 565 | connectedSwitches.remove(dpid); |
| 566 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 567 | if (sw == null) { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 568 | log.debug("sw was null for {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 569 | sw = activeEqualSwitches.remove(dpid); |
| 570 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 571 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 572 | l.switchRemoved(dpid); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | @Override |
| 577 | public void processMessage(Dpid dpid, OFMessage m) { |
| 578 | processPacket(dpid, m); |
| 579 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 580 | |
| 581 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 582 | public void returnRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 583 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 584 | l.receivedRoleReply(dpid, requested, response); |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 585 | } |
| 586 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 587 | } |
| 588 | |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 589 | private final class OFMessageHandler implements Runnable { |
| 590 | |
| 591 | private final OFMessage msg; |
| 592 | private final Dpid dpid; |
| 593 | |
| 594 | public OFMessageHandler(Dpid dpid, OFMessage msg) { |
| 595 | this.msg = msg; |
| 596 | this.dpid = dpid; |
| 597 | } |
| 598 | |
| 599 | @Override |
| 600 | public void run() { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 601 | for (OpenFlowEventListener listener : ofEventListener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 602 | listener.handleMessage(dpid, msg); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | } |
| 607 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 608 | } |