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