Jonathan Hart | 382623d | 2014-04-03 09:48:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.bgproute; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 2 | |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 3 | import java.io.File; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 4 | import java.io.IOException; |
| 5 | import java.net.InetAddress; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 6 | import java.util.ArrayList; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 7 | import java.util.Collection; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 8 | import java.util.HashMap; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 9 | import java.util.List; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 10 | import java.util.Map; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 11 | import java.util.Set; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 12 | import java.util.concurrent.BlockingQueue; |
| 13 | import java.util.concurrent.ExecutorService; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 14 | import java.util.concurrent.Executors; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 15 | import java.util.concurrent.LinkedBlockingQueue; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 16 | import java.util.concurrent.ScheduledExecutorService; |
| 17 | import java.util.concurrent.TimeUnit; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 18 | |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 19 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 20 | import net.floodlightcontroller.core.IOFSwitch; |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 21 | import net.floodlightcontroller.core.IOFSwitchListener; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 22 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 23 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 24 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 25 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 26 | import net.floodlightcontroller.core.util.SingletonTask; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 27 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 28 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | 382623d | 2014-04-03 09:48:11 -0700 | [diff] [blame] | 29 | import net.onrc.onos.apps.bgproute.RibUpdate.Operation; |
Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 30 | import net.onrc.onos.apps.proxyarp.IArpRequester; |
| 31 | import net.onrc.onos.apps.proxyarp.IProxyArpService; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.linkdiscovery.ILinkDiscovery.LDUpdate; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 33 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService; |
Jonathan Hart | 51f6f5b | 2014-04-03 10:32:10 -0700 | [diff] [blame] | 34 | import net.onrc.onos.core.main.config.IConfigInfoService; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 35 | import net.onrc.onos.core.packet.Ethernet; |
| 36 | import net.onrc.onos.core.packet.IPv4; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 37 | import net.onrc.onos.core.util.CallerId; |
| 38 | import net.onrc.onos.core.util.DataPath; |
| 39 | import net.onrc.onos.core.util.Dpid; |
| 40 | import net.onrc.onos.core.util.FlowEntryAction; |
| 41 | import net.onrc.onos.core.util.FlowEntryActions; |
| 42 | import net.onrc.onos.core.util.FlowEntryMatch; |
| 43 | import net.onrc.onos.core.util.FlowId; |
| 44 | import net.onrc.onos.core.util.FlowPath; |
| 45 | import net.onrc.onos.core.util.FlowPathFlags; |
| 46 | import net.onrc.onos.core.util.FlowPathType; |
| 47 | import net.onrc.onos.core.util.FlowPathUserState; |
| 48 | import net.onrc.onos.core.util.IPv4Net; |
| 49 | import net.onrc.onos.core.util.Port; |
| 50 | import net.onrc.onos.core.util.SwitchPort; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 51 | import net.sf.json.JSONArray; |
| 52 | import net.sf.json.JSONObject; |
| 53 | import net.sf.json.JSONSerializer; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 54 | |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 55 | import org.codehaus.jackson.JsonParseException; |
| 56 | import org.codehaus.jackson.map.JsonMappingException; |
| 57 | import org.codehaus.jackson.map.ObjectMapper; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 58 | import org.openflow.protocol.OFFlowMod; |
| 59 | import org.openflow.protocol.OFMatch; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 60 | import org.openflow.protocol.OFPacketOut; |
Jonathan Hart | 9575cb6 | 2013-07-05 13:43:49 +1200 | [diff] [blame] | 61 | import org.openflow.protocol.OFPort; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 62 | import org.openflow.protocol.action.OFAction; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 63 | import org.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 1236a9b | 2013-06-18 22:10:05 +1200 | [diff] [blame] | 64 | import org.openflow.util.HexString; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 65 | import org.slf4j.Logger; |
| 66 | import org.slf4j.LoggerFactory; |
| 67 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 68 | import com.google.common.collect.HashMultimap; |
Jonathan Hart | 0ee0f02 | 2013-08-03 22:21:54 +1200 | [diff] [blame] | 69 | import com.google.common.collect.Multimap; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 70 | import com.google.common.collect.Multimaps; |
| 71 | import com.google.common.collect.SetMultimap; |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 72 | import com.google.common.net.InetAddresses; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 73 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 74 | |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 75 | public class BgpRoute implements IFloodlightModule, IBgpRouteService, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | IArpRequester, |
| 77 | IOFSwitchListener, IConfigInfoService { |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 78 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 79 | private final static Logger log = LoggerFactory.getLogger(BgpRoute.class); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 80 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 81 | private IFloodlightProviderService floodlightProvider; |
| 82 | private ILinkDiscoveryService linkDiscoveryService; |
| 83 | private IRestApiService restApi; |
| 84 | private IProxyArpService proxyArp; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 85 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | private IPatriciaTrie<RibEntry> ptree; |
| 87 | private IPatriciaTrie<Interface> interfacePtrie; |
| 88 | private BlockingQueue<RibUpdate> ribUpdates; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 89 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 90 | private String bgpdRestIp; |
| 91 | private String routerId; |
| 92 | private String configFilename = "config.json"; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 93 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | //We need to identify our flows somehow. But like it says in LearningSwitch.java, |
| 95 | //the controller/OS should hand out cookie IDs to prevent conflicts. |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 96 | private static final long APP_COOKIE = 0xa0000000000000L; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 97 | //Cookie for flows that do L2 forwarding within SDN domain to egress routers |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 98 | private static final long L2_FWD_COOKIE = APP_COOKIE + 1; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 99 | //Cookie for flows in ingress switches that rewrite the MAC address |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 100 | private static final long MAC_RW_COOKIE = APP_COOKIE + 2; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | //Cookie for flows that setup BGP paths |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 102 | private static final long BGP_COOKIE = APP_COOKIE + 3; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | //Forwarding uses priority 0, and the mac rewrite entries in ingress switches |
| 104 | //need to be higher priority than this otherwise the rewrite may not get done |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 105 | private static final short SDNIP_PRIORITY = 10; |
| 106 | private static final short ARP_PRIORITY = 20; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 107 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 108 | private static final short BGP_PORT = 179; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 109 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 110 | private static final int TOPO_DETECTION_WAIT = 2; //seconds |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 111 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 112 | //Configuration stuff |
| 113 | private List<String> switches; |
| 114 | private Map<String, Interface> interfaces; |
| 115 | private Map<InetAddress, BgpPeer> bgpPeers; |
| 116 | private SwitchPort bgpdAttachmentPoint; |
| 117 | private MACAddress bgpdMacAddress; |
| 118 | private short vlan; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 119 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 120 | //True when all switches have connected |
| 121 | private volatile boolean switchesConnected = false; |
| 122 | //True when we have a full mesh of shortest paths between gateways |
| 123 | private volatile boolean topologyReady = false; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 124 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 125 | private ArrayList<LDUpdate> linkUpdates; |
| 126 | private SingletonTask topologyChangeDetectorTask; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 127 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 128 | private SetMultimap<InetAddress, RibUpdate> prefixesWaitingOnArp; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 129 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 130 | private Map<InetAddress, Path> pathsWaitingOnArp; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 131 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | private ExecutorService bgpUpdatesExecutor; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 133 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 134 | private Map<InetAddress, Path> pushedPaths; |
| 135 | private Map<Prefix, Path> prefixToPath; |
| 136 | // private Multimap<Prefix, PushedFlowMod> pushedFlows; |
| 137 | private Multimap<Prefix, FlowId> pushedFlowIds; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 138 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 139 | private FlowCache flowCache; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 140 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 141 | // TODO: Fix for the new Topology Network Graph |
| 142 | // private volatile Topology topology = null; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 143 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 144 | private class TopologyChangeDetector implements Runnable { |
| 145 | @Override |
| 146 | public void run() { |
| 147 | log.debug("Running topology change detection task"); |
| 148 | synchronized (linkUpdates) { |
| 149 | //This is the model the REST API uses to retrieve network graph info |
| 150 | // TODO: Fix the code below after topoLinkService was removed |
| 151 | /* |
| 152 | ITopoLinkService topoLinkService = new TopoLinkServiceImpl(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 153 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 154 | List<Link> activeLinks = topoLinkService.getActiveLinks(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 155 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 156 | Iterator<LDUpdate> it = linkUpdates.iterator(); |
| 157 | while (it.hasNext()){ |
| 158 | LDUpdate ldu = it.next(); |
| 159 | Link l = new Link(ldu.getSrc(), ldu.getSrcPort(), |
| 160 | ldu.getDst(), ldu.getDstPort()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 161 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 162 | if (activeLinks.contains(l)){ |
| 163 | it.remove(); |
| 164 | } |
| 165 | } |
| 166 | */ |
| 167 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 168 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 169 | if (!topologyReady) { |
| 170 | if (linkUpdates.isEmpty()) { |
| 171 | //All updates have been seen in network map. |
| 172 | //We can check if topology is ready |
| 173 | log.debug("No known changes outstanding. Checking topology now"); |
| 174 | checkStatus(); |
| 175 | } else { |
| 176 | //We know of some link updates that haven't propagated to the database yet |
| 177 | log.debug("Some changes not found in network map - {} links missing", linkUpdates.size()); |
| 178 | topologyChangeDetectorTask.reschedule(TOPO_DETECTION_WAIT, TimeUnit.SECONDS); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 183 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 184 | private void readConfiguration(String configFilename) { |
| 185 | File gatewaysFile = new File(configFilename); |
| 186 | ObjectMapper mapper = new ObjectMapper(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 187 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 188 | try { |
| 189 | Configuration config = mapper.readValue(gatewaysFile, Configuration.class); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 190 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 191 | switches = config.getSwitches(); |
| 192 | interfaces = new HashMap<String, Interface>(); |
| 193 | for (Interface intf : config.getInterfaces()) { |
| 194 | interfaces.put(intf.getName(), intf); |
| 195 | } |
| 196 | bgpPeers = new HashMap<InetAddress, BgpPeer>(); |
| 197 | for (BgpPeer peer : config.getPeers()) { |
| 198 | bgpPeers.put(peer.getIpAddress(), peer); |
| 199 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 200 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 201 | bgpdAttachmentPoint = new SwitchPort( |
| 202 | new Dpid(config.getBgpdAttachmentDpid()), |
| 203 | new Port(config.getBgpdAttachmentPort())); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 204 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 205 | bgpdMacAddress = config.getBgpdMacAddress(); |
| 206 | vlan = config.getVlan(); |
| 207 | } catch (JsonParseException e) { |
| 208 | log.error("Error in JSON file", e); |
| 209 | System.exit(1); |
| 210 | } catch (JsonMappingException e) { |
| 211 | log.error("Error in JSON file", e); |
| 212 | System.exit(1); |
| 213 | } catch (IOException e) { |
| 214 | log.error("Error reading JSON file", e); |
| 215 | System.exit(1); |
| 216 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 217 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 218 | //Populate the interface Patricia Trie |
| 219 | for (Interface intf : interfaces.values()) { |
| 220 | Prefix prefix = new Prefix(intf.getIpAddress().getAddress(), intf.getPrefixLength()); |
| 221 | interfacePtrie.put(prefix, intf); |
| 222 | } |
| 223 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 224 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 225 | @Override |
| 226 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 227 | Collection<Class<? extends IFloodlightService>> l |
| 228 | = new ArrayList<Class<? extends IFloodlightService>>(); |
| 229 | l.add(IBgpRouteService.class); |
| 230 | l.add(IConfigInfoService.class); |
| 231 | return l; |
| 232 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 233 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 234 | @Override |
| 235 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 236 | Map<Class<? extends IFloodlightService>, IFloodlightService> m |
| 237 | = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
| 238 | m.put(IBgpRouteService.class, this); |
| 239 | m.put(IConfigInfoService.class, this); |
| 240 | return m; |
| 241 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 242 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 243 | @Override |
| 244 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 245 | Collection<Class<? extends IFloodlightService>> l |
| 246 | = new ArrayList<Class<? extends IFloodlightService>>(); |
| 247 | l.add(IFloodlightProviderService.class); |
| 248 | l.add(IRestApiService.class); |
| 249 | return l; |
| 250 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 251 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 252 | @Override |
| 253 | public void init(FloodlightModuleContext context) |
| 254 | throws FloodlightModuleException { |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 255 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 256 | ptree = new PatriciaTrie<RibEntry>(32); |
| 257 | interfacePtrie = new PatriciaTrie<Interface>(32); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 258 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 259 | ribUpdates = new LinkedBlockingQueue<RibUpdate>(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 260 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 261 | // Register floodlight provider and REST handler. |
| 262 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 263 | linkDiscoveryService = context.getServiceImpl(ILinkDiscoveryService.class); |
| 264 | restApi = context.getServiceImpl(IRestApiService.class); |
| 265 | proxyArp = context.getServiceImpl(IProxyArpService.class); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 266 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 267 | linkUpdates = new ArrayList<LDUpdate>(); |
| 268 | ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); |
| 269 | topologyChangeDetectorTask = new SingletonTask(executor, new TopologyChangeDetector()); |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 270 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 271 | pathsWaitingOnArp = new HashMap<InetAddress, Path>(); |
| 272 | prefixesWaitingOnArp = Multimaps.synchronizedSetMultimap( |
| 273 | HashMultimap.<InetAddress, RibUpdate>create()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 274 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 275 | pushedPaths = new HashMap<InetAddress, Path>(); |
| 276 | prefixToPath = new HashMap<Prefix, Path>(); |
| 277 | // pushedFlows = HashMultimap.<Prefix, PushedFlowMod>create(); |
| 278 | pushedFlowIds = HashMultimap.<Prefix, FlowId>create(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 279 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 280 | flowCache = new FlowCache(floodlightProvider); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 281 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 282 | bgpUpdatesExecutor = Executors.newSingleThreadExecutor( |
| 283 | new ThreadFactoryBuilder().setNameFormat("bgp-updates-%d").build()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 284 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 285 | //Read in config values |
| 286 | bgpdRestIp = context.getConfigParams(this).get("BgpdRestIp"); |
| 287 | if (bgpdRestIp == null) { |
| 288 | log.error("BgpdRestIp property not found in config file"); |
| 289 | System.exit(1); |
| 290 | } else { |
| 291 | log.info("BgpdRestIp set to {}", bgpdRestIp); |
| 292 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 293 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 294 | routerId = context.getConfigParams(this).get("RouterId"); |
| 295 | if (routerId == null) { |
| 296 | log.error("RouterId property not found in config file"); |
| 297 | System.exit(1); |
| 298 | } else { |
| 299 | log.info("RouterId set to {}", routerId); |
| 300 | } |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 301 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 302 | String configFilenameParameter = context.getConfigParams(this).get("configfile"); |
| 303 | if (configFilenameParameter != null) { |
| 304 | configFilename = configFilenameParameter; |
| 305 | } |
| 306 | log.debug("Config file set to {}", configFilename); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 307 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 308 | readConfiguration(configFilename); |
| 309 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 310 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 311 | @Override |
| 312 | public void startUp(FloodlightModuleContext context) { |
| 313 | restApi.addRestletRoutable(new BgpRouteWebRoutable()); |
| 314 | floodlightProvider.addOFSwitchListener(this); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 315 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 316 | //Retrieve the RIB from BGPd during startup |
| 317 | retrieveRib(); |
| 318 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 319 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 320 | @Override |
| 321 | public IPatriciaTrie<RibEntry> getPtree() { |
| 322 | return ptree; |
| 323 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 324 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 325 | @Override |
| 326 | public void clearPtree() { |
| 327 | ptree = new PatriciaTrie<RibEntry>(32); |
| 328 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 329 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 330 | @Override |
| 331 | public String getBGPdRestIp() { |
| 332 | return bgpdRestIp; |
| 333 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 334 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 335 | @Override |
| 336 | public String getRouterId() { |
| 337 | return routerId; |
| 338 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 339 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 340 | private void retrieveRib() { |
| 341 | String url = "http://" + bgpdRestIp + "/wm/bgp/" + routerId; |
| 342 | String response = RestClient.get(url); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 343 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 344 | if (response.equals("")) { |
| 345 | return; |
| 346 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 347 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 348 | response = response.replaceAll("\"", "'"); |
| 349 | JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON(response); |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 350 | JSONArray ribJsonArray = jsonObj.getJSONArray("rib"); |
| 351 | String routerId = jsonObj.getString("router-id"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 352 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 353 | int size = ribJsonArray.size(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 354 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 355 | log.info("Retrived RIB of {} entries from BGPd", size); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 356 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 357 | for (int j = 0; j < size; j++) { |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 358 | JSONObject secondJsonObject = ribJsonArray.getJSONObject(j); |
| 359 | String prefix = secondJsonObject.getString("prefix"); |
| 360 | String nexthop = secondJsonObject.getString("nexthop"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 361 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 362 | //insert each rib entry into the local rib; |
| 363 | String[] substring = prefix.split("/"); |
| 364 | String prefix1 = substring[0]; |
| 365 | String mask1 = substring[1]; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 366 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 367 | Prefix p; |
| 368 | try { |
| 369 | p = new Prefix(prefix1, Integer.valueOf(mask1)); |
| 370 | } catch (NumberFormatException e) { |
| 371 | log.warn("Wrong mask format in RIB JSON: {}", mask1); |
| 372 | continue; |
| 373 | } catch (IllegalArgumentException e1) { |
| 374 | log.warn("Wrong prefix format in RIB JSON: {}", prefix1); |
| 375 | continue; |
| 376 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 377 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 378 | RibEntry rib = new RibEntry(routerId, nexthop); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 379 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 380 | try { |
| 381 | ribUpdates.put(new RibUpdate(Operation.UPDATE, p, rib)); |
| 382 | } catch (InterruptedException e) { |
| 383 | log.debug("Interrupted while pushing onto update queue"); |
| 384 | } |
| 385 | } |
| 386 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 387 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 388 | @Override |
| 389 | public void newRibUpdate(RibUpdate update) { |
| 390 | try { |
| 391 | ribUpdates.put(update); |
| 392 | } catch (InterruptedException e) { |
| 393 | log.debug("Interrupted while putting on ribUpdates queue", e); |
| 394 | Thread.currentThread().interrupt(); |
| 395 | } |
| 396 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 397 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 398 | public synchronized void processRibAdd(RibUpdate update) { |
| 399 | Prefix prefix = update.getPrefix(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 400 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 401 | log.debug("Processing prefix add {}", prefix); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 402 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 403 | RibEntry rib = ptree.put(prefix, update.getRibEntry()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 404 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 405 | if (rib != null && !rib.equals(update.getRibEntry())) { |
| 406 | //There was an existing nexthop for this prefix. This update supersedes that, |
| 407 | //so we need to remove the old flows for this prefix from the switches |
| 408 | _processDeletePrefix(prefix, rib); |
| 409 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 410 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 411 | if (update.getRibEntry().getNextHop().equals( |
| 412 | InetAddresses.forString("0.0.0.0"))) { |
| 413 | //Route originated by SDN domain |
| 414 | //We don't handle these at the moment |
| 415 | log.debug("Own route {} to {}", prefix, |
| 416 | update.getRibEntry().getNextHop().getHostAddress()); |
| 417 | return; |
| 418 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 419 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 420 | _processRibAdd(update); |
| 421 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 422 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 423 | private void _processRibAdd(RibUpdate update) { |
| 424 | Prefix prefix = update.getPrefix(); |
| 425 | RibEntry rib = update.getRibEntry(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 426 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 427 | InetAddress dstIpAddress = rib.getNextHop(); |
| 428 | MACAddress nextHopMacAddress = null; |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 429 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 430 | // See if we know the MAC address of the next hop |
| 431 | // TODO if we do not treat the next hop as a device in the future, we need to update this |
| 432 | // TODO: Fix the code below after deviceStorage was removed |
| 433 | /* |
| 434 | IDeviceObject nextHopDevice = |
| 435 | deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(dstIpAddress)); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 436 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 437 | if (nextHopDevice == null){ |
| 438 | log.debug("NextHopDevice for IP: {} is null", dstIpAddress); |
| 439 | prefixesWaitingOnArp.put(dstIpAddress, |
| 440 | new RibUpdate(Operation.UPDATE, prefix, rib)); |
| 441 | proxyArp.sendArpRequest(dstIpAddress, this, true); |
| 442 | return; |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 443 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 444 | } |
| 445 | nextHopMacAddress = MACAddress.valueOf(nextHopDevice.getMACAddress()); |
| 446 | */ |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 447 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 448 | // Find the attachment point (egress interface) of the next hop |
| 449 | Interface egressInterface = null; |
| 450 | if (bgpPeers.containsKey(dstIpAddress)) { |
| 451 | //Route to a peer |
| 452 | log.debug("Route to peer {}", dstIpAddress); |
| 453 | BgpPeer peer = bgpPeers.get(dstIpAddress); |
| 454 | egressInterface = interfaces.get(peer.getInterfaceName()); |
| 455 | } else { |
| 456 | //Route to non-peer |
| 457 | log.debug("Route to non-peer {}", dstIpAddress); |
| 458 | egressInterface = interfacePtrie.match( |
| 459 | new Prefix(dstIpAddress.getAddress(), 32)); |
| 460 | if (egressInterface == null) { |
| 461 | log.warn("No outgoing interface found for {}", dstIpAddress.getHostAddress()); |
| 462 | return; |
| 463 | } |
| 464 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 465 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 466 | if (nextHopMacAddress == null) { |
| 467 | prefixesWaitingOnArp.put(dstIpAddress, |
| 468 | new RibUpdate(Operation.UPDATE, prefix, rib)); |
| 469 | proxyArp.sendArpRequest(dstIpAddress, this, true); |
| 470 | return; |
| 471 | } else { |
| 472 | if (!bgpPeers.containsKey(dstIpAddress)) { |
| 473 | //If the prefix is for a non-peer we need to ensure there's a path, |
| 474 | //and push one if there isn't. |
| 475 | Path path = pushedPaths.get(dstIpAddress); |
| 476 | if (path == null) { |
| 477 | path = new Path(egressInterface, dstIpAddress); |
| 478 | calculateAndPushPath(path, nextHopMacAddress); |
| 479 | pushedPaths.put(dstIpAddress, path); |
| 480 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 481 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 482 | path.incrementUsers(); |
| 483 | prefixToPath.put(prefix, path); |
| 484 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 485 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 486 | //For all prefixes we need to add the first-hop mac-rewriting flows |
| 487 | addPrefixFlows(prefix, egressInterface, nextHopMacAddress); |
| 488 | } |
| 489 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 490 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 491 | /** |
| 492 | * Add a flow to match dst-IP prefix and rewrite MAC for one IP prefix |
| 493 | * to all other border switches |
| 494 | */ |
| 495 | private void addPrefixFlows(Prefix prefix, Interface egressInterface, |
| 496 | MACAddress nextHopMacAddress) { |
| 497 | log.debug("Adding flows for prefix {}, next hop mac {}", |
| 498 | prefix, nextHopMacAddress); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 499 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 500 | FlowPath flowPath = new FlowPath(); |
| 501 | flowPath.setInstallerId(new CallerId("SDNIP")); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 502 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 503 | // Set flowPath FlowPathType and FlowPathUserState |
| 504 | flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH); |
| 505 | flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 506 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 507 | // Insert dst-ip prefix based forwarding and MAC rewrite flow entry |
| 508 | // only to the first-host switches |
| 509 | FlowPathFlags flowPathFlags = new FlowPathFlags(); |
| 510 | flowPathFlags.setFlags(FlowPathFlags.KEEP_ONLY_FIRST_HOP_ENTRY); |
| 511 | flowPath.setFlowPathFlags(flowPathFlags); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 512 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 513 | // Create the DataPath object: dstSwitchPort |
| 514 | SwitchPort dstPort = new SwitchPort(); |
| 515 | dstPort.setDpid(new Dpid(egressInterface.getDpid())); |
| 516 | dstPort.setPort(new Port(egressInterface.getPort())); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 517 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 518 | // We only need one flow mod per switch, so pick one interface on each switch |
| 519 | Map<Long, Interface> srcInterfaces = new HashMap<Long, Interface>(); |
| 520 | for (Interface intf : interfaces.values()) { |
| 521 | if (!srcInterfaces.containsKey(intf.getDpid()) |
| 522 | && !intf.equals(egressInterface)) { |
| 523 | srcInterfaces.put(intf.getDpid(), intf); |
| 524 | } |
| 525 | } |
| 526 | for (Interface srcInterface : srcInterfaces.values()) { |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 527 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 528 | if (egressInterface.equals(srcInterface)) { |
| 529 | continue; |
| 530 | } |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 531 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 532 | // Create flowPath FlowId |
| 533 | flowPath.setFlowId(new FlowId()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 534 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 535 | // Create DataPath object: srcSwitchPort |
| 536 | SwitchPort srcPort = new SwitchPort(); |
| 537 | srcPort.setDpid(new Dpid(srcInterface.getDpid())); |
| 538 | srcPort.setPort(new Port(srcInterface.getPort())); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 539 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 540 | DataPath dataPath = new DataPath(); |
| 541 | dataPath.setSrcPort(srcPort); |
| 542 | dataPath.setDstPort(dstPort); |
| 543 | flowPath.setDataPath(dataPath); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 544 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 545 | // Create flow path matching condition(s): IPv4 Prefix |
| 546 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 547 | flowEntryMatch.enableEthernetFrameType(Ethernet.TYPE_IPv4); |
| 548 | IPv4Net dstIPv4Net = new IPv4Net(prefix.toString()); |
| 549 | flowEntryMatch.enableDstIPv4Net(dstIPv4Net); |
| 550 | flowPath.setFlowEntryMatch(flowEntryMatch); |
Jonathan Hart | 1912afc | 2013-10-11 12:02:44 +1300 | [diff] [blame] | 551 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 552 | /* |
| 553 | * Create the Flow Entry Action(s): dst-MAC rewrite action |
| 554 | */ |
| 555 | FlowEntryActions flowEntryActions = new FlowEntryActions(); |
| 556 | FlowEntryAction flowEntryAction1 = new FlowEntryAction(); |
| 557 | flowEntryAction1.setActionSetEthernetDstAddr(nextHopMacAddress); |
| 558 | // flowEntryAction1.actionSetEthernetDstAddr(nextHopMacAddress); |
| 559 | flowEntryActions.addAction(flowEntryAction1); |
| 560 | flowPath.setFlowEntryActions(flowEntryActions); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 561 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 562 | // Flow Path installation, only to first hop switches |
| 563 | // TODO: Add the flow by using the new Path Intent framework |
| 564 | /* |
| 565 | if (flowManagerService.addFlow(flowPath) == null) { |
| 566 | log.error("Failed to install flow path to the first hop for " + |
| 567 | "prefix: {}, nextHopMacAddress: {}", prefix.getAddress(), |
| 568 | nextHopMacAddress); |
| 569 | } |
| 570 | else { |
| 571 | log.debug("Successfully installed flow path to the first hop " + |
| 572 | "for prefix: {}, nextHopMacAddress: {}", prefix.getAddress(), |
| 573 | nextHopMacAddress); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 574 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 575 | pushedFlowIds.put(prefix, flowPath.flowId()); |
| 576 | } |
| 577 | */ |
| 578 | } |
| 579 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 580 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 581 | public synchronized void processRibDelete(RibUpdate update) { |
| 582 | Prefix prefix = update.getPrefix(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 583 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 584 | if (ptree.remove(prefix, update.getRibEntry())) { |
| 585 | /* |
| 586 | * Only delete flows if an entry was actually removed from the trie. |
| 587 | * If no entry was removed, the <prefix, nexthop> wasn't there so |
| 588 | * it's probably already been removed and we don't need to do anything |
| 589 | */ |
| 590 | _processDeletePrefix(prefix, update.getRibEntry()); |
| 591 | } |
| 592 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 593 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 594 | private void _processDeletePrefix(Prefix prefix, RibEntry ribEntry) { |
| 595 | deletePrefixFlows(prefix); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 596 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 597 | log.debug("Deleting {} to {}", prefix, ribEntry.getNextHop()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 598 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 599 | if (!bgpPeers.containsKey(ribEntry.getNextHop())) { |
| 600 | log.debug("Getting path for route with non-peer nexthop"); |
| 601 | Path path = prefixToPath.remove(prefix); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 602 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 603 | if (path != null) { |
| 604 | //path could be null if we added to the Ptree but didn't push |
| 605 | //flows yet because we were waiting to resolve ARP |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 606 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 607 | path.decrementUsers(); |
| 608 | if (path.getUsers() <= 0 && !path.isPermanent()) { |
| 609 | deletePath(path); |
| 610 | pushedPaths.remove(path.getDstIpAddress()); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 615 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 616 | // TODO have not tested this module |
| 617 | private void deletePrefixFlows(Prefix prefix) { |
| 618 | log.debug("Deleting flows for prefix {}", prefix); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 619 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 620 | Collection<FlowId> flowIds = pushedFlowIds.removeAll(prefix); |
| 621 | for (FlowId flowId : flowIds) { |
| 622 | // TODO: Delete the flow by using the new Path Intent framework |
| 623 | /* |
| 624 | if (log.isTraceEnabled()) { |
| 625 | //Trace the flow status by flowPath in the switch before deleting it |
| 626 | log.trace("Pushing a DELETE flow mod to flowPath : {}", |
| 627 | flowManagerService.getFlow(flowId).toString()); |
| 628 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 629 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 630 | if( flowManagerService.deleteFlow(flowId)) |
| 631 | { |
| 632 | log.debug("Successfully deleted FlowId: {}",flowId); |
| 633 | } |
| 634 | else |
| 635 | { |
| 636 | log.debug("Failed to delete FlowId: {}",flowId); |
| 637 | } |
| 638 | */ |
| 639 | } |
| 640 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 641 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 642 | // TODO need to record the path and then delete here |
| 643 | private void deletePath(Path path) { |
| 644 | log.debug("Deleting flows for path to {}", |
| 645 | path.getDstIpAddress().getHostAddress()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 646 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 647 | // TODO need update |
| 648 | /*for (PushedFlowMod pfm : path.getFlowMods()) { |
| 649 | if (log.isTraceEnabled()) { |
| 650 | log.trace("Pushing a DELETE flow mod to {}, dst MAC {}", |
| 651 | new Object[] {HexString.toHexString(pfm.getDpid()), |
| 652 | HexString.toHexString(pfm.getFlowMod().getMatch().getDataLayerDestination()) |
| 653 | }); |
| 654 | } |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 655 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 656 | sendDeleteFlowMod(pfm.getFlowMod(), pfm.getDpid()); |
| 657 | }*/ |
| 658 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 659 | |
| 660 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 661 | //TODO test next-hop changes |
| 662 | //TODO check delete/add synchronization |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 663 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 664 | /** |
| 665 | * On startup, we need to calculate a full mesh of paths between all gateway |
| 666 | * switches |
| 667 | */ |
| 668 | private void setupFullMesh() { |
| 669 | //For each border router, calculate and install a path from every other |
| 670 | //border switch to said border router. However, don't install the entry |
| 671 | //in to the first hop switch, as we need to install an entry to rewrite |
| 672 | //for each prefix received. This will be done later when prefixes have |
| 673 | //actually been received. |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 674 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 675 | for (BgpPeer peer : bgpPeers.values()) { |
| 676 | Interface peerInterface = interfaces.get(peer.getInterfaceName()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 677 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 678 | //We know there's not already a Path here pushed, because this is |
| 679 | //called before all other routing |
| 680 | Path path = new Path(peerInterface, peer.getIpAddress()); |
| 681 | path.setPermanent(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 682 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 683 | //See if we know the MAC address of the peer. If not we can't |
| 684 | //do anything until we learn it |
| 685 | // TODO: Fix the code below after deviceStorage was removed |
| 686 | MACAddress macAddress = null; |
| 687 | /* |
| 688 | IDeviceObject nextHopDevice = |
| 689 | deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(peer.getIpAddress())); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 690 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 691 | if(nextHopDevice == null){ |
| 692 | log.debug("There is no DeviceObject for {}", peer.getIpAddress().getHostAddress()); |
| 693 | //Put in the pending paths list first |
| 694 | pathsWaitingOnArp.put(peer.getIpAddress(), path); |
| 695 | proxyArp.sendArpRequest(peer.getIpAddress(), this, true); |
| 696 | continue; |
| 697 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 698 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 699 | macAddress = MACAddress.valueOf(nextHopDevice.getMACAddress()); |
| 700 | */ |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 701 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 702 | if (macAddress == null) { |
| 703 | log.debug("Don't know MAC for {}", peer.getIpAddress().getHostAddress()); |
| 704 | //Put in the pending paths list first |
| 705 | pathsWaitingOnArp.put(peer.getIpAddress(), path); |
| 706 | proxyArp.sendArpRequest(peer.getIpAddress(), this, true); |
| 707 | continue; |
| 708 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 709 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 710 | //If we know the MAC, lets go ahead and push the paths to this peer |
| 711 | calculateAndPushPath(path, macAddress); |
| 712 | } |
| 713 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 714 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 715 | private void calculateAndPushPath(Path path, MACAddress dstMacAddress) { |
| 716 | Interface dstInterface = path.getDstInterface(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 717 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 718 | log.debug("Setting up path to {}, {}", path.getDstIpAddress().getHostAddress(), |
| 719 | dstMacAddress); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 720 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 721 | FlowPath flowPath = new FlowPath(); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 722 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 723 | flowPath.setInstallerId(new CallerId("SDNIP")); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 724 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 725 | // Set flowPath FlowPathType and FlowPathUserState |
| 726 | flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH); |
| 727 | flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 728 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 729 | // Insert the dest-mac based forwarding flow entry to the non-first-hop switches |
| 730 | FlowPathFlags flowPathFlags = new FlowPathFlags(); |
| 731 | flowPathFlags.setFlags(FlowPathFlags.DISCARD_FIRST_HOP_ENTRY); |
| 732 | flowPath.setFlowPathFlags(flowPathFlags); |
| 733 | |
| 734 | // Create the DataPath object: dstSwitchPort |
| 735 | SwitchPort dstPort = new SwitchPort(); |
| 736 | dstPort.setDpid(new Dpid(dstInterface.getDpid())); |
| 737 | dstPort.setPort(new Port(dstInterface.getPort())); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 738 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 739 | for (Interface srcInterface : interfaces.values()) { |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 740 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 741 | if (dstInterface.equals(srcInterface)) { |
| 742 | continue; |
| 743 | } |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 744 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 745 | // Create flowPath FlowId |
| 746 | flowPath.setFlowId(new FlowId()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 747 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 748 | // Create the DataPath object: srcSwitchPort |
| 749 | SwitchPort srcPort = new SwitchPort(); |
| 750 | srcPort.setDpid(new Dpid(srcInterface.getDpid())); |
| 751 | srcPort.setPort(new Port(srcInterface.getPort())); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 752 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 753 | DataPath dataPath = new DataPath(); |
| 754 | dataPath.setSrcPort(srcPort); |
| 755 | dataPath.setDstPort(dstPort); |
| 756 | flowPath.setDataPath(dataPath); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 757 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 758 | // Create the Flow Path Match condition(s) |
| 759 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 760 | flowEntryMatch.enableEthernetFrameType(Ethernet.TYPE_IPv4); |
| 761 | flowEntryMatch.enableDstMac(dstMacAddress); |
| 762 | flowPath.setFlowEntryMatch(flowEntryMatch); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 763 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 764 | // NOTE: No need to add ACTION_OUTPUT. It is implied when creating |
| 765 | // Shortest Path Flow, and is always the last action for the Flow Entries |
| 766 | log.debug("FlowPath of MAC based forwarding: {}", flowPath.toString()); |
| 767 | // TODO: Add the flow by using the new Path Intent framework |
| 768 | /* |
| 769 | if (flowManagerService.addFlow(flowPath) == null) { |
| 770 | log.error("Failed to set up MAC based forwarding path to {}, {}", |
| 771 | path.getDstIpAddress().getHostAddress(),dstMacAddress); |
| 772 | } |
| 773 | else { |
| 774 | log.debug("Successfully set up MAC based forwarding path to {}, {}", |
| 775 | path.getDstIpAddress().getHostAddress(),dstMacAddress); |
| 776 | } |
| 777 | */ |
| 778 | } |
| 779 | } |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 780 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 781 | /** |
| 782 | * Pre-actively install all BGP traffic paths from BGP host attachment point |
| 783 | * in SDN network to all the virtual gateways to BGP peers in other networks |
| 784 | */ |
| 785 | private void setupBgpPaths() { |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 786 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 787 | for (BgpPeer bgpPeer : bgpPeers.values()) { |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 788 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 789 | FlowPath flowPath = new FlowPath(); |
| 790 | flowPath.setInstallerId(new CallerId("SDNIP")); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 791 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 792 | // Set flowPath FlowPathType and FlowPathUserState |
| 793 | flowPath.setFlowPathType(FlowPathType.FP_TYPE_SHORTEST_PATH); |
| 794 | flowPath.setFlowPathUserState(FlowPathUserState.FP_USER_ADD); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 795 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 796 | // Install flow paths between BGPd and its peers |
| 797 | // There is no need to set the FlowPathFlags |
| 798 | flowPath.setFlowPathFlags(new FlowPathFlags(0)); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 799 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 800 | Interface peerInterface = interfaces.get(bgpPeer.getInterfaceName()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 801 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 802 | // Create the Flow Path Match condition(s) |
| 803 | FlowEntryMatch flowEntryMatch = new FlowEntryMatch(); |
| 804 | flowEntryMatch.enableEthernetFrameType(Ethernet.TYPE_IPv4); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 805 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 806 | // Match both source address and dest address |
| 807 | IPv4Net dstIPv4Net = new IPv4Net(bgpPeer.getIpAddress().getHostAddress() + "/32"); |
| 808 | flowEntryMatch.enableDstIPv4Net(dstIPv4Net); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 809 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 810 | IPv4Net srcIPv4Net = new IPv4Net(peerInterface.getIpAddress().getHostAddress() + "/32"); |
| 811 | flowEntryMatch.enableSrcIPv4Net(srcIPv4Net); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 812 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 813 | // Match TCP protocol |
| 814 | flowEntryMatch.enableIpProto(IPv4.PROTOCOL_TCP); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 815 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 816 | // Match destination TCP port |
| 817 | flowEntryMatch.enableDstTcpUdpPort(BGP_PORT); |
| 818 | flowPath.setFlowEntryMatch(flowEntryMatch); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 819 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 820 | /** |
| 821 | * Create the DataPath: BGP -> BGP peer |
| 822 | */ |
| 823 | // Flow path for src-TCP-port |
| 824 | DataPath dataPath = new DataPath(); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 825 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 826 | SwitchPort srcPort = new SwitchPort(); |
| 827 | srcPort.setDpid(bgpdAttachmentPoint.dpid()); |
| 828 | srcPort.setPort(bgpdAttachmentPoint.port()); |
| 829 | dataPath.setSrcPort(srcPort); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 830 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 831 | SwitchPort dstPort = new SwitchPort(); |
| 832 | dstPort.setDpid(new Dpid(peerInterface.getDpid())); |
| 833 | dstPort.setPort(new Port(peerInterface.getSwitchPort().port())); |
| 834 | dataPath.setDstPort(dstPort); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 835 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 836 | flowPath.setDataPath(dataPath); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 837 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 838 | // TODO: Add the flow by using the new Path Intent framework |
| 839 | /* |
| 840 | if (flowManagerService.addFlow(flowPath) == null) { |
| 841 | log.error("Failed to set up path BGP -> peer {}"+"; dst-TCP-port:179", |
| 842 | bgpPeer.getIpAddress().getHostAddress()); |
| 843 | } |
| 844 | else { |
| 845 | log.debug("Successfully set up path BGP -> peer {}"+"; dst-TCP-port:179", |
| 846 | bgpPeer.getIpAddress().getHostAddress()); |
| 847 | } |
| 848 | */ |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 849 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 850 | // Disable dst-TCP-port, and set src-TCP-port |
| 851 | flowEntryMatch.disableDstTcpUdpPort(); |
| 852 | flowEntryMatch.enableSrcTcpUdpPort(BGP_PORT); |
| 853 | flowPath.setFlowEntryMatch(flowEntryMatch); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 854 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 855 | // Create a new FlowId |
| 856 | flowPath.setFlowId(new FlowId()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 857 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 858 | // TODO: Add the flow by using the new Path Intent framework |
| 859 | /* |
| 860 | if (flowManagerService.addFlow(flowPath) == null) { |
| 861 | log.error("Failed to set up path BGP -> Peer {}" + "; src-TCP-port:179", |
| 862 | bgpPeer.getIpAddress().getHostAddress()); |
| 863 | } |
| 864 | else { |
| 865 | log.debug("Successfully set up path BGP -> Peer {}" + "; src-TCP-port:179", |
| 866 | bgpPeer.getIpAddress().getHostAddress()); |
| 867 | } |
| 868 | */ |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 869 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 870 | /** |
| 871 | * Create the DataPath: BGP <-BGP peer |
| 872 | */ |
| 873 | // Reversed BGP flow path for src-TCP-port |
| 874 | flowPath.setFlowId(new FlowId()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 875 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 876 | DataPath reverseDataPath = new DataPath(); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 877 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 878 | SwitchPort reverseDstPort = new SwitchPort(); |
| 879 | reverseDstPort.setDpid(bgpdAttachmentPoint.dpid()); |
| 880 | reverseDstPort.setPort(bgpdAttachmentPoint.port()); |
| 881 | reverseDataPath.setDstPort(reverseDstPort); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 882 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 883 | SwitchPort reverseSrcPort = new SwitchPort(); |
| 884 | reverseSrcPort.setDpid(new Dpid(peerInterface.getDpid())); |
| 885 | reverseSrcPort.setPort(new Port(peerInterface.getSwitchPort().port())); |
| 886 | reverseDataPath.setSrcPort(reverseSrcPort); |
| 887 | flowPath.setDataPath(reverseDataPath); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 888 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 889 | // reverse the dst IP and src IP addresses |
| 890 | flowEntryMatch.enableDstIPv4Net(srcIPv4Net); |
| 891 | flowEntryMatch.enableSrcIPv4Net(dstIPv4Net); |
| 892 | flowPath.setFlowEntryMatch(flowEntryMatch); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 893 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 894 | log.debug("Reversed BGP FlowPath: {}", flowPath.toString()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 895 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 896 | // TODO: Add the flow by using the new Path Intent framework |
| 897 | /* |
| 898 | if (flowManagerService.addFlow(flowPath) == null) { |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 899 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 900 | log.error("Failed to set up path BGP <- Peer {}" + "; src-TCP-port:179", |
| 901 | bgpPeer.getIpAddress().getHostAddress()); |
| 902 | } |
| 903 | else { |
| 904 | log.debug("Successfully set up path BGP <- Peer {}" + "; src-TCP-port:179", |
| 905 | bgpPeer.getIpAddress().getHostAddress()); |
| 906 | } |
| 907 | */ |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 908 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 909 | // Reversed BGP flow path for dst-TCP-port |
| 910 | flowPath.setFlowId(new FlowId()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 911 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 912 | // Disable src-TCP-port, and set the dst-TCP-port |
| 913 | flowEntryMatch.disableSrcTcpUdpPort(); |
| 914 | flowEntryMatch.enableDstTcpUdpPort(BGP_PORT); |
| 915 | flowPath.setFlowEntryMatch(flowEntryMatch); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 916 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 917 | log.debug("Reversed BGP FlowPath: {}", flowPath.toString()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 918 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 919 | // TODO: Add the flow by using the new Path Intent framework |
| 920 | /* |
| 921 | if (flowManagerService.addFlow(flowPath) == null) { |
| 922 | log.error("Failed to setting up path BGP <- Peer {}" + "; dst-TCP-port:179", |
| 923 | bgpPeer.getIpAddress().getHostAddress()); |
| 924 | } |
| 925 | else { |
| 926 | log.debug("Successfully setting up path BGP <- Peer {}" + "; dst-TCP-port:179", |
| 927 | bgpPeer.getIpAddress().getHostAddress()); |
| 928 | } |
| 929 | */ |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 930 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 931 | /** |
| 932 | * ICMP paths between BGPd and its peers |
| 933 | */ |
| 934 | //match ICMP protocol BGP <- Peer |
| 935 | flowPath.setFlowId(new FlowId()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 936 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 937 | flowEntryMatch.enableIpProto(IPv4.PROTOCOL_ICMP); |
| 938 | flowEntryMatch.disableSrcTcpUdpPort(); |
| 939 | flowEntryMatch.disableDstTcpUdpPort(); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 940 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 941 | flowPath.setFlowEntryMatch(flowEntryMatch); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 942 | |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame^] | 943 | flowPath.setDataPath(reverseDataPath); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 944 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 945 | log.debug("Reversed ICMP FlowPath: {}", flowPath.toString()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 946 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 947 | // TODO: Add the flow by using the new Path Intent framework |
| 948 | /* |
| 949 | if (flowManagerService.addFlow(flowPath) == null) { |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 950 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 951 | log.error("Failed to set up ICMP path BGP <- Peer {}", |
| 952 | bgpPeer.getIpAddress().getHostAddress()); |
| 953 | } |
| 954 | else { |
| 955 | log.debug("Successfully set up ICMP path BGP <- Peer {}", |
| 956 | bgpPeer.getIpAddress().getHostAddress()); |
| 957 | } |
| 958 | */ |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 959 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 960 | //match ICMP protocol BGP -> Peer |
| 961 | flowPath.setFlowId(new FlowId()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 962 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 963 | flowEntryMatch.enableDstIPv4Net(dstIPv4Net); |
| 964 | flowEntryMatch.enableSrcIPv4Net(srcIPv4Net); |
| 965 | flowPath.setFlowEntryMatch(flowEntryMatch); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 966 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 967 | flowPath.setDataPath(dataPath); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 968 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 969 | log.debug("ICMP flowPath: {}", flowPath.toString()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 970 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 971 | // TODO: Add the flow by using the new Path Intent framework |
| 972 | /* |
| 973 | if (flowManagerService.addFlow(flowPath) == null) { |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 974 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 975 | log.error("Failed to set up ICMP path BGP -> Peer {}", |
| 976 | bgpPeer.getIpAddress().getHostAddress()); |
| 977 | } |
| 978 | else { |
| 979 | log.debug("Successfully set up ICMP path BGP -> Peer {}", |
| 980 | bgpPeer.getIpAddress().getHostAddress()); |
| 981 | } |
| 982 | */ |
| 983 | } |
| 984 | } |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 985 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 986 | @Override |
| 987 | public void arpResponse(InetAddress ipAddress, MACAddress macAddress) { |
| 988 | log.debug("Received ARP response: {} => {}", |
| 989 | ipAddress.getHostAddress(), macAddress); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 990 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 991 | /* |
| 992 | * We synchronize on this to prevent changes to the ptree while we're pushing |
| 993 | * flows to the switches. If the ptree changes, the ptree and switches |
| 994 | * could get out of sync. |
| 995 | */ |
| 996 | synchronized (this) { |
| 997 | Path path = pathsWaitingOnArp.remove(ipAddress); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 998 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 999 | if (path != null) { |
| 1000 | log.debug("Pushing path to {} at {} on {}", new Object[]{ |
| 1001 | path.getDstIpAddress().getHostAddress(), macAddress, |
| 1002 | path.getDstInterface().getSwitchPort()}); |
| 1003 | //These paths should always be to BGP peers. Paths to non-peers are |
| 1004 | //handled once the first prefix is ready to push |
| 1005 | if (pushedPaths.containsKey(path.getDstIpAddress())) { |
| 1006 | //A path already got pushed to this endpoint while we were waiting |
| 1007 | //for ARP. We'll copy over the permanent attribute if it is set on this path. |
| 1008 | if (path.isPermanent()) { |
| 1009 | pushedPaths.get(path.getDstIpAddress()).setPermanent(); |
| 1010 | } |
| 1011 | } else { |
| 1012 | calculateAndPushPath(path, macAddress); |
| 1013 | pushedPaths.put(path.getDstIpAddress(), path); |
| 1014 | } |
| 1015 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1016 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1017 | Set<RibUpdate> prefixesToPush = prefixesWaitingOnArp.removeAll(ipAddress); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1018 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1019 | for (RibUpdate update : prefixesToPush) { |
| 1020 | //These will always be adds |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1021 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1022 | RibEntry rib = ptree.lookup(update.getPrefix()); |
| 1023 | if (rib != null && rib.equals(update.getRibEntry())) { |
| 1024 | log.debug("Pushing prefix {} next hop {}", update.getPrefix(), |
| 1025 | rib.getNextHop().getHostAddress()); |
| 1026 | //We only push prefix flows if the prefix is still in the ptree |
| 1027 | //and the next hop is the same as our update. The prefix could |
| 1028 | //have been removed while we were waiting for the ARP, or the |
| 1029 | //next hop could have changed. |
| 1030 | _processRibAdd(update); |
| 1031 | } else { |
| 1032 | log.debug("Received ARP response, but {},{} is no longer in ptree", |
| 1033 | update.getPrefix(), update.getRibEntry()); |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1038 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1039 | //TODO wait the priority module of the flow Manager |
| 1040 | private void setupArpFlows() { |
| 1041 | OFMatch match = new OFMatch(); |
| 1042 | match.setDataLayerType(Ethernet.TYPE_ARP); |
| 1043 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1044 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1045 | OFFlowMod fm = new OFFlowMod(); |
| 1046 | fm.setMatch(match); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1047 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1048 | OFActionOutput action = new OFActionOutput(); |
| 1049 | action.setPort(OFPort.OFPP_CONTROLLER.getValue()); |
| 1050 | action.setMaxLength((short) 0xffff); |
| 1051 | List<OFAction> actions = new ArrayList<OFAction>(1); |
| 1052 | actions.add(action); |
| 1053 | fm.setActions(actions); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1054 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1055 | fm.setIdleTimeout((short) 0) |
| 1056 | .setHardTimeout((short) 0) |
| 1057 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 1058 | .setCookie(0) |
| 1059 | .setCommand(OFFlowMod.OFPFC_ADD) |
| 1060 | .setPriority(ARP_PRIORITY) |
| 1061 | .setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1062 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1063 | for (String strdpid : switches) { |
| 1064 | flowCache.write(HexString.toLong(strdpid), fm); |
| 1065 | } |
| 1066 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1067 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1068 | //TODO need update, waiting for the priority feature from flow Manager |
| 1069 | private void setupDefaultDropFlows() { |
| 1070 | OFFlowMod fm = new OFFlowMod(); |
| 1071 | fm.setMatch(new OFMatch()); |
| 1072 | fm.setActions(new ArrayList<OFAction>()); //No action means drop |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1073 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1074 | fm.setIdleTimeout((short) 0) |
| 1075 | .setHardTimeout((short) 0) |
| 1076 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 1077 | .setCookie(0) |
| 1078 | .setCommand(OFFlowMod.OFPFC_ADD) |
| 1079 | .setPriority((short) 0) |
| 1080 | .setLengthU(OFFlowMod.MINIMUM_LENGTH); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1081 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1082 | OFFlowMod fmLLDP; |
| 1083 | OFFlowMod fmBDDP; |
| 1084 | try { |
| 1085 | fmLLDP = fm.clone(); |
| 1086 | fmBDDP = fm.clone(); |
| 1087 | } catch (CloneNotSupportedException e1) { |
| 1088 | log.error("Error cloning flow mod", e1); |
| 1089 | return; |
| 1090 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1091 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1092 | OFMatch matchLLDP = new OFMatch(); |
| 1093 | matchLLDP.setDataLayerType((short) 0x88cc); |
| 1094 | matchLLDP.setWildcards(matchLLDP.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 1095 | fmLLDP.setMatch(matchLLDP); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1096 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1097 | OFMatch matchBDDP = new OFMatch(); |
| 1098 | matchBDDP.setDataLayerType((short) 0x8942); |
| 1099 | matchBDDP.setWildcards(matchBDDP.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 1100 | fmBDDP.setMatch(matchBDDP); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1101 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1102 | OFActionOutput action = new OFActionOutput(); |
| 1103 | action.setPort(OFPort.OFPP_CONTROLLER.getValue()); |
| 1104 | action.setMaxLength((short) 0xffff); |
| 1105 | List<OFAction> actions = new ArrayList<OFAction>(1); |
| 1106 | actions.add(action); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1107 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1108 | fmLLDP.setActions(actions); |
| 1109 | fmBDDP.setActions(actions); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1110 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1111 | fmLLDP.setPriority(ARP_PRIORITY); |
| 1112 | fmLLDP.setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
| 1113 | fmBDDP.setPriority(ARP_PRIORITY); |
| 1114 | fmBDDP.setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1115 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1116 | List<OFFlowMod> flowModList = new ArrayList<OFFlowMod>(3); |
| 1117 | flowModList.add(fm); |
| 1118 | flowModList.add(fmLLDP); |
| 1119 | flowModList.add(fmBDDP); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1120 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1121 | for (String strdpid : switches) { |
| 1122 | flowCache.write(HexString.toLong(strdpid), flowModList); |
| 1123 | } |
| 1124 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1125 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1126 | private void beginRouting() { |
| 1127 | log.debug("Topology is now ready, beginning routing function"); |
| 1128 | // TODO: Fix for the new Topology Network Graph |
| 1129 | // topology = topologyNetService.newDatabaseTopology(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1130 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1131 | // Wait Pavlin's API. We need the following functions. |
| 1132 | /*setupArpFlows(); |
| 1133 | setupDefaultDropFlows();*/ |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1134 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1135 | setupBgpPaths(); |
| 1136 | setupFullMesh(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1137 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1138 | //Suppress link discovery on external-facing router ports |
| 1139 | for (Interface intf : interfaces.values()) { |
| 1140 | linkDiscoveryService.AddToSuppressLLDPs(intf.getDpid(), intf.getPort()); |
| 1141 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1142 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1143 | bgpUpdatesExecutor.execute(new Runnable() { |
| 1144 | @Override |
| 1145 | public void run() { |
| 1146 | doUpdatesThread(); |
| 1147 | } |
| 1148 | }); |
| 1149 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1150 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1151 | // Before inserting the paths for BGP traffic, we should check |
| 1152 | // whether all the switches in the configure file are discovered by onos. |
| 1153 | private void checkSwitchesConnected() { |
| 1154 | for (String dpid : switches) { |
| 1155 | // TODO: Fix the code below after topoSwitchSerice was removed |
| 1156 | /* |
| 1157 | Iterator<ISwitchObject> activeSwitches = topoSwitchService. |
| 1158 | getActiveSwitches().iterator(); |
| 1159 | while(activeSwitches.hasNext()) |
| 1160 | { |
| 1161 | ISwitchObject switchObject = activeSwitches.next(); |
| 1162 | if (switchObject.getDPID().equals(dpid)) { |
| 1163 | break; |
| 1164 | } |
| 1165 | if(activeSwitches.hasNext() == false) { |
| 1166 | log.debug("Not all switches are here yet"); |
| 1167 | return; |
| 1168 | } |
| 1169 | } |
| 1170 | */ |
| 1171 | } |
| 1172 | switchesConnected = true; |
| 1173 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1174 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1175 | //Actually we only need to go half way round to verify full mesh connectivity |
| 1176 | //(n^2)/2 |
| 1177 | private void checkTopologyReady() { |
| 1178 | for (Interface dstInterface : interfaces.values()) { |
| 1179 | for (Interface srcInterface : interfaces.values()) { |
| 1180 | if (dstInterface.equals(srcInterface)) { |
| 1181 | continue; |
| 1182 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1183 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1184 | // TODO: Fix for the new Topology Network Graph |
| 1185 | /* |
| 1186 | DataPath shortestPath = topologyNetService.getDatabaseShortestPath( |
| 1187 | srcInterface.getSwitchPort(), dstInterface.getSwitchPort()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1188 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1189 | if (shortestPath == null){ |
| 1190 | log.debug("Shortest path between {} and {} not found", |
| 1191 | srcInterface.getSwitchPort(), dstInterface.getSwitchPort()); |
| 1192 | return; |
| 1193 | } |
| 1194 | */ |
| 1195 | } |
| 1196 | } |
| 1197 | topologyReady = true; |
| 1198 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1199 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1200 | private void checkStatus() { |
| 1201 | if (!switchesConnected) { |
| 1202 | checkSwitchesConnected(); |
| 1203 | } |
| 1204 | boolean oldTopologyReadyStatus = topologyReady; |
| 1205 | if (switchesConnected && !topologyReady) { |
| 1206 | checkTopologyReady(); |
| 1207 | } |
| 1208 | if (!oldTopologyReadyStatus && topologyReady) { |
| 1209 | beginRouting(); |
| 1210 | } |
| 1211 | } |
Jonathan Hart | a23ffdb | 2013-08-14 14:36:54 +1200 | [diff] [blame] | 1212 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1213 | private void doUpdatesThread() { |
| 1214 | boolean interrupted = false; |
| 1215 | try { |
| 1216 | while (true) { |
| 1217 | try { |
| 1218 | RibUpdate update = ribUpdates.take(); |
| 1219 | switch (update.getOperation()) { |
| 1220 | case UPDATE: |
| 1221 | if (validateUpdate(update)) { |
| 1222 | processRibAdd(update); |
| 1223 | } else { |
| 1224 | log.debug("Rib UPDATE out of order: {} via {}", |
| 1225 | update.getPrefix(), update.getRibEntry().getNextHop()); |
| 1226 | } |
| 1227 | break; |
| 1228 | case DELETE: |
| 1229 | if (validateUpdate(update)) { |
| 1230 | processRibDelete(update); |
| 1231 | } else { |
| 1232 | log.debug("Rib DELETE out of order: {} via {}", |
| 1233 | update.getPrefix(), update.getRibEntry().getNextHop()); |
| 1234 | } |
| 1235 | break; |
| 1236 | } |
| 1237 | } catch (InterruptedException e) { |
| 1238 | log.debug("Interrupted while taking from updates queue", e); |
| 1239 | interrupted = true; |
| 1240 | } catch (Exception e) { |
| 1241 | log.debug("exception", e); |
| 1242 | } |
| 1243 | } |
| 1244 | } finally { |
| 1245 | if (interrupted) { |
| 1246 | Thread.currentThread().interrupt(); |
| 1247 | } |
| 1248 | } |
| 1249 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1250 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1251 | private boolean validateUpdate(RibUpdate update) { |
| 1252 | RibEntry newEntry = update.getRibEntry(); |
| 1253 | RibEntry oldEntry = ptree.lookup(update.getPrefix()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1254 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1255 | //If there is no existing entry we must assume this is the most recent |
| 1256 | //update. However this might not always be the case as we might have a |
| 1257 | //POST then DELETE reordering. |
| 1258 | //if (oldEntry == null || !newEntry.getNextHop().equals(oldEntry.getNextHop())) { |
| 1259 | if (oldEntry == null) { |
| 1260 | return true; |
| 1261 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1262 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1263 | // This handles the case where routes are gathered in the initial |
| 1264 | // request because they don't have sequence number info |
| 1265 | if (newEntry.getSysUpTime() == -1 && newEntry.getSequenceNum() == -1) { |
| 1266 | return true; |
| 1267 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1268 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1269 | if (newEntry.getSysUpTime() > oldEntry.getSysUpTime()) { |
| 1270 | return true; |
| 1271 | } else if (newEntry.getSysUpTime() == oldEntry.getSysUpTime()) { |
| 1272 | if (newEntry.getSequenceNum() > oldEntry.getSequenceNum()) { |
| 1273 | return true; |
| 1274 | } else { |
| 1275 | return false; |
| 1276 | } |
| 1277 | } else { |
| 1278 | return false; |
| 1279 | } |
| 1280 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1281 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1282 | // The code below should be reimplemented after removal of Floodlight's |
| 1283 | // ITopologyService API. It should be implemented on top of network graph |
| 1284 | // notifications. (It was pretty hacky anyway...) |
| 1285 | /* |
| 1286 | @Override |
| 1287 | public void topologyChanged() { |
| 1288 | if (topologyReady) { |
| 1289 | return; |
| 1290 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1291 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1292 | boolean refreshNeeded = false; |
| 1293 | for (LDUpdate ldu : topologyService.getLastLinkUpdates()){ |
| 1294 | if (!ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.LINK_UPDATED)){ |
| 1295 | //We don't need to recalculate anything for just link updates |
| 1296 | //They happen very frequently |
| 1297 | refreshNeeded = true; |
| 1298 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1299 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1300 | log.debug("Topo change {}", ldu.getOperation()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1301 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1302 | if (ldu.getOperation().equals(ILinkDiscovery.UpdateOperation.LINK_ADDED)){ |
| 1303 | synchronized (linkUpdates) { |
| 1304 | linkUpdates.add(ldu); |
| 1305 | } |
| 1306 | } |
| 1307 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1308 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1309 | if (refreshNeeded && !topologyReady){ |
| 1310 | topologyChangeDetectorTask.reschedule(TOPO_DETECTION_WAIT, TimeUnit.SECONDS); |
| 1311 | } |
| 1312 | } |
| 1313 | */ |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 1314 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1315 | @Override |
| 1316 | public void addedSwitch(IOFSwitch sw) { |
| 1317 | if (!topologyReady) { |
| 1318 | sw.clearAllFlowMods(); |
| 1319 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1320 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1321 | flowCache.switchConnected(sw); |
| 1322 | } |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 1323 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1324 | @Override |
| 1325 | public void removedSwitch(IOFSwitch sw) { |
| 1326 | } |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 1327 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1328 | @Override |
| 1329 | public void switchPortChanged(Long switchId) { |
| 1330 | } |
Jonathan Hart | 64c0b20 | 2013-08-20 15:45:07 +1200 | [diff] [blame] | 1331 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1332 | @Override |
| 1333 | public String getName() { |
| 1334 | return "BgpRoute"; |
| 1335 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1336 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1337 | /* |
| 1338 | * IConfigInfoService methods |
| 1339 | */ |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1340 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1341 | @Override |
| 1342 | public boolean isInterfaceAddress(InetAddress address) { |
| 1343 | Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32)); |
| 1344 | return (intf != null && intf.getIpAddress().equals(address)); |
| 1345 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1346 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1347 | @Override |
| 1348 | public boolean inConnectedNetwork(InetAddress address) { |
| 1349 | Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32)); |
| 1350 | return (intf != null && !intf.getIpAddress().equals(address)); |
| 1351 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1352 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1353 | @Override |
| 1354 | public boolean fromExternalNetwork(long inDpid, short inPort) { |
| 1355 | for (Interface intf : interfaces.values()) { |
| 1356 | if (intf.getDpid() == inDpid && intf.getPort() == inPort) { |
| 1357 | return true; |
| 1358 | } |
| 1359 | } |
| 1360 | return false; |
| 1361 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1362 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1363 | @Override |
| 1364 | public Interface getOutgoingInterface(InetAddress dstIpAddress) { |
| 1365 | return interfacePtrie.match(new Prefix(dstIpAddress.getAddress(), 32)); |
| 1366 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1367 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1368 | @Override |
| 1369 | public boolean hasLayer3Configuration() { |
| 1370 | return !interfaces.isEmpty(); |
| 1371 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1372 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1373 | @Override |
| 1374 | public MACAddress getRouterMacAddress() { |
| 1375 | return bgpdMacAddress; |
| 1376 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 1377 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1378 | @Override |
| 1379 | public short getVlan() { |
| 1380 | return vlan; |
| 1381 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1382 | } |