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