Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present 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; |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 30 | import org.onosproject.core.CoreService; |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 31 | import org.onosproject.net.device.DeviceEvent; |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 32 | import org.onosproject.net.device.DeviceListener; |
| 33 | import org.onosproject.net.device.DeviceService; |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 34 | import org.onosproject.net.driver.DefaultDriverProviderService; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 35 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.openflow.controller.DefaultOpenFlowPacketContext; |
| 37 | import org.onosproject.openflow.controller.Dpid; |
| 38 | import org.onosproject.openflow.controller.OpenFlowController; |
| 39 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 40 | import org.onosproject.openflow.controller.OpenFlowMessageListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.openflow.controller.OpenFlowPacketContext; |
| 42 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 43 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 44 | import org.onosproject.openflow.controller.PacketListener; |
| 45 | import org.onosproject.openflow.controller.RoleState; |
| 46 | import org.onosproject.openflow.controller.driver.OpenFlowAgent; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 47 | import org.osgi.service.component.ComponentContext; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 48 | import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsEntry; |
| 49 | import org.projectfloodlight.openflow.protocol.OFCalientFlowStatsReply; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 50 | import org.projectfloodlight.openflow.protocol.OFCircuitPortStatus; |
| 51 | import org.projectfloodlight.openflow.protocol.OFExperimenter; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 52 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 53 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 54 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 55 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsEntry; |
| 56 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
| 57 | import org.projectfloodlight.openflow.protocol.OFGroupStatsEntry; |
| 58 | import org.projectfloodlight.openflow.protocol.OFGroupStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 60 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.protocol.OFPortStatsEntry; |
| 63 | import org.projectfloodlight.openflow.protocol.OFPortStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 65 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 66 | import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags; |
Jian Li | 2266bff | 2016-04-21 11:01:25 -0700 | [diff] [blame] | 67 | import org.projectfloodlight.openflow.protocol.OFTableStatsEntry; |
| 68 | import org.projectfloodlight.openflow.protocol.OFTableStatsReply; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 69 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
| 70 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 71 | import org.slf4j.Logger; |
| 72 | import org.slf4j.LoggerFactory; |
| 73 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 74 | import java.util.Collection; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 75 | import java.util.Collections; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 76 | import java.util.LinkedList; |
| 77 | import java.util.List; |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 78 | import java.util.Optional; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 79 | import java.util.Set; |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame^] | 80 | import java.util.concurrent.CompletableFuture; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 81 | import java.util.concurrent.ConcurrentHashMap; |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 82 | import java.util.concurrent.ConcurrentMap; |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 83 | import java.util.concurrent.CopyOnWriteArraySet; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 84 | import java.util.concurrent.ExecutorService; |
| 85 | import java.util.concurrent.Executors; |
| 86 | import java.util.concurrent.locks.Lock; |
| 87 | import java.util.concurrent.locks.ReentrantLock; |
Jian Li | 2266bff | 2016-04-21 11:01:25 -0700 | [diff] [blame] | 88 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 89 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | e71494d | 2016-11-15 14:45:25 -0800 | [diff] [blame] | 90 | import static org.onosproject.net.Device.Type.CONTROLLER; |
| 91 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED; |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 92 | import static org.onosproject.openflow.controller.Dpid.dpid; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 93 | |
| 94 | @Component(immediate = true) |
| 95 | @Service |
| 96 | public class OpenFlowControllerImpl implements OpenFlowController { |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 97 | private static final String APP_ID = "org.onosproject.openflow-base"; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 98 | private static final String DEFAULT_OFPORT = "6633,6653"; |
Yuta HIGUCHI | 8552b17 | 2016-07-25 12:10:08 -0700 | [diff] [blame] | 99 | private static final int DEFAULT_WORKER_THREADS = 0; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 100 | |
| 101 | private static final Logger log = |
| 102 | LoggerFactory.getLogger(OpenFlowControllerImpl.class); |
| 103 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 105 | protected CoreService coreService; |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 108 | protected DriverService driverService; |
| 109 | |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 110 | // References exists merely for sequencing purpose to assure drivers are loaded |
| 111 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 112 | protected DefaultDriverProviderService defaultDriverProviderService; |
| 113 | |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected ComponentConfigService cfgService; |
| 116 | |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 118 | protected DeviceService deviceService; |
| 119 | |
| 120 | |
Brian O'Connor | e2a399e | 2015-09-22 15:32:50 -0700 | [diff] [blame] | 121 | @Property(name = "openflowPorts", value = DEFAULT_OFPORT, |
| 122 | label = "Port numbers (comma separated) used by OpenFlow protocol; default is 6633,6653") |
| 123 | private String openflowPorts = DEFAULT_OFPORT; |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 124 | |
| 125 | @Property(name = "workerThreads", intValue = DEFAULT_WORKER_THREADS, |
Yuta HIGUCHI | 8552b17 | 2016-07-25 12:10:08 -0700 | [diff] [blame] | 126 | label = "Number of controller worker threads") |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 127 | private int workerThreads = DEFAULT_WORKER_THREADS; |
| 128 | |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 129 | protected ExecutorService executorMsgs = |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 130 | Executors.newFixedThreadPool(32, groupedThreads("onos/of", "event-stats-%d", log)); |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 131 | |
| 132 | private final ExecutorService executorBarrier = |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 133 | Executors.newFixedThreadPool(4, groupedThreads("onos/of", "event-barrier-%d", log)); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 134 | |
Prince Pereira | e779803 | 2016-07-08 16:31:58 +0530 | [diff] [blame] | 135 | //Separate executor thread for handling error messages and barrier replies for same failed |
| 136 | // transactions to avoid context switching of thread |
| 137 | protected ExecutorService executorErrorMsgs = |
| 138 | Executors.newSingleThreadExecutor(groupedThreads("onos/of", "event-error-msg-%d", log)); |
| 139 | |
| 140 | //concurrent hashmap to track failed transactions |
| 141 | protected ConcurrentMap<Long, Boolean> errorMsgs = |
| 142 | new ConcurrentHashMap<>(); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 143 | protected ConcurrentMap<Dpid, OpenFlowSwitch> connectedSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 144 | new ConcurrentHashMap<>(); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 145 | protected ConcurrentMap<Dpid, OpenFlowSwitch> activeMasterSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 146 | new ConcurrentHashMap<>(); |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 147 | protected ConcurrentMap<Dpid, OpenFlowSwitch> activeEqualSwitches = |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 148 | new ConcurrentHashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 149 | |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame^] | 150 | // Key: dpid, value: map with key: long (XID), value: completable future |
| 151 | protected ConcurrentMap<Dpid, ConcurrentMap<Long, CompletableFuture<OFMessage>>> responses = |
| 152 | new ConcurrentHashMap<>(); |
| 153 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 154 | protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent(); |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 155 | protected Set<OpenFlowSwitchListener> ofSwitchListener = new CopyOnWriteArraySet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 156 | |
| 157 | protected Multimap<Integer, PacketListener> ofPacketListener = |
| 158 | ArrayListMultimap.create(); |
| 159 | |
Jonathan Hart | 6d44d19 | 2015-05-11 18:01:19 -0700 | [diff] [blame] | 160 | protected Set<OpenFlowEventListener> ofEventListener = new CopyOnWriteArraySet<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 161 | |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 162 | protected Set<OpenFlowMessageListener> ofMessageListener = new CopyOnWriteArraySet<>(); |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 163 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 164 | protected Multimap<Dpid, OFFlowStatsEntry> fullFlowStats = |
| 165 | ArrayListMultimap.create(); |
| 166 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 167 | protected Multimap<Dpid, OFTableStatsEntry> fullTableStats = |
| 168 | ArrayListMultimap.create(); |
| 169 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 170 | protected Multimap<Dpid, OFGroupStatsEntry> fullGroupStats = |
| 171 | ArrayListMultimap.create(); |
| 172 | |
| 173 | protected Multimap<Dpid, OFGroupDescStatsEntry> fullGroupDescStats = |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 174 | ArrayListMultimap.create(); |
| 175 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 176 | protected Multimap<Dpid, OFPortStatsEntry> fullPortStats = |
| 177 | ArrayListMultimap.create(); |
| 178 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 179 | private final Controller ctrl = new Controller(); |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 180 | private InternalDeviceListener listener = new InternalDeviceListener(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 181 | |
| 182 | @Activate |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 183 | public void activate(ComponentContext context) { |
Andrea Campanella | 3556f36 | 2016-04-28 15:18:10 -0700 | [diff] [blame] | 184 | coreService.registerApplication(APP_ID, this::cleanup); |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 185 | cfgService.registerProperties(getClass()); |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 186 | deviceService.addListener(listener); |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 187 | ctrl.setConfigParams(context.getProperties()); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 188 | ctrl.start(agent, driverService); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Andrea Campanella | 3556f36 | 2016-04-28 15:18:10 -0700 | [diff] [blame] | 191 | private void cleanup() { |
| 192 | // Close listening channel and all OF channels. Clean information about switches |
| 193 | // before deactivating |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 194 | ctrl.stop(); |
| 195 | connectedSwitches.values().forEach(OpenFlowSwitch::disconnectSwitch); |
Andrea Campanella | 3556f36 | 2016-04-28 15:18:10 -0700 | [diff] [blame] | 196 | connectedSwitches.clear(); |
| 197 | activeMasterSwitches.clear(); |
| 198 | activeEqualSwitches.clear(); |
Charles Chan | ecfdfb7 | 2015-11-24 19:05:50 -0800 | [diff] [blame] | 199 | } |
| 200 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 201 | @Deactivate |
| 202 | public void deactivate() { |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 203 | deviceService.removeListener(listener); |
Thiago Santos | 6172540 | 2016-08-05 17:58:56 -0300 | [diff] [blame] | 204 | cleanup(); |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 205 | cfgService.unregisterProperties(getClass(), false); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 208 | @Modified |
| 209 | public void modified(ComponentContext context) { |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 210 | ctrl.stop(); |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 211 | ctrl.setConfigParams(context.getProperties()); |
Charles Chan | 3b00e1b | 2015-08-26 23:12:52 +0800 | [diff] [blame] | 212 | ctrl.start(agent, driverService); |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 213 | } |
| 214 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 215 | @Override |
| 216 | public Iterable<OpenFlowSwitch> getSwitches() { |
| 217 | return connectedSwitches.values(); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Iterable<OpenFlowSwitch> getMasterSwitches() { |
| 222 | return activeMasterSwitches.values(); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public Iterable<OpenFlowSwitch> getEqualSwitches() { |
| 227 | return activeEqualSwitches.values(); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public OpenFlowSwitch getSwitch(Dpid dpid) { |
| 232 | return connectedSwitches.get(dpid); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public OpenFlowSwitch getMasterSwitch(Dpid dpid) { |
| 237 | return activeMasterSwitches.get(dpid); |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public OpenFlowSwitch getEqualSwitch(Dpid dpid) { |
| 242 | return activeEqualSwitches.get(dpid); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public void addListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 247 | if (!ofSwitchListener.contains(listener)) { |
| 248 | this.ofSwitchListener.add(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void removeListener(OpenFlowSwitchListener listener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 254 | this.ofSwitchListener.remove(listener); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 258 | public void addMessageListener(OpenFlowMessageListener listener) { |
| 259 | ofMessageListener.add(listener); |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void removeMessageListener(OpenFlowMessageListener listener) { |
| 264 | ofMessageListener.remove(listener); |
| 265 | } |
| 266 | |
| 267 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 268 | public void addPacketListener(int priority, PacketListener listener) { |
| 269 | ofPacketListener.put(priority, listener); |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public void removePacketListener(PacketListener listener) { |
| 274 | ofPacketListener.values().remove(listener); |
| 275 | } |
| 276 | |
| 277 | @Override |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 278 | public void addEventListener(OpenFlowEventListener listener) { |
| 279 | ofEventListener.add(listener); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void removeEventListener(OpenFlowEventListener listener) { |
| 284 | ofEventListener.remove(listener); |
| 285 | } |
| 286 | |
| 287 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 288 | public void write(Dpid dpid, OFMessage msg) { |
| 289 | this.getSwitch(dpid).sendMsg(msg); |
| 290 | } |
| 291 | |
| 292 | @Override |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame^] | 293 | public CompletableFuture<OFMessage> writeResponse(Dpid dpid, OFMessage msg) { |
| 294 | write(dpid, msg); |
| 295 | |
| 296 | ConcurrentMap<Long, CompletableFuture<OFMessage>> xids = |
| 297 | responses.computeIfAbsent(dpid, k -> new ConcurrentHashMap()); |
| 298 | |
| 299 | CompletableFuture<OFMessage> future = new CompletableFuture(); |
| 300 | xids.put(msg.getXid(), future); |
| 301 | |
| 302 | return future; |
| 303 | } |
| 304 | |
| 305 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 306 | public void processPacket(Dpid dpid, OFMessage msg) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 307 | Collection<OFFlowStatsEntry> flowStats; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 308 | Collection<OFTableStatsEntry> tableStats; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 309 | Collection<OFGroupStatsEntry> groupStats; |
| 310 | Collection<OFGroupDescStatsEntry> groupDescStats; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 311 | Collection<OFPortStatsEntry> portStats; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 312 | |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 313 | OpenFlowSwitch sw = this.getSwitch(dpid); |
| 314 | |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame^] | 315 | // Check if someone is waiting for this message |
| 316 | ConcurrentMap<Long, CompletableFuture<OFMessage>> xids = responses.get(dpid); |
| 317 | if (xids != null && xids.containsKey(msg.getXid())) { |
| 318 | xids.remove(msg.getXid()).complete(msg); |
| 319 | } |
| 320 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 321 | switch (msg.getType()) { |
| 322 | case PORT_STATUS: |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 323 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 324 | l.portChanged(dpid, (OFPortStatus) msg); |
| 325 | } |
| 326 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 327 | case FEATURES_REPLY: |
| 328 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 329 | l.switchChanged(dpid); |
| 330 | } |
| 331 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 332 | case PACKET_IN: |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 333 | if (sw == null) { |
| 334 | log.error("Switch {} is not found", dpid); |
| 335 | break; |
| 336 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 337 | OpenFlowPacketContext pktCtx = DefaultOpenFlowPacketContext |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 338 | .packetContextFromPacketIn(sw, (OFPacketIn) msg); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 339 | for (PacketListener p : ofPacketListener.values()) { |
| 340 | p.handlePacket(pktCtx); |
| 341 | } |
| 342 | break; |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 343 | // TODO: Consider using separate threadpool for sensitive messages. |
| 344 | // ie. Back to back error could cause us to starve. |
| 345 | case FLOW_REMOVED: |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 346 | executorMsgs.execute(new OFMessageHandler(dpid, msg)); |
Pavlin Radoslavov | 369c643 | 2014-12-03 16:25:14 -0800 | [diff] [blame] | 347 | break; |
Prince Pereira | e779803 | 2016-07-08 16:31:58 +0530 | [diff] [blame] | 348 | case ERROR: |
| 349 | log.debug("Received error message from {}: {}", dpid, msg); |
| 350 | errorMsgs.putIfAbsent(msg.getXid(), true); |
| 351 | executorErrorMsgs.execute(new OFMessageHandler(dpid, msg)); |
| 352 | break; |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 353 | case STATS_REPLY: |
| 354 | OFStatsReply reply = (OFStatsReply) msg; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 355 | switch (reply.getStatsType()) { |
| 356 | case PORT_DESC: |
| 357 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 358 | l.switchChanged(dpid); |
| 359 | } |
| 360 | break; |
| 361 | case FLOW: |
| 362 | flowStats = publishFlowStats(dpid, (OFFlowStatsReply) reply); |
| 363 | if (flowStats != null) { |
| 364 | OFFlowStatsReply.Builder rep = |
| 365 | OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply(); |
| 366 | rep.setEntries(Lists.newLinkedList(flowStats)); |
ssyoon90 | 30fbcd9 | 2015-08-17 10:42:07 +0900 | [diff] [blame] | 367 | rep.setXid(reply.getXid()); |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 368 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 369 | } |
| 370 | break; |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 371 | case TABLE: |
| 372 | tableStats = publishTableStats(dpid, (OFTableStatsReply) reply); |
| 373 | if (tableStats != null) { |
| 374 | OFTableStatsReply.Builder rep = |
| 375 | OFFactories.getFactory(msg.getVersion()).buildTableStatsReply(); |
| 376 | rep.setEntries(Lists.newLinkedList(tableStats)); |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 377 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 378 | } |
| 379 | break; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 380 | case GROUP: |
| 381 | groupStats = publishGroupStats(dpid, (OFGroupStatsReply) reply); |
| 382 | if (groupStats != null) { |
| 383 | OFGroupStatsReply.Builder rep = |
| 384 | OFFactories.getFactory(msg.getVersion()).buildGroupStatsReply(); |
| 385 | rep.setEntries(Lists.newLinkedList(groupStats)); |
| 386 | rep.setXid(reply.getXid()); |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 387 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 388 | } |
| 389 | break; |
| 390 | case GROUP_DESC: |
| 391 | groupDescStats = publishGroupDescStats(dpid, |
| 392 | (OFGroupDescStatsReply) reply); |
| 393 | if (groupDescStats != null) { |
| 394 | OFGroupDescStatsReply.Builder rep = |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 395 | OFFactories.getFactory(msg.getVersion()).buildGroupDescStatsReply(); |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 396 | rep.setEntries(Lists.newLinkedList(groupDescStats)); |
| 397 | rep.setXid(reply.getXid()); |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 398 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 399 | } |
| 400 | break; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 401 | case PORT: |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 402 | executorMsgs.execute(new OFMessageHandler(dpid, reply)); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 403 | break; |
alshabib | 5eb7939 | 2015-08-19 18:09:55 -0700 | [diff] [blame] | 404 | case METER: |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 405 | executorMsgs.execute(new OFMessageHandler(dpid, reply)); |
alshabib | 5eb7939 | 2015-08-19 18:09:55 -0700 | [diff] [blame] | 406 | break; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 407 | case EXPERIMENTER: |
| 408 | if (reply instanceof OFCalientFlowStatsReply) { |
| 409 | // Convert Calient flow statistics to regular flow stats |
| 410 | // TODO: parse remaining fields such as power levels etc. when we have proper monitoring API |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 411 | if (sw == null) { |
| 412 | log.error("Switch {} is not found", dpid); |
| 413 | break; |
| 414 | } |
| 415 | OFFlowStatsReply.Builder fsr = sw.factory().buildFlowStatsReply(); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 416 | List<OFFlowStatsEntry> entries = new LinkedList<>(); |
| 417 | for (OFCalientFlowStatsEntry entry : ((OFCalientFlowStatsReply) msg).getEntries()) { |
| 418 | |
| 419 | // Single instruction, i.e., output to port |
| 420 | OFActionOutput action = OFFactories |
| 421 | .getFactory(msg.getVersion()) |
| 422 | .actions() |
| 423 | .buildOutput() |
| 424 | .setPort(entry.getOutPort()) |
| 425 | .build(); |
| 426 | OFInstruction instruction = OFFactories |
| 427 | .getFactory(msg.getVersion()) |
| 428 | .instructions() |
| 429 | .applyActions(Collections.singletonList(action)); |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 430 | OFFlowStatsEntry fs = sw.factory().buildFlowStatsEntry() |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 431 | .setMatch(entry.getMatch()) |
| 432 | .setTableId(entry.getTableId()) |
| 433 | .setDurationSec(entry.getDurationSec()) |
| 434 | .setDurationNsec(entry.getDurationNsec()) |
| 435 | .setPriority(entry.getPriority()) |
| 436 | .setIdleTimeout(entry.getIdleTimeout()) |
| 437 | .setHardTimeout(entry.getHardTimeout()) |
| 438 | .setFlags(entry.getFlags()) |
| 439 | .setCookie(entry.getCookie()) |
| 440 | .setInstructions(Collections.singletonList(instruction)) |
| 441 | .build(); |
| 442 | entries.add(fs); |
| 443 | } |
| 444 | fsr.setEntries(entries); |
| 445 | |
| 446 | flowStats = publishFlowStats(dpid, fsr.build()); |
| 447 | if (flowStats != null) { |
| 448 | OFFlowStatsReply.Builder rep = |
| 449 | OFFactories.getFactory(msg.getVersion()).buildFlowStatsReply(); |
| 450 | rep.setEntries(Lists.newLinkedList(flowStats)); |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 451 | executorMsgs.execute(new OFMessageHandler(dpid, rep.build())); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 452 | } |
| 453 | } else { |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 454 | executorMsgs.execute(new OFMessageHandler(dpid, reply)); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 455 | } |
| 456 | break; |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 457 | default: |
alshabib | 5eb7939 | 2015-08-19 18:09:55 -0700 | [diff] [blame] | 458 | log.warn("Discarding unknown stats reply type {}", reply.getStatsType()); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 459 | break; |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 460 | } |
| 461 | break; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 462 | case BARRIER_REPLY: |
Prince Pereira | e779803 | 2016-07-08 16:31:58 +0530 | [diff] [blame] | 463 | if (errorMsgs.containsKey(msg.getXid())) { |
| 464 | //To make oferror msg handling and corresponding barrier reply serialized, |
| 465 | // executorErrorMsgs is used for both transaction |
| 466 | errorMsgs.remove(msg.getXid()); |
| 467 | executorErrorMsgs.execute(new OFMessageHandler(dpid, msg)); |
| 468 | } else { |
| 469 | executorBarrier.execute(new OFMessageHandler(dpid, msg)); |
| 470 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 471 | break; |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 472 | case EXPERIMENTER: |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 473 | if (sw == null) { |
| 474 | log.error("Switch {} is not found", dpid); |
| 475 | break; |
| 476 | } |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 477 | long experimenter = ((OFExperimenter) msg).getExperimenter(); |
| 478 | if (experimenter == 0x748771) { |
| 479 | // LINC-OE port stats |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 480 | OFCircuitPortStatus circuitPortStatus = (OFCircuitPortStatus) msg; |
sangyun-han | 69ed446 | 2016-07-27 12:10:12 +0900 | [diff] [blame] | 481 | OFPortStatus.Builder portStatus = sw.factory().buildPortStatus(); |
| 482 | OFPortDesc.Builder portDesc = sw.factory().buildPortDesc(); |
Marc De Leenheer | 631ffce | 2014-10-28 16:29:07 -0700 | [diff] [blame] | 483 | portDesc.setPortNo(circuitPortStatus.getPortNo()) |
| 484 | .setHwAddr(circuitPortStatus.getHwAddr()) |
| 485 | .setName(circuitPortStatus.getName()) |
| 486 | .setConfig(circuitPortStatus.getConfig()) |
| 487 | .setState(circuitPortStatus.getState()); |
| 488 | portStatus.setReason(circuitPortStatus.getReason()).setDesc(portDesc.build()); |
| 489 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
| 490 | l.portChanged(dpid, portStatus.build()); |
| 491 | } |
| 492 | } else { |
| 493 | log.warn("Handling experimenter type {} not yet implemented", |
| 494 | ((OFExperimenter) msg).getExperimenter(), msg); |
| 495 | } |
| 496 | break; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 497 | default: |
| 498 | log.warn("Handling message type {} not yet implemented {}", |
| 499 | msg.getType(), msg); |
| 500 | } |
| 501 | } |
| 502 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 503 | private synchronized Collection<OFFlowStatsEntry> publishFlowStats(Dpid dpid, |
| 504 | OFFlowStatsReply reply) { |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 505 | //TODO: Get rid of synchronized |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 506 | fullFlowStats.putAll(dpid, reply.getEntries()); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 507 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 508 | return fullFlowStats.removeAll(dpid); |
| 509 | } |
| 510 | return null; |
| 511 | } |
| 512 | |
Srikanth Vavilapalli | 95810f5 | 2015-09-14 15:49:56 -0700 | [diff] [blame] | 513 | private synchronized Collection<OFTableStatsEntry> publishTableStats(Dpid dpid, |
| 514 | OFTableStatsReply reply) { |
| 515 | //TODO: Get rid of synchronized |
| 516 | fullTableStats.putAll(dpid, reply.getEntries()); |
| 517 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 518 | return fullTableStats.removeAll(dpid); |
| 519 | } |
| 520 | return null; |
| 521 | } |
| 522 | |
sangho | 6a0bb17 | 2015-02-05 12:24:48 -0800 | [diff] [blame] | 523 | private synchronized Collection<OFGroupStatsEntry> publishGroupStats(Dpid dpid, |
| 524 | OFGroupStatsReply reply) { |
| 525 | //TODO: Get rid of synchronized |
| 526 | fullGroupStats.putAll(dpid, reply.getEntries()); |
| 527 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 528 | return fullGroupStats.removeAll(dpid); |
| 529 | } |
| 530 | return null; |
| 531 | } |
| 532 | |
| 533 | private synchronized Collection<OFGroupDescStatsEntry> publishGroupDescStats(Dpid dpid, |
| 534 | OFGroupDescStatsReply reply) { |
| 535 | //TODO: Get rid of synchronized |
| 536 | fullGroupDescStats.putAll(dpid, reply.getEntries()); |
| 537 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 538 | return fullGroupDescStats.removeAll(dpid); |
alshabib | 64def64 | 2014-12-02 23:27:37 -0800 | [diff] [blame] | 539 | } |
| 540 | return null; |
| 541 | } |
| 542 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 543 | private synchronized Collection<OFPortStatsEntry> publishPortStats(Dpid dpid, |
| 544 | OFPortStatsReply reply) { |
| 545 | fullPortStats.putAll(dpid, reply.getEntries()); |
| 546 | if (!reply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
| 547 | return fullPortStats.removeAll(dpid); |
| 548 | } |
| 549 | return null; |
| 550 | } |
| 551 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 552 | @Override |
| 553 | public void setRole(Dpid dpid, RoleState role) { |
Yuta HIGUCHI | 79cbd1c | 2014-10-02 16:57:57 -0700 | [diff] [blame] | 554 | final OpenFlowSwitch sw = getSwitch(dpid); |
| 555 | if (sw == null) { |
| 556 | log.debug("Switch not connected. Ignoring setRole({}, {})", dpid, role); |
| 557 | return; |
| 558 | } |
| 559 | sw.setRole(role); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 562 | class InternalDeviceListener implements DeviceListener { |
| 563 | |
| 564 | @Override |
| 565 | public boolean isRelevant(DeviceEvent event) { |
Hyunsun Moon | e71494d | 2016-11-15 14:45:25 -0800 | [diff] [blame] | 566 | return event.subject().type() != CONTROLLER && event.type() == DEVICE_REMOVED; |
Yuta HIGUCHI | cedd0df | 2016-08-23 11:44:13 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | @Override |
| 570 | public void event(DeviceEvent event) { |
| 571 | switch (event.type()) { |
| 572 | case DEVICE_ADDED: |
| 573 | break; |
| 574 | case DEVICE_AVAILABILITY_CHANGED: |
| 575 | break; |
| 576 | case DEVICE_REMOVED: |
| 577 | // Device administratively removed, disconnect |
| 578 | Optional.ofNullable(getSwitch(dpid(event.subject().id().uri()))) |
| 579 | .ifPresent(OpenFlowSwitch::disconnectSwitch); |
| 580 | break; |
| 581 | case DEVICE_SUSPENDED: |
| 582 | break; |
| 583 | case DEVICE_UPDATED: |
| 584 | break; |
| 585 | case PORT_ADDED: |
| 586 | break; |
| 587 | case PORT_REMOVED: |
| 588 | break; |
| 589 | case PORT_STATS_UPDATED: |
| 590 | break; |
| 591 | case PORT_UPDATED: |
| 592 | break; |
| 593 | default: |
| 594 | break; |
| 595 | |
| 596 | } |
| 597 | |
| 598 | } |
| 599 | |
| 600 | } |
| 601 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 602 | /** |
| 603 | * Implementation of an OpenFlow Agent which is responsible for |
| 604 | * keeping track of connected switches and the state in which |
| 605 | * they are. |
| 606 | */ |
| 607 | public class OpenFlowSwitchAgent implements OpenFlowAgent { |
| 608 | |
| 609 | private final Logger log = LoggerFactory.getLogger(OpenFlowSwitchAgent.class); |
| 610 | private final Lock switchLock = new ReentrantLock(); |
| 611 | |
| 612 | @Override |
| 613 | public boolean addConnectedSwitch(Dpid dpid, OpenFlowSwitch sw) { |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 614 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 615 | if (connectedSwitches.get(dpid) != null) { |
| 616 | log.error("Trying to add connectedSwitch but found a previous " |
| 617 | + "value for dpid: {}", dpid); |
| 618 | return false; |
| 619 | } else { |
Yuta HIGUCHI | eb3f30b | 2014-10-22 11:34:49 -0700 | [diff] [blame] | 620 | log.info("Added switch {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 621 | connectedSwitches.put(dpid, sw); |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 622 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 623 | l.switchAdded(dpid); |
| 624 | } |
| 625 | return true; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | @Override |
| 630 | public boolean validActivation(Dpid dpid) { |
| 631 | if (connectedSwitches.get(dpid) == null) { |
| 632 | log.error("Trying to activate switch but is not in " |
| 633 | + "connected switches: dpid {}. Aborting ..", |
| 634 | dpid); |
| 635 | return false; |
| 636 | } |
| 637 | if (activeMasterSwitches.get(dpid) != null || |
| 638 | activeEqualSwitches.get(dpid) != null) { |
| 639 | log.error("Trying to activate switch but it is already " |
| 640 | + "activated: dpid {}. Found in activeMaster: {} " |
Ray Milkey | 6bc43c2 | 2015-11-06 13:22:38 -0800 | [diff] [blame] | 641 | + "Found in activeEqual: {}. Aborting ..", |
| 642 | dpid, |
| 643 | (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y', |
| 644 | (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 645 | return false; |
| 646 | } |
| 647 | return true; |
| 648 | } |
| 649 | |
| 650 | |
| 651 | @Override |
| 652 | public boolean addActivatedMasterSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 653 | switchLock.lock(); |
| 654 | try { |
| 655 | if (!validActivation(dpid)) { |
| 656 | return false; |
| 657 | } |
| 658 | activeMasterSwitches.put(dpid, sw); |
| 659 | return true; |
| 660 | } finally { |
| 661 | switchLock.unlock(); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | @Override |
| 666 | public boolean addActivatedEqualSwitch(Dpid dpid, OpenFlowSwitch sw) { |
| 667 | switchLock.lock(); |
| 668 | try { |
| 669 | if (!validActivation(dpid)) { |
| 670 | return false; |
| 671 | } |
| 672 | activeEqualSwitches.put(dpid, sw); |
| 673 | log.info("Added Activated EQUAL Switch {}", dpid); |
| 674 | return true; |
| 675 | } finally { |
| 676 | switchLock.unlock(); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | @Override |
| 681 | public void transitionToMasterSwitch(Dpid dpid) { |
| 682 | switchLock.lock(); |
| 683 | try { |
| 684 | if (activeMasterSwitches.containsKey(dpid)) { |
| 685 | return; |
| 686 | } |
| 687 | OpenFlowSwitch sw = activeEqualSwitches.remove(dpid); |
| 688 | if (sw == null) { |
| 689 | sw = getSwitch(dpid); |
| 690 | if (sw == null) { |
| 691 | log.error("Transition to master called on sw {}, but switch " |
| 692 | + "was not found in controller-cache", dpid); |
| 693 | return; |
| 694 | } |
| 695 | } |
| 696 | log.info("Transitioned switch {} to MASTER", dpid); |
| 697 | activeMasterSwitches.put(dpid, sw); |
| 698 | } finally { |
| 699 | switchLock.unlock(); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | |
| 704 | @Override |
| 705 | public void transitionToEqualSwitch(Dpid dpid) { |
| 706 | switchLock.lock(); |
| 707 | try { |
| 708 | if (activeEqualSwitches.containsKey(dpid)) { |
| 709 | return; |
| 710 | } |
| 711 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 712 | if (sw == null) { |
| 713 | sw = getSwitch(dpid); |
| 714 | if (sw == null) { |
| 715 | log.error("Transition to equal called on sw {}, but switch " |
| 716 | + "was not found in controller-cache", dpid); |
| 717 | return; |
| 718 | } |
| 719 | } |
| 720 | log.info("Transitioned switch {} to EQUAL", dpid); |
| 721 | activeEqualSwitches.put(dpid, sw); |
| 722 | } finally { |
| 723 | switchLock.unlock(); |
| 724 | } |
| 725 | |
| 726 | } |
| 727 | |
| 728 | @Override |
| 729 | public void removeConnectedSwitch(Dpid dpid) { |
| 730 | connectedSwitches.remove(dpid); |
| 731 | OpenFlowSwitch sw = activeMasterSwitches.remove(dpid); |
| 732 | if (sw == null) { |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame] | 733 | log.debug("sw was null for {}", dpid); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 734 | sw = activeEqualSwitches.remove(dpid); |
| 735 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 736 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 737 | l.switchRemoved(dpid); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | @Override |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 742 | public void processDownstreamMessage(Dpid dpid, List<OFMessage> m) { |
| 743 | for (OpenFlowMessageListener listener : ofMessageListener) { |
| 744 | listener.handleOutgoingMessage(dpid, m); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | |
| 749 | @Override |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 750 | public void processMessage(Dpid dpid, OFMessage m) { |
| 751 | processPacket(dpid, m); |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 752 | |
| 753 | for (OpenFlowMessageListener listener : ofMessageListener) { |
| 754 | listener.handleIncomingMessage(dpid, m); |
| 755 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 756 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 757 | |
| 758 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 759 | public void returnRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 760 | for (OpenFlowSwitchListener l : ofSwitchListener) { |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 761 | l.receivedRoleReply(dpid, requested, response); |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 766 | /** |
Jian Li | 2266bff | 2016-04-21 11:01:25 -0700 | [diff] [blame] | 767 | * OpenFlow message handler. |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 768 | */ |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 769 | protected final class OFMessageHandler implements Runnable { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 770 | |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 771 | protected final OFMessage msg; |
| 772 | protected final Dpid dpid; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 773 | |
| 774 | public OFMessageHandler(Dpid dpid, OFMessage msg) { |
| 775 | this.msg = msg; |
| 776 | this.dpid = dpid; |
| 777 | } |
| 778 | |
| 779 | @Override |
| 780 | public void run() { |
alshabib | eec3a06 | 2014-09-17 18:01:26 -0700 | [diff] [blame] | 781 | for (OpenFlowEventListener listener : ofEventListener) { |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 782 | listener.handleMessage(dpid, msg); |
| 783 | } |
| 784 | } |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 785 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 786 | } |