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