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