Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.sdnip; |
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; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 9 | import java.util.HashSet; |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 10 | import java.util.Iterator; |
Jonathan Hart | e7e1c6e | 2013-06-04 20:50:23 -0700 | [diff] [blame] | 11 | import java.util.List; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 12 | import java.util.Map; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 13 | import java.util.Set; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 14 | import java.util.concurrent.BlockingQueue; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 15 | import java.util.concurrent.ConcurrentHashMap; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 16 | import java.util.concurrent.ExecutorService; |
Jonathan Hart | 98957bf | 2013-07-01 14:49:24 +1200 | [diff] [blame] | 17 | import java.util.concurrent.Executors; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 18 | import java.util.concurrent.LinkedBlockingQueue; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 19 | |
| 20 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 21 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 22 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 23 | import net.floodlightcontroller.core.module.IFloodlightService; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 24 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 25 | import net.floodlightcontroller.util.MACAddress; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 26 | import net.onrc.onos.api.newintent.IntentService; |
| 27 | import net.onrc.onos.api.newintent.MultiPointToSinglePointIntent; |
Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 28 | import net.onrc.onos.apps.proxyarp.IArpRequester; |
| 29 | import net.onrc.onos.apps.proxyarp.IProxyArpService; |
Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 30 | import net.onrc.onos.apps.sdnip.RibUpdate.Operation; |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 31 | import net.onrc.onos.apps.sdnip.web.SdnIpWebRoutable; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 32 | import net.onrc.onos.apps.sdnip.web.SdnIpWebRoutableNew; |
| 33 | import net.onrc.onos.core.intent.IntentOperation; |
| 34 | import net.onrc.onos.core.intent.IntentOperationList; |
| 35 | import net.onrc.onos.core.intent.ShortestPathIntent; |
| 36 | import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 37 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService; |
Jonathan Hart | 51f6f5b | 2014-04-03 10:32:10 -0700 | [diff] [blame] | 38 | import net.onrc.onos.core.main.config.IConfigInfoService; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 39 | import net.onrc.onos.core.matchaction.action.ModifyDstMacAction; |
| 40 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
| 41 | import net.onrc.onos.core.matchaction.match.PacketMatchBuilder; |
| 42 | import net.onrc.onos.core.newintent.IdBlockAllocatorBasedIntentIdGenerator; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 43 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 44 | import net.onrc.onos.core.util.Dpid; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 45 | import net.onrc.onos.core.util.IPv4; |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 46 | import net.onrc.onos.core.util.PortNumber; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 47 | import net.onrc.onos.core.util.SwitchPort; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 48 | import net.sf.json.JSONArray; |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 49 | import net.sf.json.JSONException; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 50 | import net.sf.json.JSONObject; |
| 51 | import net.sf.json.JSONSerializer; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 52 | |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 53 | import org.apache.commons.configuration.ConfigurationRuntimeException; |
Jonathan Hart | d1f2325 | 2013-06-13 15:17:05 +1200 | [diff] [blame] | 54 | import org.codehaus.jackson.JsonParseException; |
| 55 | import org.codehaus.jackson.map.JsonMappingException; |
| 56 | import org.codehaus.jackson.map.ObjectMapper; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | import org.slf4j.LoggerFactory; |
| 59 | |
Jonathan Hart | 4dfc365 | 2013-08-02 20:22:36 +1200 | [diff] [blame] | 60 | import com.google.common.collect.HashMultimap; |
| 61 | import com.google.common.collect.Multimaps; |
| 62 | import com.google.common.collect.SetMultimap; |
Jonathan Hart | 309889c | 2013-08-13 23:26:24 +1200 | [diff] [blame] | 63 | import com.google.common.net.InetAddresses; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 64 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 65 | import com.googlecode.concurrenttrees.radix.RadixTree; |
| 66 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 67 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 68 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
Jonathan Hart | 8b9349e | 2013-07-26 15:55:28 +1200 | [diff] [blame] | 69 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 70 | /** |
| 71 | * This class sets up BGP paths, handles RIB updates and relative intents. |
| 72 | * TODO: Thread-safe. |
| 73 | */ |
Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 74 | public class SdnIp implements IFloodlightModule, ISdnIpService, |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 75 | IArpRequester, IConfigInfoService { |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 76 | |
Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 77 | private static final Logger log = LoggerFactory.getLogger(SdnIp.class); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 78 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 79 | private ILinkDiscoveryService linkDiscoveryService; |
| 80 | private IRestApiService restApi; |
| 81 | private IProxyArpService proxyArp; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 82 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 83 | private InvertedRadixTree<RibEntry> bgpRoutes; |
| 84 | private InvertedRadixTree<Interface> interfaceRoutes; |
| 85 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | private BlockingQueue<RibUpdate> ribUpdates; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 87 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | private String bgpdRestIp; |
| 89 | private String routerId; |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 90 | private static final String DEFAULT_CONFIG_FILENAME = "config.json"; |
| 91 | private String currentConfigFilename = DEFAULT_CONFIG_FILENAME; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 92 | |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 93 | /* ShortestPath Intent Variables */ |
Sho SHIMIZU | 9ec3982 | 2014-07-10 17:15:12 -0700 | [diff] [blame] | 94 | private final String caller = "SdnIp"; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 95 | private IControllerRegistryService controllerRegistryService; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 96 | private IntentService intentService; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 97 | private IPathCalcRuntimeService pathRuntime; |
| 98 | /* Shortest Intent Path Variables */ |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 99 | private IdBlockAllocatorBasedIntentIdGenerator intentIdGenerator; |
| 100 | //private static final short ARP_PRIORITY = 20; |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 101 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 102 | //private static final short BGP_PORT = 179; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 103 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 104 | // Configuration stuff |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | private List<String> switches; |
| 106 | private Map<String, Interface> interfaces; |
| 107 | private Map<InetAddress, BgpPeer> bgpPeers; |
| 108 | private SwitchPort bgpdAttachmentPoint; |
| 109 | private MACAddress bgpdMacAddress; |
| 110 | private short vlan; |
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 | private SetMultimap<InetAddress, RibUpdate> prefixesWaitingOnArp; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 113 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 114 | private ExecutorService bgpUpdatesExecutor; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 115 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 116 | private ConcurrentHashMap<Prefix, MultiPointToSinglePointIntent> pushedRouteIntents; |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 117 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 118 | /** |
| 119 | * SDN-IP application has a configuration file. This method is to read all |
| 120 | * the info from this file. |
| 121 | * |
| 122 | * @param configFilename the name of configuration file for SDN-IP application |
| 123 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 124 | private void readConfiguration(String configFilename) { |
| 125 | File gatewaysFile = new File(configFilename); |
| 126 | ObjectMapper mapper = new ObjectMapper(); |
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 | try { |
| 129 | Configuration config = mapper.readValue(gatewaysFile, Configuration.class); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 130 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 131 | switches = config.getSwitches(); |
Sho SHIMIZU | 84a72de | 2014-07-09 07:56:40 -0700 | [diff] [blame] | 132 | interfaces = new HashMap<>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 133 | for (Interface intf : config.getInterfaces()) { |
| 134 | interfaces.put(intf.getName(), intf); |
| 135 | } |
Sho SHIMIZU | 84a72de | 2014-07-09 07:56:40 -0700 | [diff] [blame] | 136 | bgpPeers = new HashMap<>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | for (BgpPeer peer : config.getPeers()) { |
| 138 | bgpPeers.put(peer.getIpAddress(), peer); |
| 139 | } |
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 | bgpdAttachmentPoint = new SwitchPort( |
| 142 | new Dpid(config.getBgpdAttachmentDpid()), |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame] | 143 | PortNumber.uint16(config.getBgpdAttachmentPort())); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 144 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 145 | bgpdMacAddress = config.getBgpdMacAddress(); |
| 146 | vlan = config.getVlan(); |
Sho SHIMIZU | dde5a0b | 2014-07-09 10:42:23 -0700 | [diff] [blame] | 147 | } catch (JsonParseException | JsonMappingException e) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 148 | log.error("Error in JSON file", e); |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 149 | throw new ConfigurationRuntimeException("Error in JSON file", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 150 | } catch (IOException e) { |
| 151 | log.error("Error reading JSON file", e); |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 152 | throw new ConfigurationRuntimeException("Error in JSON file", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 153 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 154 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 155 | // Populate the interface InvertedRadixTree |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 156 | for (Interface intf : interfaces.values()) { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 157 | Prefix prefix = new Prefix(intf.getIpAddress().getAddress(), |
| 158 | intf.getPrefixLength()); |
| 159 | interfaceRoutes.put(prefix.toBinaryString(), intf); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 162 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 163 | @Override |
| 164 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 165 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(); |
Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 166 | l.add(ISdnIpService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 167 | l.add(IConfigInfoService.class); |
| 168 | return l; |
| 169 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 170 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 171 | @Override |
| 172 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 173 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>(); |
Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 174 | m.put(ISdnIpService.class, this); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 175 | m.put(IConfigInfoService.class, this); |
| 176 | return m; |
| 177 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 178 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 179 | @Override |
| 180 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 181 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 182 | l.add(IRestApiService.class); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 183 | l.add(IControllerRegistryService.class); |
| 184 | l.add(IPathCalcRuntimeService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 185 | return l; |
| 186 | } |
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 | @Override |
| 189 | public void init(FloodlightModuleContext context) |
| 190 | throws FloodlightModuleException { |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 191 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 192 | bgpRoutes = new ConcurrentInvertedRadixTree<>( |
| 193 | new DefaultByteArrayNodeFactory()); |
| 194 | interfaceRoutes = new ConcurrentInvertedRadixTree<>( |
| 195 | new DefaultByteArrayNodeFactory()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 196 | |
Sho SHIMIZU | 84a72de | 2014-07-09 07:56:40 -0700 | [diff] [blame] | 197 | ribUpdates = new LinkedBlockingQueue<>(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 198 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 199 | // Register REST handler. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 200 | restApi = context.getServiceImpl(IRestApiService.class); |
| 201 | proxyArp = context.getServiceImpl(IProxyArpService.class); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 202 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 203 | controllerRegistryService = context |
| 204 | .getServiceImpl(IControllerRegistryService.class); |
pingping-lin | bec31fc | 2014-08-28 11:48:41 -0700 | [diff] [blame^] | 205 | pathRuntime = context.getServiceImpl(IPathCalcRuntimeService.class); |
| 206 | linkDiscoveryService = context.getServiceImpl(ILinkDiscoveryService.class); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 207 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 208 | intentIdGenerator = new IdBlockAllocatorBasedIntentIdGenerator( |
| 209 | controllerRegistryService); |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 210 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 211 | // TODO: initialize intentService |
| 212 | |
| 213 | pushedRouteIntents = new ConcurrentHashMap<>(); |
| 214 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 215 | prefixesWaitingOnArp = Multimaps.synchronizedSetMultimap( |
| 216 | HashMultimap.<InetAddress, RibUpdate>create()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 217 | |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 218 | //flowCache = new FlowCache(floodlightProvider); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 219 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 220 | bgpUpdatesExecutor = Executors.newSingleThreadExecutor( |
| 221 | new ThreadFactoryBuilder().setNameFormat("bgp-updates-%d").build()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 222 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 223 | // Read in config values |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 224 | bgpdRestIp = context.getConfigParams(this).get("BgpdRestIp"); |
| 225 | if (bgpdRestIp == null) { |
| 226 | log.error("BgpdRestIp property not found in config file"); |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 227 | throw new ConfigurationRuntimeException( |
| 228 | "BgpdRestIp property not found in config file"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 229 | } else { |
| 230 | log.info("BgpdRestIp set to {}", bgpdRestIp); |
| 231 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 232 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 233 | routerId = context.getConfigParams(this).get("RouterId"); |
| 234 | if (routerId == null) { |
| 235 | log.error("RouterId property not found in config file"); |
Jonathan Hart | ec9ee2e | 2014-04-08 22:45:44 -0700 | [diff] [blame] | 236 | throw new ConfigurationRuntimeException( |
| 237 | "RouterId property not found in config file"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 238 | } else { |
| 239 | log.info("RouterId set to {}", routerId); |
| 240 | } |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 241 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 242 | String configFilenameParameter = context.getConfigParams(this).get("configfile"); |
| 243 | if (configFilenameParameter != null) { |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 244 | currentConfigFilename = configFilenameParameter; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 245 | } |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 246 | log.debug("Config file set to {}", currentConfigFilename); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 247 | |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 248 | readConfiguration(currentConfigFilename); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 249 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 250 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 251 | @Override |
| 252 | public void startUp(FloodlightModuleContext context) { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 253 | restApi.addRestletRoutable(new SdnIpWebRoutable()); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 254 | restApi.addRestletRoutable(new SdnIpWebRoutableNew()); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 255 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 256 | // Retrieve the RIB from BGPd during startup |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 257 | retrieveRib(); |
| 258 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 259 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 260 | @Override |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 261 | public RadixTree<RibEntry> getPtree() { |
| 262 | return bgpRoutes; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 263 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 264 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 265 | @Override |
| 266 | public void clearPtree() { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 267 | log.warn("Clear table operation not supported"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 268 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 269 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 270 | @Override |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 271 | public String getBgpdRestIp() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 272 | return bgpdRestIp; |
| 273 | } |
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 | @Override |
| 276 | public String getRouterId() { |
| 277 | return routerId; |
| 278 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 279 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 280 | /** |
| 281 | * SDN-IP application will fetch all rib entries from BGPd when it starts. |
| 282 | * Especially when we restart SDN-IP application while the BGPd has been |
| 283 | * running all the time. Then before SDN-IP application re-connects to BGPd, |
| 284 | * there are already lots of rib entries in BGPd. |
| 285 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 286 | private void retrieveRib() { |
| 287 | String url = "http://" + bgpdRestIp + "/wm/bgp/" + routerId; |
| 288 | String response = RestClient.get(url); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 289 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 290 | if ("".equals(response)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 291 | return; |
| 292 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 293 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 294 | try { |
| 295 | response = response.replaceAll("\"", "'"); |
| 296 | JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON(response); |
| 297 | JSONArray ribArray = jsonObj.getJSONArray("rib"); |
| 298 | String inboundRouterId = jsonObj.getString("router-id"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 299 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 300 | int size = ribArray.size(); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 301 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 302 | log.info("Retrived RIB of {} entries from BGPd", size); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 303 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 304 | for (int j = 0; j < size; j++) { |
| 305 | JSONObject ribEntry = ribArray.getJSONObject(j); |
| 306 | String prefix = ribEntry.getString("prefix"); |
| 307 | String nexthop = ribEntry.getString("nexthop"); |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 308 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 309 | // Insert each rib entry into the local rib |
| 310 | String[] substring = prefix.split("/"); |
| 311 | String prefix1 = substring[0]; |
| 312 | String mask1 = substring[1]; |
Jonathan Hart | 61ba937 | 2013-05-19 20:10:29 -0700 | [diff] [blame] | 313 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 314 | Prefix p; |
| 315 | try { |
| 316 | p = new Prefix(prefix1, Integer.parseInt(mask1)); |
| 317 | } catch (NumberFormatException e) { |
| 318 | log.warn("Wrong mask format in RIB JSON: {}", mask1); |
| 319 | continue; |
| 320 | } catch (IllegalArgumentException e1) { |
| 321 | log.warn("Wrong prefix format in RIB JSON: {}", prefix1); |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | RibEntry rib = new RibEntry(inboundRouterId, nexthop); |
| 326 | |
| 327 | try { |
| 328 | ribUpdates.put(new RibUpdate(Operation.UPDATE, p, rib)); |
| 329 | } catch (InterruptedException e) { |
| 330 | log.debug("Interrupted while pushing onto update queue"); |
| 331 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 332 | } |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 333 | } catch (JSONException e) { |
| 334 | // TODO don't parse JSON manually |
| 335 | log.error("Error parsing inital route table JSON:", e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 336 | } |
| 337 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 338 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 339 | /** |
| 340 | * Put RIB update to RIB update queue. |
| 341 | * |
| 342 | * @param update RIB update |
| 343 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 344 | @Override |
| 345 | public void newRibUpdate(RibUpdate update) { |
| 346 | try { |
| 347 | ribUpdates.put(update); |
| 348 | } catch (InterruptedException e) { |
| 349 | log.debug("Interrupted while putting on ribUpdates queue", e); |
| 350 | Thread.currentThread().interrupt(); |
| 351 | } |
| 352 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 353 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 354 | /** |
| 355 | * Process adding RIB update. |
| 356 | * Put new RIB update into InvertedRadixTree. If there was an existing nexthop |
| 357 | * for this prefix, but the next hop was different, then execute deleting old |
| 358 | * RIB update. If the next hop is the SDN domain, we do not handle it at the |
| 359 | * moment. Otherwise, execute adding RIB. |
| 360 | * |
| 361 | * @param update RIB update |
| 362 | */ |
Sho SHIMIZU | ba37219 | 2014-07-10 08:59:57 -0700 | [diff] [blame] | 363 | private void processRibAdd(RibUpdate update) { |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 364 | synchronized (this) { |
| 365 | Prefix prefix = update.getPrefix(); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 366 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 367 | log.debug("Processing prefix add {}", prefix); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 368 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 369 | RibEntry rib = bgpRoutes.put(prefix.toBinaryString(), update.getRibEntry()); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 370 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 371 | if (rib != null && !rib.equals(update.getRibEntry())) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 372 | // There was an existing nexthop for this prefix. This update |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 373 | // supersedes that, so we need to remove the old flows for this |
| 374 | // prefix from the switches |
| 375 | executeDeleteRoute(prefix, rib); |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 376 | } |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 377 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 378 | if (update.getRibEntry().getNextHop().equals( |
| 379 | InetAddresses.forString("0.0.0.0"))) { |
| 380 | // Route originated by SDN domain |
| 381 | // We don't handle these at the moment |
| 382 | log.debug("Own route {} to {}", prefix, |
| 383 | update.getRibEntry().getNextHop().getHostAddress()); |
| 384 | return; |
| 385 | } |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 386 | |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 387 | executeRibAdd(update); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 388 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 389 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 390 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 391 | /** |
| 392 | * Execute adding RIB update. |
| 393 | * Find out the egress Interface and MAC address of next hop router for this |
| 394 | * RIB update. If the MAC address can not be found in ARP cache, then this |
| 395 | * prefix will be put in prefixesWaitingOnArp queue. Otherwise, new flow |
| 396 | * intent will be created and installed. |
| 397 | * |
| 398 | * @param update RIB update |
| 399 | */ |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 400 | private void executeRibAdd(RibUpdate update) { |
Pavlin Radoslavov | 9fc535a | 2014-04-11 13:00:12 -0700 | [diff] [blame] | 401 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 402 | Prefix prefix = update.getPrefix(); |
| 403 | RibEntry rib = update.getRibEntry(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 404 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 405 | InetAddress nextHopIpAddress = rib.getNextHop(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 406 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 407 | // Find the attachment point (egress interface) of the next hop |
| 408 | Interface egressInterface = null; |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 409 | |
| 410 | if (bgpPeers.containsKey(nextHopIpAddress)) { |
| 411 | // Route to a peer |
| 412 | log.debug("Route to peer {}", nextHopIpAddress); |
| 413 | BgpPeer peer = bgpPeers.get(nextHopIpAddress); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 414 | egressInterface = interfaces.get(peer.getInterfaceName()); |
| 415 | } else { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 416 | // Route to non-peer |
| 417 | log.debug("Route to non-peer {}", nextHopIpAddress); |
| 418 | egressInterface = getOutgoingInterface(nextHopIpAddress); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 419 | if (egressInterface == null) { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 420 | log.warn("No outgoing interface found for {}", nextHopIpAddress |
| 421 | .getHostAddress()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 425 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 426 | // See if we know the MAC address of the next hop |
| 427 | MACAddress nextHopMacAddress = proxyArp.getMacAddress(nextHopIpAddress); |
| 428 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 429 | if (nextHopMacAddress == null) { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 430 | prefixesWaitingOnArp.put(nextHopIpAddress, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 431 | new RibUpdate(Operation.UPDATE, prefix, rib)); |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 432 | proxyArp.sendArpRequest(nextHopIpAddress, this, true); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 433 | return; |
| 434 | } else { |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 435 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 436 | //For all prefixes we need to add a intent for each of them |
| 437 | addRouteIntent(prefix, egressInterface, nextHopMacAddress); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 438 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 439 | } |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 440 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 441 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 442 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 443 | /** |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 444 | * Install a flow intent for a prefix. |
| 445 | * Intent will match dst IP prefix and rewrite dst MAC address at all other |
| 446 | * border switches, then forward packets according to dst MAC address. |
| 447 | * |
| 448 | * @param prefix IP prefix from BGP route |
| 449 | * @param egressInterface egress Interface connected to next hop router |
| 450 | * @param nextHopMacAddress MAC address of next hop router |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 451 | */ |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 452 | private void addRouteIntent(Prefix prefix, Interface egressInterface, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 453 | MACAddress nextHopMacAddress) { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 454 | log.debug("Adding intent for prefix {}, next hop mac {}", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 455 | prefix, nextHopMacAddress); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 456 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 457 | MultiPointToSinglePointIntent pushedIntent = pushedRouteIntents.get(prefix); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 458 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 459 | // Just for testing. |
| 460 | if (pushedIntent != null) { |
| 461 | log.error("There should not be a pushed intent: {}", pushedIntent); |
| 462 | } |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 463 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 464 | SwitchPort egressPort = egressInterface.getSwitchPort(); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 465 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 466 | Set<SwitchPort> ingressPorts = new HashSet<SwitchPort>(); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 467 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 468 | for (Interface intf : interfaces.values()) { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 469 | if (!intf.equals(egressInterface)) { |
| 470 | SwitchPort srcPort = intf.getSwitchPort(); |
| 471 | ingressPorts.add(srcPort); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 474 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 475 | // Match the destination IP prefix at the first hop |
| 476 | PacketMatchBuilder builder = new PacketMatchBuilder(); |
| 477 | builder.setDstIp(new IPv4(InetAddresses |
| 478 | .coerceToInteger(prefix.getInetAddress())), |
| 479 | (short) prefix.getPrefixLength()); |
| 480 | PacketMatch packetMatch = builder.build(); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 481 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 482 | // Rewrite the destination MAC address |
| 483 | ModifyDstMacAction modifyDstMacAction = |
| 484 | new ModifyDstMacAction(nextHopMacAddress); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 485 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 486 | MultiPointToSinglePointIntent intent = |
| 487 | new MultiPointToSinglePointIntent(intentIdGenerator.getNewId(), |
| 488 | packetMatch, modifyDstMacAction, ingressPorts, egressPort); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 489 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 490 | intentService.submit(intent); |
pingping-lin | ba5c52f | 2014-02-11 16:52:01 -0800 | [diff] [blame] | 491 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 492 | // Maintain the Intent |
| 493 | pushedRouteIntents.put(prefix, intent); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 494 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 495 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 496 | /** |
| 497 | * Remove prefix from InvertedRadixTree, if success, then try to delete the |
| 498 | * relative intent. |
| 499 | * |
| 500 | * @param update RIB update |
| 501 | */ |
Sho SHIMIZU | ba37219 | 2014-07-10 08:59:57 -0700 | [diff] [blame] | 502 | private void processRibDelete(RibUpdate update) { |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 503 | synchronized (this) { |
| 504 | Prefix prefix = update.getPrefix(); |
Ray Milkey | 5d40601 | 2014-04-08 14:44:41 -0700 | [diff] [blame] | 505 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 506 | // if (ptree.remove(prefix, update.getRibEntry())) { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 507 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 508 | // TODO check the change of logic here - remove doesn't check that |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 509 | // the rib entry was what we expected (and we can't do this |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 510 | // concurrently) |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 511 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 512 | if (bgpRoutes.remove(prefix.toBinaryString())) { |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 513 | /* |
Jonathan Hart | 5e54f2e | 2014-04-17 13:43:40 -0700 | [diff] [blame] | 514 | * Only delete flows if an entry was actually removed from the tree. |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 515 | * If no entry was removed, the <prefix, nexthop> wasn't there so |
| 516 | * it's probably already been removed and we don't need to do anything |
| 517 | */ |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 518 | executeDeleteRoute(prefix, update.getRibEntry()); |
| 519 | |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 520 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 521 | } |
| 522 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 523 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 524 | /** |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 525 | * Delete prefix intent installed. |
| 526 | * |
| 527 | * @param prefix IP prefix withdrew in a rib update announcement |
| 528 | * @param ribEntry next hop information |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 529 | */ |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 530 | private void executeDeleteRoute(Prefix prefix, RibEntry ribEntry) { |
| 531 | log.debug("Deleting {} to {}", prefix, ribEntry.getNextHop()); |
Pavlin Radoslavov | 9fc535a | 2014-04-11 13:00:12 -0700 | [diff] [blame] | 532 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 533 | MultiPointToSinglePointIntent intent = pushedRouteIntents.remove(prefix); |
Pavlin Radoslavov | 9fc535a | 2014-04-11 13:00:12 -0700 | [diff] [blame] | 534 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 535 | if (intent == null) { |
| 536 | log.debug("There is no intent in pushedRouteIntents to delete for " + |
| 537 | "prefix: {}", prefix); |
| 538 | } else { |
| 539 | intentService.withdraw(intent); |
| 540 | log.debug("Deleted the pushedRouteIntent for prefix: {}", prefix); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 541 | } |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 544 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 545 | * Setup the Paths to the BGP Daemon. Run a loop for all of the bgpPeers |
| 546 | * Push flow from BGPd to the peer Push flow from peer to BGPd Parameters to |
| 547 | * pass to the intent are as follows: String id, long srcSwitch, long |
| 548 | * srcPort, long srcMac, int srcIP, long dstSwitch, long dstPort, long |
| 549 | * dstMac, int dstIP |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 550 | */ |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 551 | private void setupBgpPaths() { |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 552 | IntentOperationList operations = new IntentOperationList(); |
| 553 | for (BgpPeer bgpPeer : bgpPeers.values()) { |
| 554 | Interface peerInterface = interfaces.get(bgpPeer.getInterfaceName()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 555 | // Inet4Address. |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 556 | int srcIP = InetAddresses.coerceToInteger(peerInterface.getIpAddress()); |
| 557 | int dstIP = InetAddresses.coerceToInteger(bgpPeer.getIpAddress()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 558 | String fwdIntentId = caller + ":" |
| 559 | + controllerRegistryService.getNextUniqueId(); |
| 560 | String bwdIntentId = caller + ":" |
| 561 | + controllerRegistryService.getNextUniqueId(); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 562 | SwitchPort srcPort = |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 563 | new SwitchPort(bgpdAttachmentPoint.getDpid(), |
| 564 | bgpdAttachmentPoint.getPortNumber()); |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 565 | // TODO: replace the code below with peerInterface.getSwitchPort() |
| 566 | // when using poingToPointIntent |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 567 | SwitchPort dstPort = |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 568 | new SwitchPort(new Dpid(peerInterface.getDpid()), |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 569 | new PortNumber(peerInterface.getSwitchPort().getPortNumber())); |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 570 | |
| 571 | // TODO: add TCP port number 179 into intent for BGP |
| 572 | |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 573 | ShortestPathIntent fwdIntent = new ShortestPathIntent(fwdIntentId, |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 574 | srcPort.getDpid().value(), srcPort.getPortNumber().value(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 575 | ShortestPathIntent.EMPTYMACADDRESS, srcIP, |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 576 | dstPort.getDpid().value(), dstPort.getPortNumber().value(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 577 | ShortestPathIntent.EMPTYMACADDRESS, dstIP); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 578 | ShortestPathIntent bwdIntent = new ShortestPathIntent(bwdIntentId, |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 579 | dstPort.getDpid().value(), dstPort.getPortNumber().value(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 580 | ShortestPathIntent.EMPTYMACADDRESS, dstIP, |
Pavlin Radoslavov | 3d322f4 | 2014-08-18 14:58:55 -0700 | [diff] [blame] | 581 | srcPort.getDpid().value(), srcPort.getPortNumber().value(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 582 | ShortestPathIntent.EMPTYMACADDRESS, srcIP); |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 583 | IntentOperation.Operator operator = IntentOperation.Operator.ADD; |
| 584 | operations.add(operator, fwdIntent); |
| 585 | operations.add(operator, bwdIntent); |
| 586 | } |
| 587 | pathRuntime.executeIntentOperations(operations); |
| 588 | } |
| 589 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 590 | /** |
| 591 | * This method handles the prefixes which are waiting for ARP replies for |
| 592 | * MAC addresses of next hops. |
| 593 | * |
| 594 | * @param ipAddress next hop router IP address, for which we sent ARP request out |
| 595 | * @param macAddress MAC address which is relative to the ipAddress |
| 596 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 597 | @Override |
| 598 | public void arpResponse(InetAddress ipAddress, MACAddress macAddress) { |
| 599 | log.debug("Received ARP response: {} => {}", |
| 600 | ipAddress.getHostAddress(), macAddress); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 601 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 602 | /* |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 603 | * We synchronize on this to prevent changes to the InvertedRadixTree |
| 604 | * while we're pushing intent. If the InvertedRadixTree changes, the |
| 605 | * InvertedRadixTree and intent could get out of sync. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 606 | */ |
| 607 | synchronized (this) { |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 608 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 609 | Set<RibUpdate> prefixesToPush = prefixesWaitingOnArp.removeAll(ipAddress); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 610 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 611 | for (RibUpdate update : prefixesToPush) { |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 612 | // These will always be adds |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 613 | |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 614 | RibEntry rib = bgpRoutes.getValueForExactKey( |
| 615 | update.getPrefix().toBinaryString()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 616 | if (rib != null && rib.equals(update.getRibEntry())) { |
| 617 | log.debug("Pushing prefix {} next hop {}", update.getPrefix(), |
| 618 | rib.getNextHop().getHostAddress()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 619 | // We only push prefix flows if the prefix is still in the |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 620 | // InvertedRadixTree and the next hop is the same as our update. |
| 621 | // The prefix could have been removed while we were waiting |
| 622 | // for the ARP, or the next hop could have changed. |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 623 | executeRibAdd(update); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 624 | } else { |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 625 | log.debug("Received ARP response, but {},{} is no longer in " + |
| 626 | "InvertedRadixTree", update.getPrefix(), |
| 627 | update.getRibEntry()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | } |
| 631 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 632 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 633 | |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 634 | /*private void setupArpFlows() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 635 | OFMatch match = new OFMatch(); |
| 636 | match.setDataLayerType(Ethernet.TYPE_ARP); |
| 637 | match.setWildcards(match.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 638 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 639 | OFFlowMod fm = new OFFlowMod(); |
| 640 | fm.setMatch(match); |
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 | OFActionOutput action = new OFActionOutput(); |
| 643 | action.setPort(OFPort.OFPP_CONTROLLER.getValue()); |
| 644 | action.setMaxLength((short) 0xffff); |
Sho SHIMIZU | 84a72de | 2014-07-09 07:56:40 -0700 | [diff] [blame] | 645 | List<OFAction> actions = new ArrayList<>(1); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 646 | actions.add(action); |
| 647 | fm.setActions(actions); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 648 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 649 | fm.setIdleTimeout((short) 0) |
| 650 | .setHardTimeout((short) 0) |
| 651 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 652 | .setCookie(0) |
| 653 | .setCommand(OFFlowMod.OFPFC_ADD) |
| 654 | .setPriority(ARP_PRIORITY) |
| 655 | .setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 656 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 657 | for (String strdpid : switches) { |
| 658 | flowCache.write(HexString.toLong(strdpid), fm); |
| 659 | } |
| 660 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 661 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 662 | private void setupDefaultDropFlows() { |
| 663 | OFFlowMod fm = new OFFlowMod(); |
| 664 | fm.setMatch(new OFMatch()); |
Jonathan Hart | 938a015 | 2014-04-07 18:27:31 -0700 | [diff] [blame] | 665 | fm.setActions(new ArrayList<OFAction>()); // No action means drop |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 666 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 667 | fm.setIdleTimeout((short) 0) |
| 668 | .setHardTimeout((short) 0) |
| 669 | .setBufferId(OFPacketOut.BUFFER_ID_NONE) |
| 670 | .setCookie(0) |
| 671 | .setCommand(OFFlowMod.OFPFC_ADD) |
| 672 | .setPriority((short) 0) |
| 673 | .setLengthU(OFFlowMod.MINIMUM_LENGTH); |
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 | OFFlowMod fmLLDP; |
| 676 | OFFlowMod fmBDDP; |
| 677 | try { |
| 678 | fmLLDP = fm.clone(); |
| 679 | fmBDDP = fm.clone(); |
| 680 | } catch (CloneNotSupportedException e1) { |
| 681 | log.error("Error cloning flow mod", e1); |
| 682 | return; |
| 683 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 684 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 685 | OFMatch matchLLDP = new OFMatch(); |
| 686 | matchLLDP.setDataLayerType((short) 0x88cc); |
| 687 | matchLLDP.setWildcards(matchLLDP.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 688 | fmLLDP.setMatch(matchLLDP); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 689 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 690 | OFMatch matchBDDP = new OFMatch(); |
| 691 | matchBDDP.setDataLayerType((short) 0x8942); |
| 692 | matchBDDP.setWildcards(matchBDDP.getWildcards() & ~OFMatch.OFPFW_DL_TYPE); |
| 693 | fmBDDP.setMatch(matchBDDP); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 694 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 695 | OFActionOutput action = new OFActionOutput(); |
| 696 | action.setPort(OFPort.OFPP_CONTROLLER.getValue()); |
| 697 | action.setMaxLength((short) 0xffff); |
Sho SHIMIZU | 84a72de | 2014-07-09 07:56:40 -0700 | [diff] [blame] | 698 | List<OFAction> actions = new ArrayList<>(1); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 699 | actions.add(action); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 700 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 701 | fmLLDP.setActions(actions); |
| 702 | fmBDDP.setActions(actions); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 703 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 704 | fmLLDP.setPriority(ARP_PRIORITY); |
| 705 | fmLLDP.setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
| 706 | fmBDDP.setPriority(ARP_PRIORITY); |
| 707 | fmBDDP.setLengthU(OFFlowMod.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 708 | |
Sho SHIMIZU | 84a72de | 2014-07-09 07:56:40 -0700 | [diff] [blame] | 709 | List<OFFlowMod> flowModList = new ArrayList<>(3); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 710 | flowModList.add(fm); |
| 711 | flowModList.add(fmLLDP); |
| 712 | flowModList.add(fmBDDP); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 713 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 714 | for (String strdpid : switches) { |
| 715 | flowCache.write(HexString.toLong(strdpid), flowModList); |
| 716 | } |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 717 | }*/ |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 718 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 719 | /** |
| 720 | * The SDN-IP application is started from this method. |
| 721 | */ |
| 722 | @Override |
| 723 | public void beginRouting() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 724 | log.debug("Topology is now ready, beginning routing function"); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 725 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 726 | // TODO |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 727 | /*setupArpFlows(); |
| 728 | setupDefaultDropFlows();*/ |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 729 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 730 | setupBgpPaths(); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 731 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 732 | // Suppress link discovery on external-facing router ports |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 733 | for (Interface intf : interfaces.values()) { |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 734 | linkDiscoveryService.disableDiscoveryOnPort(intf.getDpid(), intf.getPort()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 735 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 736 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 737 | bgpUpdatesExecutor.execute(new Runnable() { |
| 738 | @Override |
| 739 | public void run() { |
| 740 | doUpdatesThread(); |
| 741 | } |
| 742 | }); |
| 743 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 744 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 745 | /** |
| 746 | * Thread for handling RIB updates. |
| 747 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 748 | private void doUpdatesThread() { |
| 749 | boolean interrupted = false; |
| 750 | try { |
| 751 | while (true) { |
| 752 | try { |
| 753 | RibUpdate update = ribUpdates.take(); |
| 754 | switch (update.getOperation()) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 755 | case UPDATE: |
| 756 | if (validateUpdate(update)) { |
| 757 | processRibAdd(update); |
| 758 | } else { |
| 759 | log.debug("Rib UPDATE out of order: {} via {}", |
| 760 | update.getPrefix(), update.getRibEntry().getNextHop()); |
| 761 | } |
| 762 | break; |
| 763 | case DELETE: |
| 764 | if (validateUpdate(update)) { |
| 765 | processRibDelete(update); |
| 766 | } else { |
| 767 | log.debug("Rib DELETE out of order: {} via {}", |
| 768 | update.getPrefix(), update.getRibEntry().getNextHop()); |
| 769 | } |
| 770 | break; |
| 771 | default: |
| 772 | log.error("Unknown operation {}", update.getOperation()); |
| 773 | break; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 774 | } |
| 775 | } catch (InterruptedException e) { |
| 776 | log.debug("Interrupted while taking from updates queue", e); |
| 777 | interrupted = true; |
| 778 | } catch (Exception e) { |
| 779 | log.debug("exception", e); |
| 780 | } |
| 781 | } |
| 782 | } finally { |
| 783 | if (interrupted) { |
| 784 | Thread.currentThread().interrupt(); |
| 785 | } |
| 786 | } |
| 787 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 788 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 789 | /** |
| 790 | * Judge whether a RIB update is in correct order. |
| 791 | * |
| 792 | * @param update RIB update |
| 793 | * @return boolean whether the RIB update is in in correct order |
| 794 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 795 | private boolean validateUpdate(RibUpdate update) { |
| 796 | RibEntry newEntry = update.getRibEntry(); |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 797 | RibEntry oldEntry = bgpRoutes.getValueForExactKey( |
| 798 | update.getPrefix().toBinaryString()); |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 799 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 800 | // If there is no existing entry we must assume this is the most recent |
| 801 | // update. However this might not always be the case as we might have a |
| 802 | // POST then DELETE reordering. |
| 803 | // if (oldEntry == null || |
| 804 | // !newEntry.getNextHop().equals(oldEntry.getNextHop())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 805 | if (oldEntry == null) { |
| 806 | return true; |
| 807 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 808 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 809 | // This handles the case where routes are gathered in the initial |
| 810 | // request because they don't have sequence number info |
| 811 | if (newEntry.getSysUpTime() == -1 && newEntry.getSequenceNum() == -1) { |
| 812 | return true; |
| 813 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 814 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 815 | if (newEntry.getSysUpTime() > oldEntry.getSysUpTime()) { |
| 816 | return true; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 817 | } |
Ray Milkey | 4985f21 | 2014-04-10 16:57:05 -0700 | [diff] [blame] | 818 | |
| 819 | return newEntry.getSysUpTime() == oldEntry.getSysUpTime() && |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 820 | newEntry.getSequenceNum() > oldEntry.getSequenceNum(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 821 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 822 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 823 | /** |
| 824 | * To find the Interface which has longest matchable IP prefix (sub-network |
| 825 | * prefix) to next hop IP address. |
| 826 | * |
| 827 | * @param address the IP address of next hop router |
| 828 | * @return Interface the Interface which has longest matchable IP prefix |
| 829 | */ |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 830 | private Interface longestInterfacePrefixMatch(InetAddress address) { |
| 831 | Prefix prefixToSearchFor = new Prefix(address.getAddress(), |
| 832 | Prefix.MAX_PREFIX_LENGTH); |
| 833 | Iterator<Interface> it = |
| 834 | interfaceRoutes.getValuesForKeysPrefixing( |
| 835 | prefixToSearchFor.toBinaryString()).iterator(); |
| 836 | Interface intf = null; |
| 837 | // Find the last prefix, which will be the longest prefix |
| 838 | while (it.hasNext()) { |
| 839 | intf = it.next(); |
| 840 | } |
| 841 | |
| 842 | return intf; |
| 843 | } |
| 844 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 845 | /* |
| 846 | * IConfigInfoService methods |
| 847 | */ |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 848 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 849 | @Override |
| 850 | public boolean isInterfaceAddress(InetAddress address) { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 851 | Interface intf = longestInterfacePrefixMatch(address); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 852 | return (intf != null && intf.getIpAddress().equals(address)); |
| 853 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 854 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 855 | @Override |
| 856 | public boolean inConnectedNetwork(InetAddress address) { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 857 | Interface intf = longestInterfacePrefixMatch(address); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 858 | return (intf != null && !intf.getIpAddress().equals(address)); |
| 859 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 860 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 861 | @Override |
| 862 | public boolean fromExternalNetwork(long inDpid, short inPort) { |
| 863 | for (Interface intf : interfaces.values()) { |
| 864 | if (intf.getDpid() == inDpid && intf.getPort() == inPort) { |
| 865 | return true; |
| 866 | } |
| 867 | } |
| 868 | return false; |
| 869 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 870 | |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 871 | /** |
| 872 | * To find the relative egress Interface for a next hop IP address. |
| 873 | * |
| 874 | * @param dstIpAddress the IP address of next hop router |
| 875 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 876 | @Override |
| 877 | public Interface getOutgoingInterface(InetAddress dstIpAddress) { |
Jonathan Hart | f6978ce | 2014-06-23 11:20:04 -0700 | [diff] [blame] | 878 | return longestInterfacePrefixMatch(dstIpAddress); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 879 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 880 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 881 | @Override |
| 882 | public boolean hasLayer3Configuration() { |
| 883 | return !interfaces.isEmpty(); |
| 884 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 885 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 886 | @Override |
| 887 | public MACAddress getRouterMacAddress() { |
| 888 | return bgpdMacAddress; |
| 889 | } |
Yuta HIGUCHI | e7eac18 | 2014-03-19 19:18:30 -0700 | [diff] [blame] | 890 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 891 | @Override |
| 892 | public short getVlan() { |
| 893 | return vlan; |
| 894 | } |
pingping-lin | 1ada7ce | 2014-08-14 13:45:22 -0700 | [diff] [blame] | 895 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 896 | } |