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