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