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