HIGUCHI Yuta | ea60e5f | 2013-06-12 11:10:21 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.bgproute; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 2 | |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 3 | import java.io.File; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 4 | import java.io.IOException; |
| 5 | import java.net.InetAddress; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 6 | import java.util.ArrayList; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 7 | import java.util.Collection; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 8 | import java.util.HashMap; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 9 | import java.util.Iterator; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 10 | import java.util.List; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 11 | import java.util.Map; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 12 | import java.util.Set; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 13 | import java.util.concurrent.BlockingQueue; |
| 14 | import java.util.concurrent.ExecutorService; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 15 | import java.util.concurrent.Executors; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 16 | import java.util.concurrent.LinkedBlockingQueue; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 17 | import java.util.concurrent.ScheduledExecutorService; |
| 18 | import java.util.concurrent.TimeUnit; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 19 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 20 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 21 | import net.floodlightcontroller.core.IOFSwitch; |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 22 | import net.floodlightcontroller.core.IOFSwitchListener; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 23 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 24 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 25 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 26 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 27 | import net.floodlightcontroller.core.util.SingletonTask; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 28 | import net.floodlightcontroller.devicemanager.IDeviceService; |
| 29 | import net.floodlightcontroller.packet.Ethernet; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 30 | import net.floodlightcontroller.packet.IPv4; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 31 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 32 | import net.floodlightcontroller.routing.Link; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 33 | import net.floodlightcontroller.topology.ITopologyListener; |
| 34 | import net.floodlightcontroller.topology.ITopologyService; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 35 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 36 | import net.onrc.onos.ofcontroller.bgproute.RibUpdate.Operation; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 37 | import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoLinkService; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 38 | import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoRouteService; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 39 | import net.onrc.onos.ofcontroller.core.internal.TopoLinkServiceImpl; |
HIGUCHI Yuta | a56fbde | 2013-06-17 14:26:05 -0700 | [diff] [blame] | 40 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscovery; |
| 41 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscovery.LDUpdate; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 42 | import net.onrc.onos.ofcontroller.proxyarp.IArpRequester; |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 43 | import net.onrc.onos.ofcontroller.proxyarp.ProxyArpManager; |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 44 | import net.onrc.onos.ofcontroller.routing.TopoRouteService; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 45 | import net.onrc.onos.ofcontroller.util.DataPath; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 46 | import net.onrc.onos.ofcontroller.util.Dpid; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 47 | import net.onrc.onos.ofcontroller.util.FlowEntry; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 48 | import net.onrc.onos.ofcontroller.util.Port; |
| 49 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 50 | import net.sf.json.JSONArray; |
| 51 | import net.sf.json.JSONObject; |
| 52 | import net.sf.json.JSONSerializer; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 53 | |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 54 | import org.codehaus.jackson.JsonParseException; |
| 55 | import org.codehaus.jackson.map.JsonMappingException; |
| 56 | import org.codehaus.jackson.map.ObjectMapper; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 57 | import org.openflow.protocol.OFFlowMod; |
| 58 | import org.openflow.protocol.OFMatch; |
| 59 | import org.openflow.protocol.OFMessage; |
| 60 | import org.openflow.protocol.OFPacketOut; |
Jonathan Hart | 9575cb6 | 2013-07-05 13:43:49 +1200 | [diff] [blame] | 61 | import org.openflow.protocol.OFPort; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 62 | import org.openflow.protocol.OFType; |
| 63 | import org.openflow.protocol.action.OFAction; |
| 64 | import org.openflow.protocol.action.OFActionDataLayerDestination; |
| 65 | import org.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 66 | import org.openflow.util.HexString; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 67 | import org.slf4j.Logger; |
| 68 | import org.slf4j.LoggerFactory; |
| 69 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 70 | import com.google.common.collect.HashMultimap; |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 71 | import com.google.common.collect.Multimap; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 72 | import com.google.common.collect.Multimaps; |
| 73 | import com.google.common.collect.SetMultimap; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 74 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 75 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 76 | public class BgpRoute implements IFloodlightModule, IBgpRouteService, |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 77 | ITopologyListener, IOFSwitchListener, |
| 78 | IArpRequester { |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 79 | |
| 80 | protected static Logger log = LoggerFactory.getLogger(BgpRoute.class); |
| 81 | |
| 82 | protected IFloodlightProviderService floodlightProvider; |
| 83 | protected ITopologyService topology; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 84 | protected ITopoRouteService topoRouteService; |
| 85 | protected IDeviceService devices; |
| 86 | protected IRestApiService restApi; |
| 87 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 88 | protected ProxyArpManager proxyArp; |
| 89 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 90 | //protected static Ptree ptree; |
Jonathan Hart | 29b972d | 2013-08-12 23:43:51 +1200 | [diff] [blame] | 91 | protected IPatriciaTrie<RibEntry> ptree; |
Jonathan Hart | abf1022 | 2013-08-13 10:19:34 +1200 | [diff] [blame^] | 92 | protected IPatriciaTrie<Interface> interfacePtrie; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 93 | protected BlockingQueue<RibUpdate> ribUpdates; |
| 94 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 95 | protected String bgpdRestIp; |
| 96 | protected String routerId; |
Jonathan Hart | 9575cb6 | 2013-07-05 13:43:49 +1200 | [diff] [blame] | 97 | protected String configFilename = "config.json"; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 98 | |
| 99 | //We need to identify our flows somehow. But like it says in LearningSwitch.java, |
| 100 | //the controller/OS should hand out cookie IDs to prevent conflicts. |
| 101 | protected final long APP_COOKIE = 0xa0000000000000L; |
| 102 | //Cookie for flows that do L2 forwarding within SDN domain to egress routers |
| 103 | protected final long L2_FWD_COOKIE = APP_COOKIE + 1; |
| 104 | //Cookie for flows in ingress switches that rewrite the MAC address |
| 105 | protected final long MAC_RW_COOKIE = APP_COOKIE + 2; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 106 | //Cookie for flows that setup BGP paths |
| 107 | protected final long BGP_COOKIE = APP_COOKIE + 3; |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 108 | //Forwarding uses priority 0, and the mac rewrite entries in ingress switches |
| 109 | //need to be higher priority than this otherwise the rewrite may not get done |
| 110 | protected final short SDNIP_PRIORITY = 10; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 111 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 112 | protected final short BGP_PORT = 179; |
| 113 | |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 114 | protected final int TOPO_DETECTION_WAIT = 2; //seconds |
| 115 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 116 | //Configuration stuff |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 117 | protected List<String> switches; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 118 | protected Map<String, Interface> interfaces; |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 119 | protected Map<InetAddress, BgpPeer> bgpPeers; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 120 | protected SwitchPort bgpdAttachmentPoint; |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 121 | |
| 122 | //True when all switches have connected |
| 123 | protected volatile boolean switchesConnected = false; |
| 124 | //True when we have a full mesh of shortest paths between gateways |
| 125 | protected volatile boolean topologyReady = false; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 126 | |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 127 | protected ArrayList<LDUpdate> linkUpdates; |
| 128 | protected SingletonTask topologyChangeDetectorTask; |
| 129 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 130 | protected SetMultimap<InetAddress, RibUpdate> prefixesWaitingOnArp; |
| 131 | protected SetMultimap<InetAddress, PathUpdate> pathsWaitingOnArp; |
| 132 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 133 | protected ExecutorService bgpUpdatesExecutor; |
| 134 | |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 135 | protected Multimap<Prefix, PushedFlowMod> pushedFlows; |
| 136 | |
| 137 | private class PushedFlowMod { |
| 138 | private long dpid; |
| 139 | private OFFlowMod flowMod; |
| 140 | |
| 141 | public PushedFlowMod(long dpid, OFFlowMod flowMod) { |
| 142 | this.dpid = dpid; |
| 143 | this.flowMod = flowMod; |
| 144 | } |
| 145 | |
| 146 | public long getDpid() { |
| 147 | return dpid; |
| 148 | } |
| 149 | |
| 150 | public OFFlowMod getFlowMod() { |
| 151 | return flowMod; |
| 152 | } |
| 153 | } |
| 154 | |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 155 | protected class TopologyChangeDetector implements Runnable { |
| 156 | @Override |
| 157 | public void run() { |
| 158 | log.debug("Running topology change detection task"); |
| 159 | synchronized (linkUpdates) { |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 160 | //This is the model the REST API uses to retrieve network graph info |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 161 | ITopoLinkService topoLinkService = new TopoLinkServiceImpl(); |
| 162 | |
| 163 | List<Link> activeLinks = topoLinkService.getActiveLinks(); |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 164 | |
| 165 | Iterator<LDUpdate> it = linkUpdates.iterator(); |
| 166 | while (it.hasNext()){ |
| 167 | LDUpdate ldu = it.next(); |
| 168 | Link l = new Link(ldu.getSrc(), ldu.getSrcPort(), |
| 169 | ldu.getDst(), ldu.getDstPort()); |
| 170 | |
| 171 | if (activeLinks.contains(l)){ |
| 172 | log.debug("Not found: {}", l); |
| 173 | it.remove(); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (linkUpdates.isEmpty()){ |
| 179 | //All updates have been seen in network map. |
| 180 | //We can check if topology is ready |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 181 | log.debug("No known changes outstanding. Checking topology now"); |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 182 | checkStatus(); |
| 183 | } |
| 184 | else { |
| 185 | //We know of some link updates that haven't propagated to the database yet |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 186 | log.debug("Some changes not found in network map - {} links missing", linkUpdates.size()); |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 187 | topologyChangeDetectorTask.reschedule(TOPO_DETECTION_WAIT, TimeUnit.SECONDS); |
| 188 | } |
| 189 | } |
| 190 | } |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 191 | |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 192 | private void readGatewaysConfiguration(String gatewaysFilename){ |
| 193 | File gatewaysFile = new File(gatewaysFilename); |
| 194 | ObjectMapper mapper = new ObjectMapper(); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 195 | |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 196 | try { |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 197 | Configuration config = mapper.readValue(gatewaysFile, Configuration.class); |
| 198 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 199 | switches = config.getSwitches(); |
Jonathan Hart | 9575cb6 | 2013-07-05 13:43:49 +1200 | [diff] [blame] | 200 | interfaces = new HashMap<String, Interface>(); |
| 201 | for (Interface intf : config.getInterfaces()){ |
| 202 | interfaces.put(intf.getName(), intf); |
| 203 | } |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 204 | bgpPeers = new HashMap<InetAddress, BgpPeer>(); |
| 205 | for (BgpPeer peer : config.getPeers()){ |
| 206 | bgpPeers.put(peer.getIpAddress(), peer); |
| 207 | } |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 208 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 209 | bgpdAttachmentPoint = new SwitchPort( |
| 210 | new Dpid(config.getBgpdAttachmentDpid()), |
| 211 | new Port(config.getBgpdAttachmentPort())); |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 212 | |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 213 | } catch (JsonParseException e) { |
| 214 | log.error("Error in JSON file", e); |
| 215 | System.exit(1); |
| 216 | } catch (JsonMappingException e) { |
| 217 | log.error("Error in JSON file", e); |
| 218 | System.exit(1); |
| 219 | } catch (IOException e) { |
| 220 | log.error("Error reading JSON file", e); |
| 221 | System.exit(1); |
| 222 | } |
Jonathan Hart | abf1022 | 2013-08-13 10:19:34 +1200 | [diff] [blame^] | 223 | |
| 224 | //Populate the interface Patricia Trie |
| 225 | for (Interface intf : interfaces.values()) { |
| 226 | Prefix prefix = new Prefix(intf.getIpAddress().getAddress(), intf.getPrefixLength()); |
| 227 | interfacePtrie.put(prefix, intf); |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | Iterator<IPatriciaTrie.Entry<Interface>> it = interfacePtrie.iterator(); |
| 232 | while (it.hasNext()) { |
| 233 | IPatriciaTrie.Entry<Interface> entry = it.next(); |
| 234 | Interface intf = entry.getValue(); |
| 235 | log.debug("Interface at prefix {}, switchport {}/{}", |
| 236 | new Object[] {entry.getPrefix(), HexString.toHexString(intf.getDpid()), intf.getPort()}); |
| 237 | } |
| 238 | */ |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 239 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 240 | |
| 241 | @Override |
| 242 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 243 | Collection<Class<? extends IFloodlightService>> l |
| 244 | = new ArrayList<Class<? extends IFloodlightService>>(); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 245 | l.add(IBgpRouteService.class); |
| 246 | return l; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 251 | Map<Class<? extends IFloodlightService>, IFloodlightService> m |
| 252 | = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 253 | m.put(IBgpRouteService.class, this); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 254 | return m; |
| 255 | } |
| 256 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 257 | @Override |
| 258 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 259 | Collection<Class<? extends IFloodlightService>> l |
| 260 | = new ArrayList<Class<? extends IFloodlightService>>(); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 261 | l.add(IFloodlightProviderService.class); |
| 262 | l.add(ITopologyService.class); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 263 | l.add(IDeviceService.class); |
| 264 | l.add(IRestApiService.class); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 265 | return l; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public void init(FloodlightModuleContext context) |
| 270 | throws FloodlightModuleException { |
| 271 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 272 | //ptree = new Ptree(32); |
Jonathan Hart | 29b972d | 2013-08-12 23:43:51 +1200 | [diff] [blame] | 273 | ptree = new PatriciaTrie<RibEntry>(32); |
Jonathan Hart | abf1022 | 2013-08-13 10:19:34 +1200 | [diff] [blame^] | 274 | interfacePtrie = new PatriciaTrie<Interface>(32); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 275 | |
| 276 | ribUpdates = new LinkedBlockingQueue<RibUpdate>(); |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 277 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 278 | // Register floodlight provider and REST handler. |
| 279 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 280 | topology = context.getServiceImpl(ITopologyService.class); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 281 | devices = context.getServiceImpl(IDeviceService.class); |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 282 | restApi = context.getServiceImpl(IRestApiService.class); |
| 283 | |
| 284 | //TODO We'll initialise this here for now, but it should really be done as |
| 285 | //part of the controller core |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 286 | proxyArp = new ProxyArpManager(floodlightProvider, topology, devices); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 287 | |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 288 | linkUpdates = new ArrayList<LDUpdate>(); |
| 289 | ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); |
| 290 | topologyChangeDetectorTask = new SingletonTask(executor, new TopologyChangeDetector()); |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 291 | |
| 292 | topoRouteService = new TopoRouteService(""); |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 293 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 294 | pathsWaitingOnArp = Multimaps.synchronizedSetMultimap( |
| 295 | HashMultimap.<InetAddress, PathUpdate>create()); |
| 296 | prefixesWaitingOnArp = Multimaps.synchronizedSetMultimap( |
| 297 | HashMultimap.<InetAddress, RibUpdate>create()); |
| 298 | |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 299 | pushedFlows = HashMultimap.<Prefix, PushedFlowMod>create(); |
| 300 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 301 | bgpUpdatesExecutor = Executors.newSingleThreadExecutor( |
| 302 | new ThreadFactoryBuilder().setNameFormat("bgp-updates-%d").build()); |
| 303 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 304 | //Read in config values |
| 305 | bgpdRestIp = context.getConfigParams(this).get("BgpdRestIp"); |
| 306 | if (bgpdRestIp == null){ |
| 307 | log.error("BgpdRestIp property not found in config file"); |
| 308 | System.exit(1); |
| 309 | } |
| 310 | else { |
| 311 | log.info("BgpdRestIp set to {}", bgpdRestIp); |
| 312 | } |
| 313 | |
| 314 | routerId = context.getConfigParams(this).get("RouterId"); |
| 315 | if (routerId == null){ |
| 316 | log.error("RouterId property not found in config file"); |
| 317 | System.exit(1); |
| 318 | } |
| 319 | else { |
| 320 | log.info("RouterId set to {}", routerId); |
| 321 | } |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 322 | |
Jonathan Hart | 9575cb6 | 2013-07-05 13:43:49 +1200 | [diff] [blame] | 323 | String configFilenameParameter = context.getConfigParams(this).get("configfile"); |
| 324 | if (configFilenameParameter != null){ |
| 325 | configFilename = configFilenameParameter; |
| 326 | } |
| 327 | log.debug("Config file set to {}", configFilename); |
| 328 | |
| 329 | readGatewaysConfiguration(configFilename); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 330 | // Test. |
| 331 | //test(); |
| 332 | } |
| 333 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 334 | //public Ptree getPtree() { |
Jonathan Hart | 29b972d | 2013-08-12 23:43:51 +1200 | [diff] [blame] | 335 | public IPatriciaTrie<RibEntry> getPtree() { |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 336 | return ptree; |
| 337 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 338 | |
| 339 | public void clearPtree() { |
| 340 | //ptree = null; |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 341 | //ptree = new Ptree(32); |
Jonathan Hart | 29b972d | 2013-08-12 23:43:51 +1200 | [diff] [blame] | 342 | ptree = new PatriciaTrie<RibEntry>(32); |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 343 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 344 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 345 | public String getBGPdRestIp() { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 346 | return bgpdRestIp; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 347 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 348 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 349 | public String getRouterId() { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 350 | return routerId; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 351 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 352 | |
| 353 | // Return nexthop address as byte array. |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 354 | /* |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 355 | public RibEntry lookupRib(byte[] dest) { |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 356 | if (ptree == null) { |
| 357 | log.debug("lookupRib: ptree null"); |
| 358 | return null; |
| 359 | } |
| 360 | |
| 361 | PtreeNode node = ptree.match(dest, 32); |
| 362 | if (node == null) { |
| 363 | log.debug("lookupRib: ptree node null"); |
| 364 | return null; |
| 365 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 366 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 367 | if (node.rib == null) { |
| 368 | log.debug("lookupRib: ptree rib null"); |
| 369 | return null; |
| 370 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 371 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 372 | ptree.delReference(node); |
| 373 | |
| 374 | return node.rib; |
| 375 | } |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 376 | */ |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 377 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 378 | /* |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 379 | //TODO looks like this should be a unit test |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 380 | @SuppressWarnings("unused") |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 381 | private void test() throws UnknownHostException { |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 382 | System.out.println("Here it is"); |
| 383 | Prefix p = new Prefix("128.0.0.0", 8); |
| 384 | Prefix q = new Prefix("8.0.0.0", 8); |
| 385 | Prefix r = new Prefix("10.0.0.0", 24); |
| 386 | Prefix a = new Prefix("10.0.0.1", 32); |
| 387 | |
Jonathan Hart | d1b9d87 | 2013-07-23 12:17:21 +1200 | [diff] [blame] | 388 | ptree.acquire(p.getAddress(), p.getPrefixLength()); |
| 389 | ptree.acquire(q.getAddress(), q.getPrefixLength()); |
| 390 | ptree.acquire(r.getAddress(), r.getPrefixLength()); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 391 | |
| 392 | System.out.println("Traverse start"); |
| 393 | for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) { |
| 394 | Prefix p_result = new Prefix(node.key, node.keyBits); |
| 395 | } |
| 396 | |
Jonathan Hart | d1b9d87 | 2013-07-23 12:17:21 +1200 | [diff] [blame] | 397 | PtreeNode n = ptree.match(a.getAddress(), a.getPrefixLength()); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 398 | if (n != null) { |
| 399 | System.out.println("Matched prefix for 10.0.0.1:"); |
| 400 | Prefix x = new Prefix(n.key, n.keyBits); |
| 401 | ptree.delReference(n); |
| 402 | } |
| 403 | |
Jonathan Hart | d1b9d87 | 2013-07-23 12:17:21 +1200 | [diff] [blame] | 404 | n = ptree.lookup(p.getAddress(), p.getPrefixLength()); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 405 | if (n != null) { |
| 406 | ptree.delReference(n); |
| 407 | ptree.delReference(n); |
| 408 | } |
| 409 | System.out.println("Traverse start"); |
| 410 | for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) { |
| 411 | Prefix p_result = new Prefix(node.key, node.keyBits); |
| 412 | } |
| 413 | |
Jonathan Hart | d1b9d87 | 2013-07-23 12:17:21 +1200 | [diff] [blame] | 414 | n = ptree.lookup(q.getAddress(), q.getPrefixLength()); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 415 | if (n != null) { |
| 416 | ptree.delReference(n); |
| 417 | ptree.delReference(n); |
| 418 | } |
| 419 | System.out.println("Traverse start"); |
| 420 | for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) { |
| 421 | Prefix p_result = new Prefix(node.key, node.keyBits); |
| 422 | } |
| 423 | |
Jonathan Hart | d1b9d87 | 2013-07-23 12:17:21 +1200 | [diff] [blame] | 424 | n = ptree.lookup(r.getAddress(), r.getPrefixLength()); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 425 | if (n != null) { |
| 426 | ptree.delReference(n); |
| 427 | ptree.delReference(n); |
| 428 | } |
| 429 | System.out.println("Traverse start"); |
| 430 | for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) { |
| 431 | Prefix p_result = new Prefix(node.key, node.keyBits); |
| 432 | } |
| 433 | |
| 434 | } |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 435 | */ |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 436 | |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 437 | //TODO once the Ptree is object oriented this can go |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 438 | /* |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 439 | private String getPrefixFromPtree(PtreeNode node){ |
| 440 | InetAddress address = null; |
| 441 | try { |
| 442 | address = InetAddress.getByAddress(node.key); |
| 443 | } catch (UnknownHostException e1) { |
| 444 | //Should never happen is the reverse conversion has already been done |
| 445 | log.error("Malformed IP address"); |
| 446 | return ""; |
| 447 | } |
| 448 | return address.toString() + "/" + node.rib.masklen; |
| 449 | } |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 450 | */ |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 451 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 452 | private void retrieveRib(){ |
| 453 | String url = "http://" + bgpdRestIp + "/wm/bgp/" + routerId; |
| 454 | String response = RestClient.get(url); |
| 455 | |
| 456 | if (response.equals("")){ |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | response = response.replaceAll("\"", "'"); |
| 461 | JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON(response); |
| 462 | JSONArray rib_json_array = jsonObj.getJSONArray("rib"); |
| 463 | String router_id = jsonObj.getString("router-id"); |
| 464 | |
| 465 | int size = rib_json_array.size(); |
| 466 | |
| 467 | log.info("Retrived RIB of {} entries from BGPd", size); |
| 468 | |
| 469 | for (int j = 0; j < size; j++) { |
| 470 | JSONObject second_json_object = rib_json_array.getJSONObject(j); |
| 471 | String prefix = second_json_object.getString("prefix"); |
| 472 | String nexthop = second_json_object.getString("nexthop"); |
| 473 | |
| 474 | //insert each rib entry into the local rib; |
| 475 | String[] substring = prefix.split("/"); |
| 476 | String prefix1 = substring[0]; |
| 477 | String mask1 = substring[1]; |
| 478 | |
| 479 | Prefix p; |
| 480 | try { |
| 481 | p = new Prefix(prefix1, Integer.valueOf(mask1)); |
| 482 | } catch (NumberFormatException e) { |
| 483 | log.warn("Wrong mask format in RIB JSON: {}", mask1); |
| 484 | continue; |
Jonathan Hart | 32e1822 | 2013-08-07 22:05:42 +1200 | [diff] [blame] | 485 | } catch (IllegalArgumentException e1) { |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 486 | log.warn("Wrong prefix format in RIB JSON: {}", prefix1); |
| 487 | continue; |
| 488 | } |
| 489 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 490 | //PtreeNode node = ptree.acquire(p.getAddress(), p.getPrefixLength()); |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 491 | RibEntry rib = new RibEntry(router_id, nexthop); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 492 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 493 | /* |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 494 | if (node.rib != null) { |
| 495 | node.rib = null; |
| 496 | ptree.delReference(node); |
| 497 | } |
| 498 | |
| 499 | node.rib = rib; |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 500 | */ |
| 501 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 502 | //ptree.put(p, rib); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 503 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 504 | //addPrefixFlows(p, rib); |
| 505 | try { |
| 506 | ribUpdates.put(new RibUpdate(Operation.UPDATE, p, rib)); |
| 507 | } catch (InterruptedException e) { |
| 508 | log.debug("Interrupted while pushing onto update queue"); |
| 509 | } |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 513 | @Override |
| 514 | public void newRibUpdate(RibUpdate update) { |
Jonathan Hart | 9ea3121 | 2013-08-12 21:40:34 +1200 | [diff] [blame] | 515 | try { |
| 516 | ribUpdates.put(update); |
| 517 | } catch (InterruptedException e) { |
| 518 | // TODO Auto-generated catch block |
| 519 | log.debug(" ", e); |
| 520 | } |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 521 | } |
| 522 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 523 | public synchronized void processRibAdd(RibUpdate update) { |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 524 | Prefix prefix = update.getPrefix(); |
| 525 | |
Jonathan Hart | 9ea3121 | 2013-08-12 21:40:34 +1200 | [diff] [blame] | 526 | log.debug("Processing prefix add {}", prefix); |
| 527 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 528 | //PtreeNode node = ptree.acquire(prefix.getAddress(), prefix.getPrefixLength()); |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 529 | RibEntry rib = ptree.put(prefix, update.getRibEntry()); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 530 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 531 | //if (node.rib != null) { |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 532 | if (rib != null && !rib.equals(update.getRibEntry())) { |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 533 | //There was an existing nexthop for this prefix. This update supersedes that, |
| 534 | //so we need to remove the old flows for this prefix from the switches |
| 535 | deletePrefixFlows(prefix); |
| 536 | |
| 537 | //Then remove the old nexthop from the Ptree |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 538 | //node.rib = null; |
| 539 | //ptree.delReference(node); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 540 | } |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 541 | |
| 542 | //Put the new nexthop in the Ptree |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 543 | //node.rib = update.getRibEntry(); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 544 | |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 545 | //Push flows for the new <prefix, nexthop> |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 546 | addPrefixFlows(prefix, update.getRibEntry()); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 547 | } |
| 548 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 549 | public synchronized void processRibDelete(RibUpdate update) { |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 550 | Prefix prefix = update.getPrefix(); |
| 551 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 552 | //PtreeNode node = ptree.lookup(prefix.getAddress(), prefix.getPrefixLength()); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 553 | |
| 554 | /* |
| 555 | * Remove the flows from the switches before the rib is lost |
| 556 | * Theory: we could get a delete for a prefix not in the Ptree. |
| 557 | * This would result in a null node being returned. We could get a delete for |
| 558 | * a node that's not actually there, but is a aggregate node. This would result |
| 559 | * in a non-null node with a null rib. Only a non-null node with a non-null |
| 560 | * rib is an actual prefix in the Ptree. |
| 561 | */ |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 562 | |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 563 | /* |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 564 | if (node != null && node.rib != null) { |
| 565 | if (update.getRibEntry().equals(node.rib)) { |
| 566 | node.rib = null; |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 567 | ptree.delReference(node); |
| 568 | |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 569 | deletePrefixFlows(update.getPrefix()); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 570 | } |
| 571 | } |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 572 | */ |
| 573 | |
| 574 | if (ptree.remove(prefix, update.getRibEntry())) { |
| 575 | /* |
| 576 | * Only delete flows if an entry was actually removed from the trie. |
| 577 | * If no entry was removed, the <prefix, nexthop> wasn't there so |
| 578 | * it's probably already been removed and we don't need to do anything |
| 579 | */ |
| 580 | deletePrefixFlows(prefix); |
| 581 | } |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 582 | } |
| 583 | |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 584 | //TODO compatibility layer, used by beginRouting() |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 585 | /*public void prefixAdded(PtreeNode node) { |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 586 | Prefix prefix = null; |
| 587 | try { |
| 588 | prefix = new Prefix(node.key, node.rib.masklen); |
Jonathan Hart | 32e1822 | 2013-08-07 22:05:42 +1200 | [diff] [blame] | 589 | } catch (IllegalArgumentException e) { |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 590 | log.error(" ", e); |
| 591 | } |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 592 | |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 593 | addPrefixFlows(prefix, node.rib); |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 594 | }*/ |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 595 | |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 596 | private void addPrefixFlows(Prefix prefix, RibEntry rib) { |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 597 | if (!topologyReady){ |
| 598 | return; |
| 599 | } |
| 600 | |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 601 | //TODO before we do anything, we have to check that the RIB entry is still in the |
| 602 | //Ptree because it could have been removed while we were waiting for ARP. |
| 603 | //I think we'll have to make prefixAdded and prefixDelete atomic as well |
| 604 | //to protect against the prefix getting deleted while where trying to add it |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 605 | |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 606 | log.debug("New prefix {} added, next hop {}", |
| 607 | prefix, rib.getNextHop().getHostAddress()); |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 608 | |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 609 | //TODO this is wrong, we shouldn't be dealing with BGP peers here. |
Jonathan Hart | 6261dcd | 2013-07-22 17:58:35 +1200 | [diff] [blame] | 610 | //We need to figure out where the device is attached and what its |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 611 | //mac address is by learning. |
| 612 | //The next hop is not necessarily the peer, and the peer's attachment |
| 613 | //point is not necessarily the next hop's attachment point. |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 614 | BgpPeer peer = bgpPeers.get(rib.getNextHop()); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 615 | |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 616 | if (peer == null){ |
| 617 | //TODO local router isn't in peers list so this will get thrown |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 618 | //Need to work out what to do about local prefixes with next hop 0.0.0.0. |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 619 | |
| 620 | //The other scenario is this is a route server route. In that |
| 621 | //case the next hop is not in our configuration |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 622 | log.error("Couldn't find next hop router in router {} in config", |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 623 | rib.getNextHop().getHostAddress()); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 624 | return; //just quit out here? This is probably a configuration error |
| 625 | } |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 626 | |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 627 | //Get MAC address for peer from the ARP module |
| 628 | //TODO separate out the 'ask for MAC' bit to another method |
| 629 | byte[] peerMacAddress = proxyArp.getMacAddress(peer.getIpAddress()); |
| 630 | if (peerMacAddress == null) { |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 631 | //A RibUpdate is still a nice way to package them up |
| 632 | prefixesWaitingOnArp.put(peer.getIpAddress(), |
| 633 | new RibUpdate(Operation.UPDATE, prefix, rib)); |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 634 | proxyArp.sendArpRequest(peer.getIpAddress(), this, true); |
| 635 | return; |
| 636 | } |
| 637 | |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 638 | Interface peerInterface = interfaces.get(peer.getInterfaceName()); |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 639 | |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 640 | //Add a flow to rewrite mac for this prefix to all border switches |
| 641 | for (Interface srcInterface : interfaces.values()) { |
| 642 | if (srcInterface == peerInterface) { |
| 643 | //Don't push a flow for the switch where this peer is attached |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 644 | continue; |
| 645 | } |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 646 | |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 647 | DataPath shortestPath = topoRouteService.getShortestPath( |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 648 | srcInterface.getSwitchPort(), |
| 649 | peerInterface.getSwitchPort()); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 650 | |
| 651 | if (shortestPath == null){ |
| 652 | log.debug("Shortest path between {} and {} not found", |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 653 | srcInterface.getSwitchPort(), |
| 654 | peerInterface.getSwitchPort()); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 655 | return; // just quit here? |
| 656 | } |
| 657 | |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 658 | //Set up the flow mod |
| 659 | OFFlowMod fm = |
| 660 | (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 661 | .getMessage(OFType.FLOW_MOD); |
| 662 | |
| 663 | fm.setIdleTimeout((short)0) |
| 664 | .setHardTimeout((short)0) |
| 665 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 666 | .setCookie(MAC_RW_COOKIE) |
| 667 | .setCommand(OFFlowMod.OFPFC_ADD) |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 668 | .setPriority(SDNIP_PRIORITY) |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 669 | .setLengthU(OFFlowMod.MINIMUM_LENGTH |
| 670 | + OFActionDataLayerDestination.MINIMUM_LENGTH |
| 671 | + OFActionOutput.MINIMUM_LENGTH); |
| 672 | |
| 673 | OFMatch match = new OFMatch(); |
| 674 | match.setDataLayerType(Ethernet.TYPE_IPv4); |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 675 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 676 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 677 | /* |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 678 | InetAddress address = null; |
| 679 | try { |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 680 | address = InetAddress.getByAddress(prefix.getAddress()); |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 681 | } catch (UnknownHostException e1) { |
| 682 | //Should never happen is the reverse conversion has already been done |
| 683 | log.error("Malformed IP address"); |
| 684 | return; |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 685 | }*/ |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 686 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 687 | //match.setFromCIDR(address.getHostAddress() + "/" + |
| 688 | // prefix.getPrefixLength(), OFMatch.STR_NW_DST); |
| 689 | match.setFromCIDR(prefix.toString(), OFMatch.STR_NW_DST); |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 690 | fm.setMatch(match); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 691 | |
| 692 | //Set up MAC rewrite action |
| 693 | OFActionDataLayerDestination macRewriteAction = new OFActionDataLayerDestination(); |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 694 | //TODO the peer's mac address is not necessarily the next hop's... |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 695 | macRewriteAction.setDataLayerAddress(peerMacAddress); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 696 | |
| 697 | //Set up output action |
| 698 | OFActionOutput outputAction = new OFActionOutput(); |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 699 | outputAction.setMaxLength((short)0xffff); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 700 | |
Jonathan Hart | 50a8d1e | 2013-06-06 16:00:47 +1200 | [diff] [blame] | 701 | Port outputPort = shortestPath.flowEntries().get(0).outPort(); |
| 702 | outputAction.setPort(outputPort.value()); |
| 703 | |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 704 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 705 | actions.add(macRewriteAction); |
| 706 | actions.add(outputAction); |
| 707 | fm.setActions(actions); |
| 708 | |
| 709 | //Write to switch |
| 710 | IOFSwitch sw = floodlightProvider.getSwitches() |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 711 | .get(srcInterface.getDpid()); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 712 | |
| 713 | if (sw == null){ |
| 714 | log.warn("Switch not found when pushing flow mod"); |
| 715 | continue; |
| 716 | } |
| 717 | |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 718 | //TODO if prefix Added/Deleted are synchronized this shouldn't have to be |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 719 | pushedFlows.put(prefix, new PushedFlowMod(sw.getId(), fm)); |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 720 | |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 721 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 722 | msglist.add(fm); |
| 723 | try { |
| 724 | sw.write(msglist, null); |
| 725 | sw.flush(); |
| 726 | } catch (IOException e) { |
| 727 | log.error("Failure writing flow mod", e); |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 732 | //TODO test next-hop changes |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 733 | //TODO check delete/add synchronization |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 734 | |
| 735 | private void deletePrefixFlows(Prefix prefix) { |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 736 | if (!topologyReady) { |
| 737 | return; |
| 738 | } |
| 739 | |
Jonathan Hart | df6ec33 | 2013-08-04 01:37:14 +1200 | [diff] [blame] | 740 | log.debug("In deletePrefixFlows for {}", prefix); |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 741 | |
| 742 | /*for (Map.Entry<Prefix, PushedFlowMod> entry : pushedFlows.entries()) { |
| 743 | log.debug("Pushed flow: {} => {}", entry.getKey(), entry.getValue()); |
| 744 | }*/ |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 745 | |
| 746 | Collection<PushedFlowMod> pushedFlowMods |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 747 | = pushedFlows.removeAll(prefix); |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 748 | |
| 749 | for (PushedFlowMod pfm : pushedFlowMods) { |
| 750 | log.debug("Pushing a DELETE flow mod to {}, matches prefix {} with mac-rewrite {}", |
| 751 | new Object[] {HexString.toHexString(pfm.getDpid()), |
| 752 | pfm.getFlowMod().getMatch().getNetworkDestination() + |
| 753 | pfm.getFlowMod().getMatch().getNetworkDestinationMaskLen(), |
| 754 | HexString.toHexString(((OFActionDataLayerDestination)pfm.getFlowMod().getActions().get(0)) |
| 755 | .getDataLayerAddress())}); |
| 756 | |
| 757 | OFFlowMod fm = pfm.getFlowMod(); |
| 758 | |
| 759 | fm.setCommand(OFFlowMod.OFPFC_DELETE) |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 760 | .setOutPort(OFPort.OFPP_NONE) |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 761 | .setLengthU(OFFlowMod.MINIMUM_LENGTH); |
| 762 | |
| 763 | fm.getActions().clear(); |
| 764 | |
| 765 | IOFSwitch sw = floodlightProvider.getSwitches().get(pfm.getDpid()); |
| 766 | if (sw == null) { |
| 767 | log.warn("Switch not found when pushing delete flow mod"); |
| 768 | continue; |
| 769 | } |
| 770 | |
| 771 | try { |
| 772 | sw.write(fm, null); |
| 773 | sw.flush(); |
| 774 | } catch (IOException e) { |
| 775 | log.error("Failure writing flow mod", e); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 780 | /* |
| 781 | * On startup we need to calculate a full mesh of paths between all gateway |
| 782 | * switches |
| 783 | */ |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 784 | private void setupFullMesh(){ |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 785 | //For each border router, calculate and install a path from every other |
| 786 | //border switch to said border router. However, don't install the entry |
| 787 | //in to the first hop switch, as we need to install an entry to rewrite |
| 788 | //for each prefix received. This will be done later when prefixes have |
| 789 | //actually been received. |
| 790 | |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 791 | for (BgpPeer peer : bgpPeers.values()) { |
| 792 | Interface peerInterface = interfaces.get(peer.getInterfaceName()); |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 793 | |
| 794 | //See if we know the MAC address of the peer. If not we can't |
| 795 | //do anything until we learn it |
| 796 | byte[] mac = proxyArp.getMacAddress(peer.getIpAddress()); |
| 797 | if (mac == null) { |
| 798 | log.debug("Don't know MAC for {}", peer.getIpAddress().getHostAddress()); |
| 799 | //Put in the pending paths list first |
| 800 | pathsWaitingOnArp.put(peer.getIpAddress(), |
| 801 | new PathUpdate(peerInterface, peer.getIpAddress())); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 802 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 803 | proxyArp.sendArpRequest(peer.getIpAddress(), this, true); |
| 804 | continue; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 805 | } |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 806 | |
| 807 | //If we know the MAC, lets go ahead and push the paths to this peer |
| 808 | calculateAndPushPath(peerInterface, MACAddress.valueOf(mac)); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 812 | private void calculateAndPushPath(Interface dstInterface, MACAddress dstMacAddress) { |
| 813 | for (Interface srcInterface : interfaces.values()) { |
| 814 | if (dstInterface.equals(srcInterface.getName())){ |
| 815 | continue; |
| 816 | } |
| 817 | |
| 818 | DataPath shortestPath = topoRouteService.getShortestPath( |
| 819 | srcInterface.getSwitchPort(), dstInterface.getSwitchPort()); |
| 820 | |
| 821 | if (shortestPath == null){ |
| 822 | log.debug("Shortest path between {} and {} not found", |
| 823 | srcInterface.getSwitchPort(), dstInterface.getSwitchPort()); |
| 824 | return; // just quit here? |
| 825 | } |
| 826 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 827 | installPath(shortestPath.flowEntries(), dstMacAddress); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | private void installPath(List<FlowEntry> flowEntries, MACAddress dstMacAddress){ |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 832 | //Set up the flow mod |
| 833 | OFFlowMod fm = |
| 834 | (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 835 | .getMessage(OFType.FLOW_MOD); |
| 836 | |
| 837 | OFActionOutput action = new OFActionOutput(); |
| 838 | action.setMaxLength((short)0xffff); |
| 839 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 840 | actions.add(action); |
| 841 | |
| 842 | fm.setIdleTimeout((short)0) |
| 843 | .setHardTimeout((short)0) |
| 844 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 845 | .setCookie(L2_FWD_COOKIE) |
| 846 | .setCommand(OFFlowMod.OFPFC_ADD) |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 847 | .setActions(actions) |
| 848 | .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH); |
| 849 | |
| 850 | //Don't push the first flow entry. We need to push entries in the |
| 851 | //first switch based on IP prefix which we don't know yet. |
| 852 | for (int i = 1; i < flowEntries.size(); i++){ |
| 853 | FlowEntry flowEntry = flowEntries.get(i); |
| 854 | |
| 855 | OFMatch match = new OFMatch(); |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 856 | //TODO Again using MAC address from configuration |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 857 | match.setDataLayerDestination(dstMacAddress.toBytes()); |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 858 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_DST); |
| 859 | ((OFActionOutput) fm.getActions().get(0)).setPort(flowEntry.outPort().value()); |
| 860 | |
| 861 | fm.setMatch(match); |
| 862 | |
| 863 | IOFSwitch sw = floodlightProvider.getSwitches().get(flowEntry.dpid().value()); |
| 864 | |
| 865 | if (sw == null){ |
| 866 | log.warn("Switch not found when pushing flow mod"); |
| 867 | continue; |
| 868 | } |
| 869 | |
| 870 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 871 | msglist.add(fm); |
| 872 | try { |
| 873 | sw.write(msglist, null); |
| 874 | sw.flush(); |
| 875 | } catch (IOException e) { |
| 876 | log.error("Failure writing flow mod", e); |
| 877 | } |
| 878 | |
| 879 | try { |
| 880 | fm = fm.clone(); |
| 881 | } catch (CloneNotSupportedException e1) { |
| 882 | log.error("Failure cloning flow mod", e1); |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 887 | private void setupBgpPaths(){ |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 888 | for (BgpPeer bgpPeer : bgpPeers.values()){ |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 889 | Interface peerInterface = interfaces.get(bgpPeer.getInterfaceName()); |
| 890 | |
| 891 | DataPath path = topoRouteService.getShortestPath( |
| 892 | peerInterface.getSwitchPort(), bgpdAttachmentPoint); |
| 893 | |
| 894 | if (path == null){ |
| 895 | log.debug("Unable to compute path for BGP traffic for {}", |
| 896 | bgpPeer.getIpAddress()); |
| 897 | continue; |
| 898 | } |
| 899 | |
| 900 | //Set up the flow mod |
| 901 | OFFlowMod fm = |
| 902 | (OFFlowMod) floodlightProvider.getOFMessageFactory() |
| 903 | .getMessage(OFType.FLOW_MOD); |
| 904 | |
| 905 | OFActionOutput action = new OFActionOutput(); |
| 906 | action.setMaxLength((short)0xffff); |
| 907 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 908 | actions.add(action); |
| 909 | |
| 910 | fm.setIdleTimeout((short)0) |
| 911 | .setHardTimeout((short)0) |
| 912 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 913 | .setCookie(BGP_COOKIE) |
| 914 | .setCommand(OFFlowMod.OFPFC_ADD) |
| 915 | .setPriority(SDNIP_PRIORITY) |
| 916 | .setActions(actions) |
| 917 | .setLengthU(OFFlowMod.MINIMUM_LENGTH+OFActionOutput.MINIMUM_LENGTH); |
| 918 | |
| 919 | //Forward = gateway -> bgpd, reverse = bgpd -> gateway |
| 920 | OFMatch forwardMatchSrc = new OFMatch(); |
| 921 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 922 | String interfaceCidrAddress = peerInterface.getIpAddress().getHostAddress() |
| 923 | + "/32"; |
| 924 | String peerCidrAddress = bgpPeer.getIpAddress().getHostAddress() |
| 925 | + "/32"; |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 926 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 927 | //Common match fields |
| 928 | forwardMatchSrc.setDataLayerType(Ethernet.TYPE_IPv4); |
| 929 | //forwardMatch.setWildcards(forwardMatch.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 930 | forwardMatchSrc.setNetworkProtocol(IPv4.PROTOCOL_TCP); |
| 931 | forwardMatchSrc.setTransportDestination(BGP_PORT); |
| 932 | forwardMatchSrc.setWildcards(forwardMatchSrc.getWildcards() & ~OFMatch.OFPFW_IN_PORT |
| 933 | & ~OFMatch.OFPFW_DL_TYPE & ~OFMatch.OFPFW_NW_PROTO); |
| 934 | |
| 935 | |
| 936 | OFMatch reverseMatchSrc = forwardMatchSrc.clone(); |
| 937 | |
| 938 | forwardMatchSrc.setFromCIDR(peerCidrAddress, OFMatch.STR_NW_SRC); |
| 939 | forwardMatchSrc.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_DST); |
| 940 | |
| 941 | OFMatch forwardMatchDst = forwardMatchSrc.clone(); |
| 942 | |
| 943 | forwardMatchSrc.setTransportSource(BGP_PORT); |
| 944 | forwardMatchSrc.setWildcards(forwardMatchSrc.getWildcards() & ~OFMatch.OFPFW_TP_SRC); |
| 945 | forwardMatchDst.setTransportDestination(BGP_PORT); |
| 946 | forwardMatchDst.setWildcards(forwardMatchDst.getWildcards() & ~OFMatch.OFPFW_TP_DST); |
| 947 | |
| 948 | reverseMatchSrc.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_SRC); |
| 949 | reverseMatchSrc.setFromCIDR(peerCidrAddress, OFMatch.STR_NW_DST); |
| 950 | |
| 951 | OFMatch reverseMatchDst = reverseMatchSrc.clone(); |
| 952 | |
| 953 | reverseMatchSrc.setTransportSource(BGP_PORT); |
| 954 | reverseMatchSrc.setWildcards(forwardMatchSrc.getWildcards() & ~OFMatch.OFPFW_TP_SRC); |
| 955 | reverseMatchDst.setTransportDestination(BGP_PORT); |
| 956 | reverseMatchDst.setWildcards(forwardMatchDst.getWildcards() & ~OFMatch.OFPFW_TP_DST); |
| 957 | |
| 958 | fm.setMatch(forwardMatchSrc); |
| 959 | |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 960 | OFMatch forwardIcmpMatch = new OFMatch(); |
| 961 | forwardIcmpMatch.setDataLayerType(Ethernet.TYPE_IPv4); |
| 962 | forwardIcmpMatch.setNetworkProtocol(IPv4.PROTOCOL_ICMP); |
| 963 | forwardIcmpMatch.setWildcards(forwardIcmpMatch.getWildcards() & |
| 964 | ~OFMatch.OFPFW_DL_TYPE & ~OFMatch.OFPFW_NW_PROTO); |
| 965 | |
| 966 | OFMatch reverseIcmpMatch = forwardIcmpMatch.clone(); |
| 967 | forwardIcmpMatch.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_DST); |
| 968 | reverseIcmpMatch.setFromCIDR(interfaceCidrAddress, OFMatch.STR_NW_SRC); |
| 969 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 970 | for (FlowEntry flowEntry : path.flowEntries()){ |
| 971 | OFFlowMod forwardFlowModSrc, forwardFlowModDst; |
| 972 | OFFlowMod reverseFlowModSrc, reverseFlowModDst; |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 973 | OFFlowMod forwardIcmp, reverseIcmp; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 974 | try { |
| 975 | forwardFlowModSrc = fm.clone(); |
| 976 | forwardFlowModDst = fm.clone(); |
| 977 | reverseFlowModSrc = fm.clone(); |
| 978 | reverseFlowModDst = fm.clone(); |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 979 | forwardIcmp = fm.clone(); |
| 980 | reverseIcmp = fm.clone(); |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 981 | } catch (CloneNotSupportedException e) { |
| 982 | log.warn("Clone failed", e); |
| 983 | continue; |
| 984 | } |
| 985 | |
| 986 | forwardMatchSrc.setInputPort(flowEntry.inPort().value()); |
| 987 | forwardFlowModSrc.setMatch(forwardMatchSrc); |
| 988 | ((OFActionOutput)forwardFlowModSrc.getActions().get(0)) |
| 989 | .setPort(flowEntry.outPort().value()); |
| 990 | |
| 991 | forwardMatchDst.setInputPort(flowEntry.inPort().value()); |
| 992 | forwardFlowModDst.setMatch(forwardMatchDst); |
| 993 | ((OFActionOutput)forwardFlowModDst.getActions().get(0)) |
| 994 | .setPort(flowEntry.outPort().value()); |
| 995 | |
| 996 | reverseMatchSrc.setInputPort(flowEntry.outPort().value()); |
| 997 | reverseFlowModSrc.setMatch(reverseMatchSrc); |
| 998 | ((OFActionOutput)reverseFlowModSrc.getActions().get(0)) |
| 999 | .setPort(flowEntry.inPort().value()); |
| 1000 | |
| 1001 | reverseMatchDst.setInputPort(flowEntry.outPort().value()); |
| 1002 | reverseFlowModDst.setMatch(reverseMatchDst); |
| 1003 | ((OFActionOutput)reverseFlowModDst.getActions().get(0)) |
| 1004 | .setPort(flowEntry.inPort().value()); |
| 1005 | |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 1006 | ((OFActionOutput)forwardIcmp.getActions().get(0)) |
| 1007 | .setPort(flowEntry.outPort().value()); |
| 1008 | forwardIcmp.setMatch(forwardIcmpMatch); |
| 1009 | |
| 1010 | ((OFActionOutput)reverseIcmp.getActions().get(0)) |
| 1011 | .setPort(flowEntry.inPort().value()); |
| 1012 | reverseIcmp.setMatch(reverseIcmpMatch); |
| 1013 | |
| 1014 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 1015 | IOFSwitch sw = floodlightProvider.getSwitches().get(flowEntry.dpid().value()); |
| 1016 | |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 1017 | if (sw == null) { |
| 1018 | log.warn("Switch not found when pushing BGP paths"); |
| 1019 | return; |
| 1020 | } |
| 1021 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 1022 | List<OFMessage> msgList = new ArrayList<OFMessage>(2); |
| 1023 | msgList.add(forwardFlowModSrc); |
| 1024 | msgList.add(forwardFlowModDst); |
| 1025 | msgList.add(reverseFlowModSrc); |
| 1026 | msgList.add(reverseFlowModDst); |
Jonathan Hart | 38c8493 | 2013-08-10 17:49:27 +1200 | [diff] [blame] | 1027 | msgList.add(forwardIcmp); |
| 1028 | msgList.add(reverseIcmp); |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 1029 | |
| 1030 | try { |
| 1031 | sw.write(msgList, null); |
| 1032 | sw.flush(); |
| 1033 | } catch (IOException e) { |
| 1034 | log.error("Failure writing flow mod", e); |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | } |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1039 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 1040 | @Override |
| 1041 | public void arpResponse(InetAddress ipAddress, byte[] macAddress) { |
| 1042 | log.debug("Received ARP response: {} => {}", ipAddress.getHostAddress(), |
| 1043 | MACAddress.valueOf(macAddress).toString()); |
| 1044 | |
| 1045 | Set<PathUpdate> pathsToPush = pathsWaitingOnArp.removeAll(ipAddress); |
| 1046 | |
| 1047 | for (PathUpdate update : pathsToPush) { |
| 1048 | log.debug("Pushing path to {} at {} on {}", new Object[] { |
| 1049 | update.getDstIpAddress().getHostAddress(), |
| 1050 | MACAddress.valueOf(macAddress), |
| 1051 | update.getDstInterface().getSwitchPort()}); |
| 1052 | calculateAndPushPath(update.getDstInterface(), |
| 1053 | MACAddress.valueOf(macAddress)); |
| 1054 | } |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 1055 | |
| 1056 | Set<RibUpdate> prefixesToPush = prefixesWaitingOnArp.removeAll(ipAddress); |
| 1057 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 1058 | /* |
| 1059 | * We synchronize on this to prevent changes to the ptree while we're pushing |
| 1060 | * flows to the switches. If the ptree changes, the ptree and switches |
| 1061 | * could get out of sync. |
| 1062 | */ |
| 1063 | synchronized (this) { |
| 1064 | for (RibUpdate update : prefixesToPush) { |
| 1065 | //These will always be adds |
| 1066 | |
| 1067 | //addPrefixFlows(update.getPrefix(), update.getRibEntry()); |
| 1068 | //processRibAdd(update); |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 1069 | RibEntry rib = ptree.lookup(update.getPrefix()); |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 1070 | if (rib != null && rib.equals(update.getRibEntry())) { |
| 1071 | log.debug("Pushing prefix {} next hop {}", update.getPrefix(), |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 1072 | rib.getNextHop().getHostAddress()); |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 1073 | //We only push prefix flows if the prefix is still in the ptree |
| 1074 | //and the next hop is the same as our update. The prefix could |
| 1075 | //have been removed while we were waiting for the ARP, or the |
| 1076 | //next hop could have changed. |
| 1077 | addPrefixFlows(update.getPrefix(), rib); |
| 1078 | } else { |
| 1079 | log.debug("Received ARP response, but {},{} is no longer in ptree", |
| 1080 | update.getPrefix(), update.getRibEntry()); |
| 1081 | } |
| 1082 | } |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 1083 | } |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 1084 | } |
| 1085 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1086 | private void beginRouting(){ |
| 1087 | log.debug("Topology is now ready, beginning routing function"); |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 1088 | setupBgpPaths(); |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 1089 | setupFullMesh(); |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1090 | |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 1091 | //Traverse ptree and create flows for all routes |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 1092 | /* |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1093 | for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)){ |
| 1094 | if (node.rib != null){ |
| 1095 | prefixAdded(node); |
| 1096 | } |
| 1097 | } |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 1098 | */ |
| 1099 | |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 1100 | /* |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 1101 | synchronized (ptree) { |
| 1102 | Iterator<IPatriciaTrie.Entry> it = ptree.iterator(); |
| 1103 | while (it.hasNext()) { |
| 1104 | IPatriciaTrie.Entry entry = it.next(); |
| 1105 | addPrefixFlows(entry.getPrefix(), entry.getRib()); |
| 1106 | } |
| 1107 | } |
Jonathan Hart | 0a46fe4 | 2013-08-10 17:08:47 +1200 | [diff] [blame] | 1108 | */ |
| 1109 | |
| 1110 | bgpUpdatesExecutor.execute(new Runnable() { |
| 1111 | @Override |
| 1112 | public void run() { |
| 1113 | doUpdatesThread(); |
| 1114 | } |
| 1115 | }); |
Jonathan Hart | d7e158d | 2013-08-07 23:04:48 +1200 | [diff] [blame] | 1116 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | private void checkSwitchesConnected(){ |
| 1120 | for (String dpid : switches){ |
| 1121 | if (floodlightProvider.getSwitches().get(HexString.toLong(dpid)) == null){ |
| 1122 | log.debug("Not all switches are here yet"); |
| 1123 | return; |
| 1124 | } |
| 1125 | } |
| 1126 | switchesConnected = true; |
| 1127 | } |
| 1128 | |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 1129 | //Actually we only need to go half way round to verify full mesh connectivity |
| 1130 | //(n^2)/2 |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1131 | private void checkTopologyReady(){ |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 1132 | for (Interface dstInterface : interfaces.values()) { |
| 1133 | for (Interface srcInterface : interfaces.values()) { |
| 1134 | if (dstInterface == srcInterface) { |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1135 | continue; |
| 1136 | } |
| 1137 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1138 | DataPath shortestPath = topoRouteService.getShortestPath( |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 1139 | srcInterface.getSwitchPort(), dstInterface.getSwitchPort()); |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1140 | |
| 1141 | if (shortestPath == null){ |
| 1142 | log.debug("Shortest path between {} and {} not found", |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 1143 | srcInterface.getSwitchPort(), dstInterface.getSwitchPort()); |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1144 | return; |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | topologyReady = true; |
| 1149 | } |
| 1150 | |
| 1151 | private void checkStatus(){ |
| 1152 | log.debug("In checkStatus, swC {}, toRe {}", switchesConnected, topologyReady); |
| 1153 | |
| 1154 | if (!switchesConnected){ |
| 1155 | checkSwitchesConnected(); |
| 1156 | } |
| 1157 | boolean oldTopologyReadyStatus = topologyReady; |
| 1158 | if (switchesConnected && !topologyReady){ |
| 1159 | checkTopologyReady(); |
| 1160 | } |
| 1161 | if (!oldTopologyReadyStatus && topologyReady){ |
| 1162 | beginRouting(); |
| 1163 | } |
| 1164 | } |
| 1165 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1166 | @Override |
| 1167 | public void startUp(FloodlightModuleContext context) { |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 1168 | restApi.addRestletRoutable(new BgpRouteWebRoutable()); |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1169 | floodlightProvider.addOFSwitchListener(this); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 1170 | topology.addListener(this); |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 1171 | |
Jonathan Hart | c7ca35d | 2013-06-25 20:54:25 +1200 | [diff] [blame] | 1172 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, proxyArp); |
| 1173 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 1174 | //Retrieve the RIB from BGPd during startup |
| 1175 | retrieveRib(); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1176 | } |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 1177 | |
| 1178 | private void doUpdatesThread() { |
| 1179 | boolean interrupted = false; |
| 1180 | try { |
| 1181 | while (true) { |
| 1182 | try { |
| 1183 | RibUpdate update = ribUpdates.take(); |
| 1184 | switch (update.getOperation()){ |
| 1185 | case UPDATE: |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 1186 | processRibAdd(update); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 1187 | break; |
| 1188 | case DELETE: |
Jonathan Hart | 2f74078 | 2013-08-04 00:49:21 +1200 | [diff] [blame] | 1189 | processRibDelete(update); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 1190 | break; |
| 1191 | } |
| 1192 | } catch (InterruptedException e) { |
Jonathan Hart | 9ea3121 | 2013-08-12 21:40:34 +1200 | [diff] [blame] | 1193 | log.debug("interrupted", e); |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 1194 | interrupted = true; |
| 1195 | } |
| 1196 | } |
| 1197 | } finally { |
| 1198 | if (interrupted) { |
| 1199 | Thread.currentThread().interrupt(); |
| 1200 | } |
| 1201 | } |
| 1202 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1203 | |
| 1204 | @Override |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1205 | public void topologyChanged() { |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1206 | boolean refreshNeeded = false; |
| 1207 | for (LDUpdate ldu : topology.getLastLinkUpdates()){ |
| 1208 | if (!ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.LINK_UPDATED)){ |
| 1209 | //We don't need to recalculate anything for just link updates |
Jonathan Hart | b39a67d | 2013-08-10 23:59:50 +1200 | [diff] [blame] | 1210 | //They happen very frequently |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1211 | refreshNeeded = true; |
| 1212 | } |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 1213 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1214 | log.debug("Topo change {}", ldu.getOperation()); |
Jonathan Hart | c824ad0 | 2013-07-03 15:58:45 +1200 | [diff] [blame] | 1215 | |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 1216 | if (ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.LINK_ADDED)){ |
| 1217 | synchronized (linkUpdates) { |
| 1218 | linkUpdates.add(ldu); |
| 1219 | } |
| 1220 | } |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | if (refreshNeeded){ |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 1224 | topologyChangeDetectorTask.reschedule(TOPO_DETECTION_WAIT, TimeUnit.SECONDS); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1225 | } |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1226 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1227 | |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1228 | //TODO determine whether we need to listen for switch joins |
| 1229 | @Override |
| 1230 | public void addedSwitch(IOFSwitch sw) { |
| 1231 | //checkStatus(); |
| 1232 | } |
| 1233 | |
| 1234 | @Override |
| 1235 | public void removedSwitch(IOFSwitch sw) { |
| 1236 | // TODO Auto-generated method stub |
| 1237 | } |
| 1238 | |
| 1239 | @Override |
| 1240 | public void switchPortChanged(Long switchId) {} |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 1241 | |
| 1242 | @Override |
| 1243 | public String getName() { |
| 1244 | return "BgpRoute"; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1245 | } |
| 1246 | } |