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