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