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