blob: 8a18fd1262a119bc599bd0789794c843d06a4a07 [file] [log] [blame]
Sangho Shin2f263692014-09-15 14:09:41 -07001package net.onrc.onos.apps.segmentrouting;
2
Sangho Shin9c0f4c32014-09-26 16:02:38 -07003import java.io.IOException;
Sangho Shin1aa93542014-09-22 09:49:44 -07004import java.net.Inet4Address;
5import java.net.InetAddress;
6import java.net.UnknownHostException;
Sangho Shin2f263692014-09-15 14:09:41 -07007import java.util.ArrayList;
8import java.util.Collection;
Srikanth Vavilapalli363f1dc2014-09-22 14:30:23 -07009import java.util.HashMap;
Sangho Shin5be3e532014-10-03 17:20:58 -070010import java.util.HashSet;
Sangho Shin2f263692014-09-15 14:09:41 -070011import java.util.Iterator;
12import java.util.List;
13import java.util.Map;
Sangho Shin5be3e532014-10-03 17:20:58 -070014import java.util.Set;
Sangho Shin61535402014-10-01 11:37:14 -070015import java.util.concurrent.ConcurrentLinkedQueue;
Sangho Shin11d4e0f2014-09-30 12:00:33 -070016import java.util.concurrent.ExecutionException;
Sangho Shin43cee112014-09-25 16:43:34 -070017import java.util.concurrent.ScheduledExecutorService;
18import java.util.concurrent.TimeUnit;
Sangho Shin11d4e0f2014-09-30 12:00:33 -070019import java.util.concurrent.TimeoutException;
Sangho Shin2f263692014-09-15 14:09:41 -070020
21import net.floodlightcontroller.core.IFloodlightProviderService;
Sangho Shin9c0f4c32014-09-26 16:02:38 -070022import net.floodlightcontroller.core.IOF13Switch;
Sangho Shin0df01982014-09-25 17:11:18 -070023import net.floodlightcontroller.core.IOF13Switch.NeighborSet;
Sangho Shin11d4e0f2014-09-30 12:00:33 -070024import net.floodlightcontroller.core.internal.OFBarrierReplyFuture;
Sangho Shin2f263692014-09-15 14:09:41 -070025import net.floodlightcontroller.core.module.FloodlightModuleContext;
26import net.floodlightcontroller.core.module.FloodlightModuleException;
27import net.floodlightcontroller.core.module.IFloodlightModule;
28import net.floodlightcontroller.core.module.IFloodlightService;
Sangho Shin43cee112014-09-25 16:43:34 -070029import net.floodlightcontroller.core.util.SingletonTask;
Sangho Shin7330c032014-10-20 10:34:51 -070030import net.floodlightcontroller.restserver.IRestApiService;
Sangho Shin43cee112014-09-25 16:43:34 -070031import net.floodlightcontroller.threadpool.IThreadPoolService;
Sangho Shin15273b62014-10-16 22:22:05 -070032import net.floodlightcontroller.util.MACAddress;
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -070033import net.onrc.onos.api.packet.IPacketListener;
Sangho Shin2f263692014-09-15 14:09:41 -070034import net.onrc.onos.api.packet.IPacketService;
Fahad Naeem Khan4444b952014-10-18 22:30:50 -070035import net.onrc.onos.apps.segmentrouting.web.SegmentRoutingWebRoutable;
Sangho Shin2f263692014-09-15 14:09:41 -070036import net.onrc.onos.core.flowprogrammer.IFlowPusherService;
Sangho Shinfbc572c2014-10-02 16:37:05 -070037import net.onrc.onos.core.intent.Path;
Sangho Shin2f263692014-09-15 14:09:41 -070038import net.onrc.onos.core.main.config.IConfigInfoService;
Sangho Shin43cee112014-09-25 16:43:34 -070039import net.onrc.onos.core.matchaction.MatchAction;
40import net.onrc.onos.core.matchaction.MatchActionId;
41import net.onrc.onos.core.matchaction.MatchActionOperationEntry;
Sangho Shin5be3e532014-10-03 17:20:58 -070042import net.onrc.onos.core.matchaction.MatchActionOperations.Operator;
Sangho Shin43cee112014-09-25 16:43:34 -070043import net.onrc.onos.core.matchaction.action.Action;
44import net.onrc.onos.core.matchaction.action.CopyTtlInAction;
45import net.onrc.onos.core.matchaction.action.CopyTtlOutAction;
46import net.onrc.onos.core.matchaction.action.DecMplsTtlAction;
47import net.onrc.onos.core.matchaction.action.DecNwTtlAction;
48import net.onrc.onos.core.matchaction.action.GroupAction;
49import net.onrc.onos.core.matchaction.action.PopMplsAction;
50import net.onrc.onos.core.matchaction.action.PushMplsAction;
51import net.onrc.onos.core.matchaction.action.SetMplsIdAction;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -070052import net.onrc.onos.core.matchaction.match.Ipv4Match;
Sangho Shin43cee112014-09-25 16:43:34 -070053import net.onrc.onos.core.matchaction.match.Match;
54import net.onrc.onos.core.matchaction.match.MplsMatch;
Sangho Shin15273b62014-10-16 22:22:05 -070055import net.onrc.onos.core.matchaction.match.PacketMatch;
56import net.onrc.onos.core.matchaction.match.PacketMatchBuilder;
Sangho Shin2f263692014-09-15 14:09:41 -070057import net.onrc.onos.core.packet.ARP;
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -070058import net.onrc.onos.core.packet.Ethernet;
59import net.onrc.onos.core.packet.IPv4;
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -070060import net.onrc.onos.core.topology.ITopologyListener;
Sangho Shin1aa93542014-09-22 09:49:44 -070061import net.onrc.onos.core.topology.ITopologyService;
Sangho Shinbce900e2014-10-07 17:13:23 -070062import net.onrc.onos.core.topology.Link;
Sangho Shinc8d2f592014-09-30 16:53:57 -070063import net.onrc.onos.core.topology.LinkData;
Sangho Shinbce900e2014-10-07 17:13:23 -070064import net.onrc.onos.core.topology.MastershipData;
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -070065import net.onrc.onos.core.topology.MutableTopology;
Sangho Shineb083032014-09-22 16:11:34 -070066import net.onrc.onos.core.topology.Port;
Sangho Shinc8d2f592014-09-30 16:53:57 -070067import net.onrc.onos.core.topology.PortData;
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -070068import net.onrc.onos.core.topology.Switch;
Sangho Shin5be3e532014-10-03 17:20:58 -070069import net.onrc.onos.core.topology.SwitchData;
Sangho Shin1aa93542014-09-22 09:49:44 -070070import net.onrc.onos.core.topology.TopologyEvents;
Srikanth Vavilapalli363f1dc2014-09-22 14:30:23 -070071import net.onrc.onos.core.util.Dpid;
Sangho Shin43cee112014-09-25 16:43:34 -070072import net.onrc.onos.core.util.IPv4Net;
73import net.onrc.onos.core.util.SwitchPort;
Sangho Shin2f263692014-09-15 14:09:41 -070074
Sangho Shin43cee112014-09-25 16:43:34 -070075import org.json.JSONArray;
76import org.json.JSONException;
Saurav Dasa962a692014-10-17 14:52:38 -070077import org.projectfloodlight.openflow.protocol.OFBarrierReply;
Saurav Dasbc594a42014-09-25 20:13:50 -070078import org.projectfloodlight.openflow.types.EthType;
Sangho Shin2f263692014-09-15 14:09:41 -070079import org.projectfloodlight.openflow.types.IPv4Address;
80import org.slf4j.Logger;
81import org.slf4j.LoggerFactory;
82
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -070083public class SegmentRoutingManager implements IFloodlightModule,
Srikanth Vavilapalli8c49db72014-10-20 13:35:08 -070084 ITopologyListener, IPacketListener, ISegmentRoutingService {
Sangho Shin2f263692014-09-15 14:09:41 -070085
86 private static final Logger log = LoggerFactory
87 .getLogger(SegmentRoutingManager.class);
Sangho Shin23f898d2014-10-13 16:54:00 -070088
Fahad Naeem Khan5b558f22014-10-16 10:35:20 -070089 private ITopologyService topologyService;
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -070090 private IPacketService packetService;
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -070091 private MutableTopology mutableTopology;
Sangho Shin61535402014-10-01 11:37:14 -070092 private ConcurrentLinkedQueue<IPv4> ipPacketQueue;
Fahad Naeem Khan4444b952014-10-18 22:30:50 -070093 private IRestApiService restApi;
Sangho Shin2f263692014-09-15 14:09:41 -070094 private List<ArpEntry> arpEntries;
Sangho Shineb083032014-09-22 16:11:34 -070095 private ArpHandler arpHandler;
96 private GenericIpHandler ipHandler;
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -070097 private IcmpHandler icmpHandler;
Sangho Shin43cee112014-09-25 16:43:34 -070098 private IThreadPoolService threadPool;
99 private SingletonTask discoveryTask;
Sangho Shin23f898d2014-10-13 16:54:00 -0700100 private SingletonTask linkAddTask;
Sangho Shin15273b62014-10-16 22:22:05 -0700101 private SingletonTask testTask;
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700102 private IFloodlightProviderService floodlightProvider;
Sangho Shin2f263692014-09-15 14:09:41 -0700103
Sangho Shinfbc572c2014-10-02 16:37:05 -0700104 private HashMap<Switch, ECMPShortestPathGraph> graphs;
Sangho Shin23f898d2014-10-13 16:54:00 -0700105 private HashMap<String, LinkData> linksDown;
106 private HashMap<String, LinkData> linksToAdd;
Sangho Shin5be3e532014-10-03 17:20:58 -0700107 private ConcurrentLinkedQueue<TopologyEvents> topologyEventQueue;
Sangho Shine020cc32014-10-20 13:28:02 -0700108 private HashMap<String, HashMap<String, TunnelRouteInfo>> stitchInfo;
109 private HashMap<String, HashMap<String, Integer>> tunnelGroupMap;
110 private HashMap<String, PolicyInfo> policyTable;
111 private HashMap<String, List<Dpid>> tunnelTable;
Sangho Shin5b8f5452014-10-20 11:46:01 -0700112
113 private int testMode = 0;
114
Sangho Shinbce900e2014-10-07 17:13:23 -0700115
116 private int numOfEvents = 0;
117 private int numOfEventProcess = 0;
118 private int numOfPopulation = 0;
Sangho Shin99918bd2014-10-08 15:52:35 -0700119 private long matchActionId = 0L;
Sangho Shin23f898d2014-10-13 16:54:00 -0700120 private final int DELAY_TO_ADD_LINK = 10;
Sangho Shin15273b62014-10-16 22:22:05 -0700121 private final int MAX_NUM_LABELS = 3;
Sangho Shinfbc572c2014-10-02 16:37:05 -0700122
Sangho Shin5b8f5452014-10-20 11:46:01 -0700123
124 private final int POLICY_ADD1 = 1;
125 private final int POLICY_ADD2 = 2;
126 private final int POLICY_REMOVE1 = 3;
127 private final int POLICY_REMOVE2 = 4;
128
129
Sangho Shin7330c032014-10-20 10:34:51 -0700130 // ************************************
131 // IFloodlightModule implementation
132 // ************************************
133
Sangho Shin2f263692014-09-15 14:09:41 -0700134 @Override
135 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Srikanth Vavilapalli8c49db72014-10-20 13:35:08 -0700136 Collection<Class<? extends IFloodlightService>> l = new ArrayList<>();
137 l.add(ISegmentRoutingService.class);
138 return l;
Sangho Shin2f263692014-09-15 14:09:41 -0700139 }
140
141 @Override
142 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
Srikanth Vavilapalli8c49db72014-10-20 13:35:08 -0700143 Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>();
144 m.put(ISegmentRoutingService.class, this);
145 return m;
Sangho Shin2f263692014-09-15 14:09:41 -0700146 }
147
148 @Override
149 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
150 Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();
151
152 l.add(IFloodlightProviderService.class);
153 l.add(IConfigInfoService.class);
154 l.add(ITopologyService.class);
155 l.add(IPacketService.class);
156 l.add(IFlowPusherService.class);
157 l.add(ITopologyService.class);
Fahad Naeem Khan4444b952014-10-18 22:30:50 -0700158 l.add(IRestApiService.class);
Sangho Shin2f263692014-09-15 14:09:41 -0700159
160 return l;
161
162 }
163
164 @Override
165 public void init(FloodlightModuleContext context) throws FloodlightModuleException {
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700166 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Sangho Shineb083032014-09-22 16:11:34 -0700167 arpHandler = new ArpHandler(context, this);
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700168 icmpHandler = new IcmpHandler(context, this);
Sangho Shineb083032014-09-22 16:11:34 -0700169 ipHandler = new GenericIpHandler(context, this);
Sangho Shin2f263692014-09-15 14:09:41 -0700170 arpEntries = new ArrayList<ArpEntry>();
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -0700171 topologyService = context.getServiceImpl(ITopologyService.class);
Sangho Shin43cee112014-09-25 16:43:34 -0700172 threadPool = context.getServiceImpl(IThreadPoolService.class);
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -0700173 mutableTopology = topologyService.getTopology();
Sangho Shin61535402014-10-01 11:37:14 -0700174 ipPacketQueue = new ConcurrentLinkedQueue<IPv4>();
Sangho Shinfbc572c2014-10-02 16:37:05 -0700175 graphs = new HashMap<Switch, ECMPShortestPathGraph>();
Sangho Shin23f898d2014-10-13 16:54:00 -0700176 linksDown = new HashMap<String, LinkData>();
177 linksToAdd = new HashMap<String, LinkData>();
Sangho Shin5be3e532014-10-03 17:20:58 -0700178 topologyEventQueue = new ConcurrentLinkedQueue<TopologyEvents>();
Sangho Shine020cc32014-10-20 13:28:02 -0700179 stitchInfo = new HashMap<String, HashMap<String, TunnelRouteInfo>>();
Sangho Shin15273b62014-10-16 22:22:05 -0700180 packetService = context.getServiceImpl(IPacketService.class);
Sangho Shine020cc32014-10-20 13:28:02 -0700181 tunnelGroupMap = new HashMap<String, HashMap<String, Integer>>();
Fahad Naeem Khan4444b952014-10-18 22:30:50 -0700182 restApi = context.getServiceImpl(IRestApiService.class);
Sangho Shine020cc32014-10-20 13:28:02 -0700183 policyTable = new HashMap<String, PolicyInfo>();
184 tunnelTable = new HashMap<String, List<Dpid>>();
Sangho Shin2f263692014-09-15 14:09:41 -0700185
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700186 packetService.registerPacketListener(this);
Sangho Shin15273b62014-10-16 22:22:05 -0700187 topologyService.addListener(this, false);
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700188
Sangho Shin99918bd2014-10-08 15:52:35 -0700189
Sangho Shin2f263692014-09-15 14:09:41 -0700190 }
191
192 @Override
193 public void startUp(FloodlightModuleContext context) throws FloodlightModuleException {
Sangho Shinc8d2f592014-09-30 16:53:57 -0700194 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
Fahad Naeem Khan4444b952014-10-18 22:30:50 -0700195 restApi.addRestletRoutable(new SegmentRoutingWebRoutable());
Sangho Shin2f263692014-09-15 14:09:41 -0700196
Sangho Shinc8d2f592014-09-30 16:53:57 -0700197 discoveryTask = new SingletonTask(ses, new Runnable() {
198 @Override
199 public void run() {
Sangho Shin5be3e532014-10-03 17:20:58 -0700200 handleTopologyChangeEvents();
Sangho Shinc8d2f592014-09-30 16:53:57 -0700201 }
202 });
Sangho Shin23f898d2014-10-13 16:54:00 -0700203
204 linkAddTask = new SingletonTask(ses, new Runnable() {
205 @Override
206 public void run() {
207 delayedAddLink();
208 }
209 });
210
Sangho Shin15273b62014-10-16 22:22:05 -0700211 testTask = new SingletonTask(ses, new Runnable() {
212 @Override
213 public void run() {
214 runTest();
215 }
216 });
217
Sangho Shin5b8f5452014-10-20 11:46:01 -0700218 testMode = POLICY_ADD1;
219 testTask.reschedule(20, TimeUnit.SECONDS);
Sangho Shin2f263692014-09-15 14:09:41 -0700220 }
221
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700222 @Override
223 public void receive(Switch sw, Port inPort, Ethernet payload) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700224 if (payload.getEtherType() == Ethernet.TYPE_ARP)
225 arpHandler.processPacketIn(sw, inPort, payload);
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700226 if (payload.getEtherType() == Ethernet.TYPE_IPV4) {
Sangho Shin7330c032014-10-20 10:34:51 -0700227 addPacketToPacketBuffer((IPv4) payload.getPayload());
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700228 if (((IPv4) payload.getPayload()).getProtocol() == IPv4.PROTOCOL_ICMP)
229 icmpHandler.processPacketIn(sw, inPort, payload);
230 else
231 ipHandler.processPacketIn(sw, inPort, payload);
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700232 }
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700233 else {
234 log.debug("{}", payload.toString());
235 }
Srikanth Vavilapallib1fce732014-09-24 14:09:50 -0700236 }
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700237
Sangho Shin2f263692014-09-15 14:09:41 -0700238
Sangho Shin7330c032014-10-20 10:34:51 -0700239 // ************************************
240 // Topology event handlers
241 // ************************************
Sangho Shineb083032014-09-22 16:11:34 -0700242
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -0700243 /**
244 * Topology events that have been generated.
Sangho Shinfbc572c2014-10-02 16:37:05 -0700245 *
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -0700246 * @param topologyEvents the generated Topology Events
247 * @see TopologyEvents
248 */
249 public void topologyEvents(TopologyEvents topologyEvents)
250 {
Sangho Shin5be3e532014-10-03 17:20:58 -0700251 topologyEventQueue.add(topologyEvents);
Sangho Shinbce900e2014-10-07 17:13:23 -0700252 discoveryTask.reschedule(100, TimeUnit.MILLISECONDS);
Sangho Shin5be3e532014-10-03 17:20:58 -0700253 }
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700254
Sangho Shin23f898d2014-10-13 16:54:00 -0700255 /**
256 * Process the multiple topology events with some delay (100MS at most for now)
257 *
258 */
Sangho Shin5be3e532014-10-03 17:20:58 -0700259 private void handleTopologyChangeEvents() {
Sangho Shinbce900e2014-10-07 17:13:23 -0700260 numOfEventProcess ++;
261
Sangho Shin51625342014-10-17 09:30:48 -0700262 Collection<LinkData> linkEntriesAddedAll = new ArrayList<LinkData>();
263 Collection<PortData> portEntriesAddedAll = new ArrayList<PortData>();
264 Collection<PortData> portEntriesRemovedAll = new ArrayList<PortData>();
265 Collection<LinkData> linkEntriesRemovedAll = new ArrayList<LinkData>();
266 Collection<SwitchData> switchAddedAll = new ArrayList<SwitchData>();
267 Collection<SwitchData> switchRemovedAll = new ArrayList<SwitchData>();
268 Collection<MastershipData> mastershipRemovedAll = new ArrayList<MastershipData>();
Sangho Shinbce900e2014-10-07 17:13:23 -0700269
Sangho Shin5be3e532014-10-03 17:20:58 -0700270 while (!topologyEventQueue.isEmpty()) {
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700271 // We should handle the events in the order of when they happen
272 // TODO: We need to simulate the final results of multiple events
273 // and shoot only the final state.
274 // Ex: link s1-s2 down, link s1-s2 up --> Do nothing
275 // Ex: ink s1-s2 up, s1-p1,p2 down --> link s1-s2 down
Sangho Shin51625342014-10-17 09:30:48 -0700276
Sangho Shin5be3e532014-10-03 17:20:58 -0700277 TopologyEvents topologyEvents = topologyEventQueue.poll();
Sangho Shin51625342014-10-17 09:30:48 -0700278
279 Collection<LinkData> linkEntriesAdded = topologyEvents.getAddedLinkDataEntries();
280 Collection<PortData> portEntriesAdded = topologyEvents.getAddedPortDataEntries();
281 Collection<PortData> portEntriesRemoved = topologyEvents.getRemovedPortDataEntries();
282 Collection<LinkData> linkEntriesRemoved = topologyEvents.getRemovedLinkDataEntries();
283 Collection<SwitchData> switchAdded = topologyEvents.getAddedSwitchDataEntries();
284 Collection<SwitchData> switchRemoved = topologyEvents.getRemovedSwitchDataEntries();
285 Collection<MastershipData> mastershipRemoved = topologyEvents.getRemovedMastershipDataEntries();
286
287 linkEntriesAddedAll.addAll(linkEntriesAdded);
288 portEntriesAddedAll.addAll(portEntriesAdded);
289 portEntriesRemovedAll.addAll(portEntriesRemoved);
290 linkEntriesRemovedAll.addAll(linkEntriesRemoved);
291 switchAddedAll.addAll(switchAdded);
292 switchRemovedAll.addAll(switchRemoved);
293 mastershipRemovedAll.addAll(mastershipRemoved);
Sangho Shinbce900e2014-10-07 17:13:23 -0700294 numOfEvents++;
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700295
296 if (!portEntriesRemoved.isEmpty()) {
297 processPortRemoval(portEntriesRemoved);
298 }
299
300 if (!linkEntriesRemoved.isEmpty()) {
301 processLinkRemoval(linkEntriesRemoved);
302 }
303
304 if (!switchRemoved.isEmpty()) {
305 processSwitchRemoved(switchRemoved);
306 }
307
308 if (!mastershipRemoved.isEmpty()) {
Sangho Shin23f898d2014-10-13 16:54:00 -0700309 log.debug("Mastership is removed. Check if ports are down also.");
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700310 }
311
312 if (!linkEntriesAdded.isEmpty()) {
Sangho Shin23f898d2014-10-13 16:54:00 -0700313 processLinkAdd(linkEntriesAdded, false);
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700314 }
315
316 if (!portEntriesAdded.isEmpty()) {
317 processPortAdd(portEntriesAdded);
318 }
319
320 if (!switchAdded.isEmpty()) {
321 processSwitchAdd(switchAdded);
322 }
Sangho Shin51625342014-10-17 09:30:48 -0700323
Sangho Shinbce900e2014-10-07 17:13:23 -0700324 }
325
Sangho Shin23f898d2014-10-13 16:54:00 -0700326 // TODO: 100ms is enough to check both mastership removed events
327 // and the port removed events? What if the PORT_STATUS packets comes late?
Sangho Shin51625342014-10-17 09:30:48 -0700328 if (!mastershipRemovedAll.isEmpty()) {
329 if (portEntriesRemovedAll.isEmpty()) {
Saurav Das82e62972014-10-16 14:53:57 -0700330 log.debug("Just mastership is removed. Do not do anthing.");
Sangho Shin23f898d2014-10-13 16:54:00 -0700331 }
332 else {
333 HashMap<String, MastershipData> mastershipToRemove =
334 new HashMap<String, MastershipData>();
Sangho Shin51625342014-10-17 09:30:48 -0700335 for (MastershipData ms: mastershipRemovedAll) {
336 for (PortData port: portEntriesRemovedAll) {
Sangho Shin23f898d2014-10-13 16:54:00 -0700337 // TODO: check ALL ports of the switch are dead ..
338 if (port.getDpid().equals(ms.getDpid())) {
339 mastershipToRemove.put(ms.getDpid().toString(), ms);
340 }
341 }
342 log.debug("Swtich {} is really down.", ms.getDpid());
343 }
344 processMastershipRemoved(mastershipToRemove.values());
345 }
346 }
347
Sangho Shinbce900e2014-10-07 17:13:23 -0700348 log.debug("num events {}, num of process {}, "
349 + "num of Population {}", numOfEvents, numOfEventProcess,
350 numOfPopulation);
351 }
352
353 /**
Sangho Shin99918bd2014-10-08 15:52:35 -0700354 * Process the SwitchAdded events from topologyMananger.
355 * It does nothing. When a switch is added, then link will be added too.
356 * LinkAdded event will handle process all re-computation.
357 *
358 * @param switchAdded
359 */
360 private void processSwitchAdd(Collection<SwitchData> switchAdded) {
361
362 }
363
364 /**
Sangho Shinbce900e2014-10-07 17:13:23 -0700365 * Remove all ports connected to the switch removed
366 *
367 * @param mastershipRemoved master switch info removed
368 */
369 private void processMastershipRemoved(Collection<MastershipData>
370 mastershipRemoved) {
371 for (MastershipData mastership: mastershipRemoved) {
372 Switch sw = mutableTopology.getSwitch(mastership.getDpid());
373 for (Link link: sw.getOutgoingLinks()) {
374 Port dstPort = link.getDstPort();
375 IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch(
376 getSwId(dstPort.getDpid().toString()));
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700377 if (dstSw != null) {
378 dstSw.removePortFromGroups(dstPort.getNumber());
379 log.debug("MasterSwitch {} is gone: remove port {}", sw.getDpid(), dstPort);
380 }
Sangho Shinbce900e2014-10-07 17:13:23 -0700381 }
Sangho Shin61535402014-10-01 11:37:14 -0700382 }
Sangho Shin23f898d2014-10-13 16:54:00 -0700383
384 linksToAdd.clear();
385 linksDown.clear();
Sangho Shin61535402014-10-01 11:37:14 -0700386 }
Sangho Shinc8d2f592014-09-30 16:53:57 -0700387
Sangho Shinbce900e2014-10-07 17:13:23 -0700388 /**
389 * Remove all ports connected to the switch removed
390 *
391 * @param switchRemoved Switch removed
392 */
Sangho Shin5be3e532014-10-03 17:20:58 -0700393 private void processSwitchRemoved(Collection<SwitchData> switchRemoved) {
Sangho Shin23f898d2014-10-13 16:54:00 -0700394 log.debug("SwitchRemoved event occurred !!!");
Sangho Shin5be3e532014-10-03 17:20:58 -0700395 }
396
Sangho Shin61535402014-10-01 11:37:14 -0700397 /**
Sangho Shin99918bd2014-10-08 15:52:35 -0700398 * Report ports added to driver
Sangho Shinfbc572c2014-10-02 16:37:05 -0700399 *
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700400 * @param portEntries
401 */
402 private void processPortAdd(Collection<PortData> portEntries) {
Sangho Shin99918bd2014-10-08 15:52:35 -0700403 // TODO: do we need to add ports with delay?
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700404 for (PortData port : portEntries) {
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700405 Dpid dpid = port.getDpid();
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700406
Sangho Shinfbc572c2014-10-02 16:37:05 -0700407 IOF13Switch sw = (IOF13Switch) floodlightProvider.getMasterSwitch(
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700408 getSwId(port.getDpid().toString()));
Sangho Shin815af0c2014-10-10 13:05:45 -0700409 if (sw != null) {
Sangho Shin721ca042014-10-09 13:03:40 -0700410 sw.addPortToGroups(port.getPortNumber());
Sangho Shin15273b62014-10-16 22:22:05 -0700411 //log.debug("Add port {} to switch {}", port, dpid);
Sangho Shin815af0c2014-10-10 13:05:45 -0700412 }
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700413 }
414 }
415
416 /**
417 * Reports ports of new links to driver and recalculate ECMP SPG
Sangho Shin23f898d2014-10-13 16:54:00 -0700418 * If the link to add was removed before, then we just schedule the add link
419 * event and do not recompute the path now.
Sangho Shinfbc572c2014-10-02 16:37:05 -0700420 *
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700421 * @param linkEntries
422 */
Sangho Shin23f898d2014-10-13 16:54:00 -0700423 private void processLinkAdd(Collection<LinkData> linkEntries, boolean delayed) {
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700424
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700425 for (LinkData link : linkEntries) {
Sangho Shin5be3e532014-10-03 17:20:58 -0700426
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700427 SwitchPort srcPort = link.getSrc();
428 SwitchPort dstPort = link.getDst();
429
Sangho Shin23f898d2014-10-13 16:54:00 -0700430 String key = srcPort.getDpid().toString() +
431 dstPort.getDpid().toString();
432 if (!delayed) {
433 if (linksDown.containsKey(key)) {
434 linksToAdd.put(key, link);
435 linksDown.remove(key);
436 linkAddTask.reschedule(DELAY_TO_ADD_LINK, TimeUnit.SECONDS);
437 log.debug("Add link {} with 5 sec delay", link);
438 // TODO: What if we have multiple events of add link:
439 // one is new link add, the other one is link up for
440 // broken link? ECMPSPG function cannot deal with it for now
441 return;
442 }
443 }
444 else {
445 if (linksDown.containsKey(key)) {
446 linksToAdd.remove(key);
447 log.debug("Do not add the link {}: it is down again!", link);
448 return;
449 }
450 }
451
Sangho Shinfbc572c2014-10-02 16:37:05 -0700452 IOF13Switch srcSw = (IOF13Switch) floodlightProvider.getMasterSwitch(
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700453 getSwId(srcPort.getDpid().toString()));
Sangho Shinfbc572c2014-10-02 16:37:05 -0700454 IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch(
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700455 getSwId(dstPort.getDpid().toString()));
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700456
Sangho Shin815af0c2014-10-10 13:05:45 -0700457 if ((srcSw == null) || (dstSw == null))
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700458 continue;
459
460 srcSw.addPortToGroups(srcPort.getPortNumber());
461 dstSw.addPortToGroups(dstPort.getPortNumber());
Sangho Shin5be3e532014-10-03 17:20:58 -0700462
Sangho Shin15273b62014-10-16 22:22:05 -0700463 //log.debug("Add a link port {} to switch {} to add link {}", srcPort, srcSw,
464 // link);
465 //log.debug("Add a link port {} to switch {} to add link {}", dstPort, dstSw,
466 // link);
Sangho Shin815af0c2014-10-10 13:05:45 -0700467
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700468 }
Sangho Shinbce900e2014-10-07 17:13:23 -0700469 populateEcmpRoutingRules(false);
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700470 }
471
472 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700473 * Check if all links are gone b/w the two switches. If all links are gone,
474 * then we need to recalculate the path. Otherwise, just report link failure
475 * to the driver.
Sangho Shinfbc572c2014-10-02 16:37:05 -0700476 *
Sangho Shin61535402014-10-01 11:37:14 -0700477 * @param linkEntries
478 */
479 private void processLinkRemoval(Collection<LinkData> linkEntries) {
Sangho Shinbce900e2014-10-07 17:13:23 -0700480 boolean recomputationRequired = false;
481
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700482 for (LinkData link : linkEntries) {
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700483 SwitchPort srcPort = link.getSrc();
484 SwitchPort dstPort = link.getDst();
Sangho Shinc8d2f592014-09-30 16:53:57 -0700485
Sangho Shinfbc572c2014-10-02 16:37:05 -0700486 IOF13Switch srcSw = (IOF13Switch) floodlightProvider.getMasterSwitch(
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700487 getSwId(srcPort.getDpid().toString()));
Sangho Shinfbc572c2014-10-02 16:37:05 -0700488 IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch(
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700489 getSwId(dstPort.getDpid().toString()));
Sangho Shin815af0c2014-10-10 13:05:45 -0700490 if ((srcSw == null) || (dstSw == null))
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700491 /* If this link is not between two switches, ignore it */
492 continue;
Sangho Shin23f898d2014-10-13 16:54:00 -0700493
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700494 srcSw.removePortFromGroups(srcPort.getPortNumber());
495 dstSw.removePortFromGroups(dstPort.getPortNumber());
Sangho Shin815af0c2014-10-10 13:05:45 -0700496 log.debug("Remove port {} from switch {}", srcPort, srcSw);
497 log.debug("Remove port {} from switch {}", dstPort, dstSw);
498
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700499 Switch srcSwitch = mutableTopology.getSwitch(srcPort.getDpid());
500 if (srcSwitch.getLinkToNeighbor(dstPort.getDpid()) == null) {
Sangho Shinbce900e2014-10-07 17:13:23 -0700501 // TODO: it is only for debugging purpose.
Sangho Shin99918bd2014-10-08 15:52:35 -0700502 // We just need to call populateEcmpRoutingRules() and return;
Sangho Shinbce900e2014-10-07 17:13:23 -0700503 recomputationRequired = true;
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700504 log.debug("All links are gone b/w {} and {}", srcPort.getDpid(),
Sangho Shin5be3e532014-10-03 17:20:58 -0700505 dstPort.getDpid());
Sangho Shinc8d2f592014-09-30 16:53:57 -0700506 }
Sangho Shin23f898d2014-10-13 16:54:00 -0700507
508 String key = link.getSrc().getDpid().toString()+
509 link.getDst().getDpid().toString();
510 if (!linksDown.containsKey(key)) {
511 linksDown.put(key, link);
512 }
Sangho Shinc8d2f592014-09-30 16:53:57 -0700513 }
Sangho Shinbce900e2014-10-07 17:13:23 -0700514
515 if (recomputationRequired)
516 populateEcmpRoutingRules(false);
Sangho Shin61535402014-10-01 11:37:14 -0700517 }
Sangho Shinc8d2f592014-09-30 16:53:57 -0700518
Sangho Shin61535402014-10-01 11:37:14 -0700519 /**
Sangho Shin3a5fcad2014-10-01 14:14:49 -0700520 * report ports removed to the driver immediately
Sangho Shinfbc572c2014-10-02 16:37:05 -0700521 *
Sangho Shin61535402014-10-01 11:37:14 -0700522 * @param portEntries
523 */
524 private void processPortRemoval(Collection<PortData> portEntries) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700525 for (PortData port : portEntries) {
Sangho Shin61535402014-10-01 11:37:14 -0700526 Dpid dpid = port.getDpid();
Sangho Shin61535402014-10-01 11:37:14 -0700527
Sangho Shinfbc572c2014-10-02 16:37:05 -0700528 IOF13Switch sw = (IOF13Switch) floodlightProvider.getMasterSwitch(
Sangho Shin61535402014-10-01 11:37:14 -0700529 getSwId(port.getDpid().toString()));
Sangho Shin815af0c2014-10-10 13:05:45 -0700530 if (sw != null) {
Sangho Shinfbc572c2014-10-02 16:37:05 -0700531 sw.removePortFromGroups(port.getPortNumber());
Sangho Shin815af0c2014-10-10 13:05:45 -0700532 log.debug("Remove port {} from switch {}", port, dpid);
533 }
Sangho Shin61535402014-10-01 11:37:14 -0700534 }
Srikanth Vavilapallib7e5c5e2014-09-18 07:38:27 -0700535 }
Sangho Shin1aa93542014-09-22 09:49:44 -0700536
537 /**
Sangho Shin7330c032014-10-20 10:34:51 -0700538 * Add the link immediately
539 * The function is scheduled when link add event happens and called
540 * DELAY_TO_ADD_LINK seconds after the event to avoid link flip-flop.
541 */
542 private void delayedAddLink() {
543
544 processLinkAdd(linksToAdd.values(), true);
545
546 }
547
548
549 // ************************************
550 // ECMP shorted path routing functions
551 // ************************************
552
553 /**
Sangho Shin43cee112014-09-25 16:43:34 -0700554 * Populate routing rules walking through the ECMP shortest paths
Sangho Shinfbc572c2014-10-02 16:37:05 -0700555 *
Sangho Shin99918bd2014-10-08 15:52:35 -0700556 * @param modified if true, it "modifies" the rules
Sangho Shin1aa93542014-09-22 09:49:44 -0700557 */
Sangho Shin5be3e532014-10-03 17:20:58 -0700558 private void populateEcmpRoutingRules(boolean modified) {
559 graphs.clear();
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700560 Iterable<Switch> switches = mutableTopology.getSwitches();
Sangho Shin43cee112014-09-25 16:43:34 -0700561 for (Switch sw : switches) {
562 ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(sw);
Sangho Shinfbc572c2014-10-02 16:37:05 -0700563 graphs.put(sw, ecmpSPG);
564 //log.debug("ECMPShortestPathGraph is computed for switch {}",
565 // HexString.toHexString(sw.getDpid().value()));
Sangho Shin5be3e532014-10-03 17:20:58 -0700566 populateEcmpRoutingRulesForPath(sw, ecmpSPG, modified);
Sangho Shinfbc572c2014-10-02 16:37:05 -0700567 }
Sangho Shinbce900e2014-10-07 17:13:23 -0700568 numOfPopulation++;
Sangho Shinfbc572c2014-10-02 16:37:05 -0700569 }
Sangho Shin1aa93542014-09-22 09:49:44 -0700570
Sangho Shin99918bd2014-10-08 15:52:35 -0700571 /**
572 * populate routing rules to forward packets from the switch given to
573 * all other switches.
574 *
575 * @param sw source switch
576 * @param ecmpSPG shortest path from the the source switch to all others
577 * @param modified modification flag
578 */
Sangho Shinfbc572c2014-10-02 16:37:05 -0700579 private void populateEcmpRoutingRulesForPath(Switch sw,
Sangho Shin5be3e532014-10-03 17:20:58 -0700580 ECMPShortestPathGraph ecmpSPG, boolean modified) {
Sangho Shin43cee112014-09-25 16:43:34 -0700581
Sangho Shinfbc572c2014-10-02 16:37:05 -0700582 HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>> switchVia =
583 ecmpSPG.getAllLearnedSwitchesAndVia();
584 for (Integer itrIdx : switchVia.keySet()) {
585 //log.debug("ECMPShortestPathGraph:Switches learned in "
586 // + "Iteration{} from switch {}:",
587 // itrIdx,
588 // HexString.toHexString(sw.getDpid().value()));
589 HashMap<Switch, ArrayList<ArrayList<Dpid>>> swViaMap =
590 switchVia.get(itrIdx);
591 for (Switch targetSw : swViaMap.keySet()) {
Sangho Shin5be3e532014-10-03 17:20:58 -0700592 //log.debug("ECMPShortestPathGraph:****switch {} via:",
593 // HexString.toHexString(targetSw.getDpid().value()));
Sangho Shinfbc572c2014-10-02 16:37:05 -0700594 String destSw = sw.getDpid().toString();
595 List<String> fwdToSw = new ArrayList<String>();
596
Sangho Shinfbc572c2014-10-02 16:37:05 -0700597 for (ArrayList<Dpid> via : swViaMap.get(targetSw)) {
Sangho Shin5be3e532014-10-03 17:20:58 -0700598 //log.debug("ECMPShortestPathGraph:******{}) {}", ++i, via);
Sangho Shinfbc572c2014-10-02 16:37:05 -0700599 if (via.isEmpty()) {
600 fwdToSw.add(destSw);
Sangho Shin43cee112014-09-25 16:43:34 -0700601 }
Sangho Shinfbc572c2014-10-02 16:37:05 -0700602 else {
603 fwdToSw.add(via.get(0).toString());
604 }
Sangho Shin43cee112014-09-25 16:43:34 -0700605 }
Sangho Shin5be3e532014-10-03 17:20:58 -0700606 setRoutingRule(targetSw, destSw, fwdToSw, modified);
Sangho Shineb083032014-09-22 16:11:34 -0700607 }
Sangho Shinfbc572c2014-10-02 16:37:05 -0700608
609 // Send Barrier Message and make sure all rules are set
610 // before we set the rules to next routers
Saurav Dasa962a692014-10-17 14:52:38 -0700611 // TODO: barriers to all switches in this update stage
Sangho Shinfbc572c2014-10-02 16:37:05 -0700612 IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
613 getSwId(sw.getDpid().toString()));
Sangho Shin5be3e532014-10-03 17:20:58 -0700614 if (sw13 != null) {
Saurav Dasa962a692014-10-17 14:52:38 -0700615 OFBarrierReplyFuture replyFuture = null;
Sangho Shin5be3e532014-10-03 17:20:58 -0700616 try {
Saurav Dasa962a692014-10-17 14:52:38 -0700617 replyFuture = sw13.sendBarrier();
Sangho Shin5be3e532014-10-03 17:20:58 -0700618 } catch (IOException e) {
Saurav Dasa962a692014-10-17 14:52:38 -0700619 log.error("Error sending barrier request to switch {}",
620 sw13.getId(), e.getCause());
Sangho Shin5be3e532014-10-03 17:20:58 -0700621 }
Saurav Dasa962a692014-10-17 14:52:38 -0700622 OFBarrierReply br = null;
623 try {
624 br = replyFuture.get(2, TimeUnit.SECONDS);
625 } catch (TimeoutException | InterruptedException | ExecutionException e) {
626 // XXX for some reason these exceptions are not being thrown
627 }
628 if (br == null) {
629 log.warn("Did not receive barrier-reply from {}", sw13.getId());
630 // XXX take corrective action
631 }
632
Sangho Shinfbc572c2014-10-02 16:37:05 -0700633 }
634 }
635
636 }
637
Sangho Shinfbc572c2014-10-02 16:37:05 -0700638 /**
639 *
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700640 * Set routing rules in targetSw {forward packets to fwdToSw switches in
641 * order to send packets to destSw} - If the target switch is an edge router
642 * and final destnation switch is also an edge router, then set IP
643 * forwarding rules to subnets - If only the target switch is an edge
644 * router, then set IP forwarding rule to the transit router loopback IP
645 * address - If the target is a transit router, then just set the MPLS
646 * forwarding rule
Sangho Shinfbc572c2014-10-02 16:37:05 -0700647 *
Sangho Shin43cee112014-09-25 16:43:34 -0700648 * @param targetSw Switch to set the rules
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700649 * @param destSw Final destination switches
Sangho Shin43cee112014-09-25 16:43:34 -0700650 * @param fwdToSw next hop switches
651 */
Sangho Shin99918bd2014-10-08 15:52:35 -0700652 private void setRoutingRule(Switch targetSw, String destSw,
653 List<String> fwdToSw, boolean modified) {
Sangho Shin43cee112014-09-25 16:43:34 -0700654
Sangho Shin43cee112014-09-25 16:43:34 -0700655 if (fwdToSw.isEmpty()) {
656 fwdToSw.add(destSw);
657 }
658
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700659 // if both target SW and dest SW are an edge router, then set IP table
Sangho Shin43cee112014-09-25 16:43:34 -0700660 if (IsEdgeRouter(targetSw.getDpid().toString()) &&
661 IsEdgeRouter(destSw)) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700662 // We assume that there is at least one transit router b/w edge
663 // routers
Sangho Shin43cee112014-09-25 16:43:34 -0700664 Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw));
665 String subnets = destSwitch.getStringAttribute("subnets");
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700666 setIpTableRouterSubnet(targetSw, subnets, getMplsLabel(destSw)
Sangho Shin5be3e532014-10-03 17:20:58 -0700667 , fwdToSw, modified);
Sangho Shin43cee112014-09-25 16:43:34 -0700668
Sangho Shin43cee112014-09-25 16:43:34 -0700669 String routerIp = destSwitch.getStringAttribute("routerIp");
Sangho Shin5be3e532014-10-03 17:20:58 -0700670 setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw,
671 null, modified);
Sangho Shin721ca042014-10-09 13:03:40 -0700672 // Edge router can be a transit router
673 setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified);
Sangho Shin43cee112014-09-25 16:43:34 -0700674 }
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700675 // Only if the target switch is the edge router, then set the IP rules
Sangho Shin43cee112014-09-25 16:43:34 -0700676 else if (IsEdgeRouter(targetSw.getDpid().toString())) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700677 // We assume that there is at least one transit router b/w edge
678 // routers
Sangho Shin43cee112014-09-25 16:43:34 -0700679 Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw));
680 String routerIp = destSwitch.getStringAttribute("routerIp");
Sangho Shin5be3e532014-10-03 17:20:58 -0700681 setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw,
682 null, modified);
Sangho Shin721ca042014-10-09 13:03:40 -0700683 // Edge router can be a transit router
684 setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified);
Sangho Shin43cee112014-09-25 16:43:34 -0700685 }
686 // if it is a transit router, then set rules in the MPLS table
687 else {
Sangho Shin5be3e532014-10-03 17:20:58 -0700688 setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified);
Sangho Shin43cee112014-09-25 16:43:34 -0700689 }
690
691 }
692
Sangho Shinfbc572c2014-10-02 16:37:05 -0700693 /**
694 * Set IP forwarding rule to the gateway of each subnet of switches
695 *
696 * @param targetSw Switch to set rules
697 * @param subnets subnet information
698 * @param mplsLabel destination MPLS label
699 * @param fwdToSw router to forward packets to
700 */
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700701 private void setIpTableRouterSubnet(Switch targetSw, String subnets,
Sangho Shin5be3e532014-10-03 17:20:58 -0700702 String mplsLabel, List<String> fwdToSw, boolean modified) {
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700703
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700704 Collection<MatchActionOperationEntry> entries =
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700705 new ArrayList<MatchActionOperationEntry>();
706
707 try {
708 JSONArray arry = new JSONArray(subnets);
709 for (int i = 0; i < arry.length(); i++) {
710 String subnetIp = (String) arry.getJSONObject(i).get("subnetIp");
Sangho Shin5be3e532014-10-03 17:20:58 -0700711 setIpTableRouter(targetSw, subnetIp, mplsLabel, fwdToSw, entries,
712 modified);
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700713 }
714 } catch (JSONException e) {
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700715 e.printStackTrace();
716 }
717
718 if (!entries.isEmpty()) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700719 IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700720 getSwId(targetSw.getDpid().toString()));
721
Sangho Shin721ca042014-10-09 13:03:40 -0700722 if (sw13 != null) {
723 try {
724 sw13.pushFlows(entries);
725 } catch (IOException e) {
726 e.printStackTrace();
727 }
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700728 }
729 }
730
731 }
732
Sangho Shin43cee112014-09-25 16:43:34 -0700733 /**
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700734 * Set IP forwarding rule - If the destination is the next hop, then do not
735 * push MPLS, just decrease the NW TTL - Otherwise, push MPLS label and set
736 * the MPLS ID
Sangho Shinfbc572c2014-10-02 16:37:05 -0700737 *
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700738 * @param sw target switch to set rules
Sangho Shin43cee112014-09-25 16:43:34 -0700739 * @param subnetIp Match IP address
740 * @param mplsLabel MPLS label of final destination router
741 * @param fwdToSws next hop routers
Sangho Shin11d4e0f2014-09-30 12:00:33 -0700742 * @param entries
Sangho Shin43cee112014-09-25 16:43:34 -0700743 */
744 private void setIpTableRouter(Switch sw, String subnetIp, String mplsLabel,
Sangho Shin5be3e532014-10-03 17:20:58 -0700745 List<String> fwdToSws, Collection<MatchActionOperationEntry> entries,
746 boolean modified) {
Sangho Shin43cee112014-09-25 16:43:34 -0700747
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700748 Ipv4Match ipMatch = new Ipv4Match(subnetIp);
Sangho Shin43cee112014-09-25 16:43:34 -0700749 List<Action> actions = new ArrayList<>();
Sangho Shin721ca042014-10-09 13:03:40 -0700750 GroupAction groupAction = new GroupAction();
Sangho Shin43cee112014-09-25 16:43:34 -0700751
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700752 // If destination SW is the same as the fwd SW, then do not push MPLS
753 // label
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700754 if (fwdToSws.size() > 1) {
Sangho Shin43cee112014-09-25 16:43:34 -0700755 PushMplsAction pushMplsAction = new PushMplsAction();
756 SetMplsIdAction setIdAction = new SetMplsIdAction(Integer.parseInt(mplsLabel));
757 CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction();
Sangho Shin463bee52014-09-29 15:14:43 -0700758 DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1);
Sangho Shin43cee112014-09-25 16:43:34 -0700759
Sangho Shin62ce5c12014-10-08 16:24:40 -0700760 //actions.add(pushMplsAction);
761 //actions.add(copyTtlOutAction);
762 //actions.add(decMplsTtlAction);
Saurav Das82e62972014-10-16 14:53:57 -0700763 // actions.add(setIdAction);
Sangho Shin721ca042014-10-09 13:03:40 -0700764 groupAction.setEdgeLabel(Integer.parseInt(mplsLabel));
Sangho Shin43cee112014-09-25 16:43:34 -0700765 }
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700766 else {
767 String fwdToSw = fwdToSws.get(0);
768 if (getMplsLabel(fwdToSw).equals(mplsLabel)) {
769 DecNwTtlAction decTtlAction = new DecNwTtlAction(1);
770 actions.add(decTtlAction);
771 }
772 else {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700773 SetMplsIdAction setIdAction = new SetMplsIdAction(
774 Integer.parseInt(mplsLabel));
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700775 CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction();
Sangho Shin463bee52014-09-29 15:14:43 -0700776 DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1);
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700777
Sangho Shin62ce5c12014-10-08 16:24:40 -0700778 //actions.add(pushMplsAction);
779 //actions.add(copyTtlOutAction);
780 //actions.add(decMplsTtlAction);
Saurav Das82e62972014-10-16 14:53:57 -0700781 // actions.add(setIdAction);
Sangho Shin721ca042014-10-09 13:03:40 -0700782 groupAction.setEdgeLabel(Integer.parseInt(mplsLabel));
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700783 }
784 }
Sangho Shin43cee112014-09-25 16:43:34 -0700785
Sangho Shin43cee112014-09-25 16:43:34 -0700786 for (String fwdSw : fwdToSws) {
787 groupAction.addSwitch(new Dpid(fwdSw));
788 }
789 actions.add(groupAction);
790
Sangho Shin99918bd2014-10-08 15:52:35 -0700791 MatchAction matchAction = new MatchAction(new MatchActionId(matchActionId++),
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700792 new SwitchPort((long) 0, (short) 0), ipMatch, actions);
Sangho Shin43cee112014-09-25 16:43:34 -0700793
Sangho Shin5be3e532014-10-03 17:20:58 -0700794 Operator operator = null;
795 if (modified)
796 operator = Operator.MODIFY;
797 else
798 operator = Operator.ADD;
799
Sangho Shin43cee112014-09-25 16:43:34 -0700800 MatchActionOperationEntry maEntry =
Sangho Shin5be3e532014-10-03 17:20:58 -0700801 new MatchActionOperationEntry(operator, matchAction);
Sangho Shin43cee112014-09-25 16:43:34 -0700802
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700803 IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700804 getSwId(sw.getDpid().toString()));
805
Sangho Shin5be3e532014-10-03 17:20:58 -0700806 if (sw13 != null) {
807 try {
Sangho Shinbce900e2014-10-07 17:13:23 -0700808 //printMatchActionOperationEntry(sw, maEntry);
Sangho Shin5be3e532014-10-03 17:20:58 -0700809 if (entries != null)
810 entries.add(maEntry);
811 else
812 sw13.pushFlow(maEntry);
813 } catch (IOException e) {
814 e.printStackTrace();
815 }
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700816 }
817
Sangho Shin43cee112014-09-25 16:43:34 -0700818 }
819
Sangho Shinac5ee2b2014-09-28 21:27:20 -0700820 /**
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700821 * Set MPLS forwarding rules to MPLS table
822 * </p>
823 * If the destination is the same as the next hop to forward packets then,
824 * pop the MPLS label according to PHP rule. Here, if BoS is set, then
825 * copy TTL In and decrement NW TTL. Otherwise, it just decrement the MPLS
826 * TTL of the another MPLS header.
827 * If the next hop is not the destination, just forward packets to next
828 * hops using Group action.
Sangho Shinfbc572c2014-10-02 16:37:05 -0700829 *
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700830 * @param sw Switch to set the rules
Sangho Shin43cee112014-09-25 16:43:34 -0700831 * @param mplsLabel destination MPLS label
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700832 * @param fwdSws next hop switches
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700833 * */
Sangho Shin5be3e532014-10-03 17:20:58 -0700834 private void setMplsTable(Switch sw, String mplsLabel, List<String> fwdSws,
835 boolean modified) {
Sangho Shin463bee52014-09-29 15:14:43 -0700836
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700837 if (fwdSws.isEmpty())
838 return;
Sangho Shin43cee112014-09-25 16:43:34 -0700839
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700840 Collection<MatchActionOperationEntry> maEntries =
841 new ArrayList<MatchActionOperationEntry>();
842 String fwdSw1 = fwdSws.get(0);
Sangho Shin43cee112014-09-25 16:43:34 -0700843
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700844 if (fwdSws.size() == 1 && mplsLabel.equals(getMplsLabel(fwdSw1))) {
845 // One rule for Bos = 1
846 MplsMatch mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel), true);
847 List<Action> actions = new ArrayList<Action>();
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700848
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700849 PopMplsAction popAction = new PopMplsAction(EthType.IPv4);
850 CopyTtlInAction copyTtlInAction = new CopyTtlInAction();
851 DecNwTtlAction decNwTtlAction = new DecNwTtlAction(1);
852
853 actions.add(copyTtlInAction);
854 actions.add(popAction);
855 actions.add(decNwTtlAction);
856
857 GroupAction groupAction = new GroupAction();
858 groupAction.addSwitch(new Dpid(fwdSw1));
859 actions.add(groupAction);
860
861 MatchAction matchAction = new MatchAction(new MatchActionId(matchActionId++),
862 new SwitchPort((long) 0, (short) 0), mplsMatch, actions);
863 Operator operator = Operator.ADD;
864 MatchActionOperationEntry maEntry =
865 new MatchActionOperationEntry(operator, matchAction);
866 maEntries.add(maEntry);
867
868 // One rule for Bos = 0
Sangho Shin23f898d2014-10-13 16:54:00 -0700869 MplsMatch mplsMatchBos = new MplsMatch(Integer.parseInt(mplsLabel), false);
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700870 List<Action> actionsBos = new ArrayList<Action>();
Sangho Shin15273b62014-10-16 22:22:05 -0700871 PopMplsAction popActionBos = new PopMplsAction(EthType.MPLS_UNICAST);
872 DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1);
873
874 actionsBos.add(copyTtlInAction);
875 actionsBos.add(popActionBos);
876 actionsBos.add(decMplsTtlAction);
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700877 actionsBos.add(groupAction);
878
879 MatchAction matchActionBos = new MatchAction(new MatchActionId(matchActionId++),
880 new SwitchPort((long) 0, (short) 0), mplsMatchBos, actionsBos);
881 MatchActionOperationEntry maEntryBos =
882 new MatchActionOperationEntry(operator, matchActionBos);
883 maEntries.add(maEntryBos);
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700884 }
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700885 else {
886 MplsMatch mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel), false);
887 List<Action> actions = new ArrayList<Action>();
Sangho Shin43cee112014-09-25 16:43:34 -0700888
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700889 DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1);
890 actions.add(decMplsTtlAction);
Sangho Shin43cee112014-09-25 16:43:34 -0700891
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700892 GroupAction groupAction = new GroupAction();
893 for (String fwdSw : fwdSws)
894 groupAction.addSwitch(new Dpid(fwdSw));
895 actions.add(groupAction);
Sangho Shin5be3e532014-10-03 17:20:58 -0700896
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700897 MatchAction matchAction = new MatchAction(new MatchActionId(
898 matchActionId++),
899 new SwitchPort((long) 0, (short) 0), mplsMatch, actions);
900 Operator operator = Operator.ADD;
901 MatchActionOperationEntry maEntry =
902 new MatchActionOperationEntry(operator, matchAction);
903 maEntries.add(maEntry);
Sangho Shine2e9bcd2014-10-13 15:14:18 -0700904
905 // BoS = 1
906 MplsMatch mplsMatchBoS = new MplsMatch(Integer.parseInt(mplsLabel), true);
907 List<Action> actionsBoS = new ArrayList<Action>();
908
909 DecMplsTtlAction decMplsTtlActionBoS = new DecMplsTtlAction(1);
910 actionsBoS.add(decMplsTtlActionBoS);
911
912 GroupAction groupActionBoS = new GroupAction();
913 for (String fwdSw : fwdSws)
914 groupActionBoS.addSwitch(new Dpid(fwdSw));
915 actionsBoS.add(groupActionBoS);
916
917 MatchAction matchActionBos = new MatchAction(new MatchActionId(
918 matchActionId++),
919 new SwitchPort((long) 0, (short) 0), mplsMatchBoS, actionsBoS);
920 MatchActionOperationEntry maEntryBoS =
921 new MatchActionOperationEntry(operator, matchActionBos);
922 maEntries.add(maEntryBoS);
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700923 }
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -0700924 IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700925 getSwId(sw.getDpid().toString()));
926
Sangho Shin5be3e532014-10-03 17:20:58 -0700927 if (sw13 != null) {
928 try {
Sangho Shinbce900e2014-10-07 17:13:23 -0700929 //printMatchActionOperationEntry(sw, maEntry);
Sangho Shinc1b8dea2014-10-13 12:03:28 -0700930 sw13.pushFlows(maEntries);
Sangho Shin5be3e532014-10-03 17:20:58 -0700931 } catch (IOException e) {
932 e.printStackTrace();
933 }
Sangho Shin9c0f4c32014-09-26 16:02:38 -0700934 }
Sangho Shin43cee112014-09-25 16:43:34 -0700935 }
936
Sangho Shin7330c032014-10-20 10:34:51 -0700937
938 // ************************************
939 // Policy routing classes and functions
940 // ************************************
941
Sangho Shin5b8f5452014-10-20 11:46:01 -0700942 class PolicyInfo {
943
Sangho Shine020cc32014-10-20 13:28:02 -0700944 String policyId;
Sangho Shin5b8f5452014-10-20 11:46:01 -0700945 PacketMatch match;
946 int priority;
Sangho Shine020cc32014-10-20 13:28:02 -0700947 String tunnelId;
Sangho Shin5b8f5452014-10-20 11:46:01 -0700948
Sangho Shine020cc32014-10-20 13:28:02 -0700949 PolicyInfo(String pid, PacketMatch match, int priority, String tid) {
Sangho Shin5b8f5452014-10-20 11:46:01 -0700950 this.policyId = pid;
951 this.match = match;
952 this.priority = priority;
953 this.tunnelId = tid;
954 }
955 }
956
Sangho Shine020cc32014-10-20 13:28:02 -0700957 class TunnelRouteInfo {
Sangho Shin7330c032014-10-20 10:34:51 -0700958
959 String srcSwDpid;
960 List<Dpid> fwdSwDpids;
961 List<String> route;
962
Sangho Shine020cc32014-10-20 13:28:02 -0700963 TunnelRouteInfo() {
Sangho Shin7330c032014-10-20 10:34:51 -0700964 fwdSwDpids = new ArrayList<Dpid>();
965 route = new ArrayList<String>();
966 }
967
968 void setSrcDpid(String dpid) {
969 this.srcSwDpid = dpid;
970 }
971
972 void setFwdSwDpid(List<Dpid> dpid) {
973 this.fwdSwDpids = dpid;
974 }
975
976 void addRoute(String id) {
977 route.add(id);
978 }
979
980 void setRoute(List<String> r) {
981 this.route = r;
982 }
983
984 String getSrcSwDpid() {
985 return this.srcSwDpid;
986 }
987
988 List<Dpid> getFwdSwDpid() {
989 return this.fwdSwDpids;
990 }
991
992 List<String> getRoute() {
993 return this.route;
994 }
995 }
996
Sangho Shin15273b62014-10-16 22:22:05 -0700997 /**
998 * Create a tunnel for policy routing
999 * It delivers the node IDs of tunnels to driver.
1000 * Split the node IDs if number of IDs exceeds the limit for stitching.
1001 *
1002 * @param tunnelId Node IDs for the tunnel
1003 * @param Ids tunnel ID
1004 */
Sangho Shine020cc32014-10-20 13:28:02 -07001005 public boolean createTunnel(String tunnelId, List<Dpid> dpids) {
Sangho Shin15273b62014-10-16 22:22:05 -07001006
Sangho Shin55d00e12014-10-20 12:13:07 -07001007 if (dpids.isEmpty() || dpids.size() < 2) {
Sangho Shin15273b62014-10-16 22:22:05 -07001008 log.debug("Wrong tunnel information");
1009 return false;
1010 }
1011
Sangho Shin55d00e12014-10-20 12:13:07 -07001012 List<String> Ids = new ArrayList<String>();
1013 for (Dpid dpid: dpids) {
1014 Ids.add(getMplsLabel(dpid.toString()));
1015 }
1016
Sangho Shine020cc32014-10-20 13:28:02 -07001017 HashMap<String, TunnelRouteInfo> stitchingRule = getStitchingRule(Ids);
1018 stitchInfo.put(tunnelId, stitchingRule);
Sangho Shin15273b62014-10-16 22:22:05 -07001019 if (stitchingRule == null) {
1020 log.debug("Failed to get the policy rule.");
1021 return false;
1022 }
1023 HashMap<String, Integer> switchGroupPair = new HashMap<String, Integer>();
1024 for (String targetDpid: stitchingRule.keySet()) {
Sangho Shine020cc32014-10-20 13:28:02 -07001025 TunnelRouteInfo route = stitchingRule.get(targetDpid);
Sangho Shin15273b62014-10-16 22:22:05 -07001026
1027 IOF13Switch targetSw = (IOF13Switch) floodlightProvider.getMasterSwitch(
1028 getSwId(targetDpid.toString()));
1029
1030 if (targetSw == null) {
1031 log.debug("Switch {} is gone.", targetDpid);
1032 return false;
1033 }
1034
1035 NeighborSet ns = new NeighborSet();
1036 for (Dpid dpid: route.getFwdSwDpid())
1037 ns.addDpid(dpid);
1038
1039 printTunnelInfo(targetSw, tunnelId, route.getRoute(), ns);
1040 int groupId = targetSw.createTunnel(tunnelId, route.getRoute(), ns);
1041 switchGroupPair.put(targetDpid.toString(), groupId);
1042
1043 }
1044
Sangho Shine020cc32014-10-20 13:28:02 -07001045 tunnelGroupMap.put(tunnelId, switchGroupPair);
1046 tunnelTable.put(tunnelId, dpids);
Sangho Shin15273b62014-10-16 22:22:05 -07001047
1048 return true;
1049 }
1050
1051 /**
Sangho Shine020cc32014-10-20 13:28:02 -07001052 * Return router DPIDs for the tunnel
1053 *
1054 * @param tid tunnel ID
1055 * @return List of DPID
1056 */
1057 public List<Dpid> getTunnelInfo(String tid) {
1058 return tunnelTable.get(tid);
1059 }
1060
1061 /**
Sangho Shin15273b62014-10-16 22:22:05 -07001062 * Set policy table for policy routing
1063 *
1064 * @param sw
1065 * @param mplsLabel
1066 */
Sangho Shine020cc32014-10-20 13:28:02 -07001067 public void createPolicy(String pid, MACAddress srcMac, MACAddress dstMac,
Sangho Shin15273b62014-10-16 22:22:05 -07001068 Short etherType, IPv4Net srcIp, IPv4Net dstIp, Byte ipProto,
Sangho Shine020cc32014-10-20 13:28:02 -07001069 Short srcTcpPort, Short dstTcpPort, int priority, String tid) {
Sangho Shin15273b62014-10-16 22:22:05 -07001070
Sangho Shine020cc32014-10-20 13:28:02 -07001071 HashMap<String, TunnelRouteInfo> routeInfo = stitchInfo.get(tid);
1072 HashMap<String, Integer> switchGroupPair = tunnelGroupMap.get(tid);
Sangho Shin5b8f5452014-10-20 11:46:01 -07001073
1074 PacketMatchBuilder packetBuilder = new PacketMatchBuilder();
1075
1076 if (srcMac != null)
1077 packetBuilder.setSrcMac(srcMac);
1078 if (dstMac != null)
1079 packetBuilder.setDstMac(dstMac);
1080 if (etherType != null)
1081 packetBuilder.setEtherType(etherType);
1082 if (srcIp != null)
1083 packetBuilder.setSrcIp(srcIp.address(), srcIp.prefixLen());
1084 if (dstIp != null)
1085 packetBuilder.setDstIp(dstIp.address(), dstIp.prefixLen());
1086 if (ipProto != null)
1087 packetBuilder.setIpProto(ipProto);
1088 if (srcTcpPort > 0)
1089 packetBuilder.setSrcTcpPort(srcTcpPort);
1090 if (dstTcpPort > 0)
1091 packetBuilder.setDstTcpPort(dstTcpPort);
1092 PacketMatch policyMatch = packetBuilder.build();
1093
Sangho Shin15273b62014-10-16 22:22:05 -07001094 for (String srcDpid: routeInfo.keySet()) {
Sangho Shin15273b62014-10-16 22:22:05 -07001095 List<Action> actions = new ArrayList<>();
1096 GroupAction groupAction = new GroupAction();
1097 int gropuId = switchGroupPair.get(srcDpid);
1098 groupAction.setGroupId(gropuId);
1099 actions.add(groupAction);
1100
1101 MatchAction matchAction = new MatchAction(new MatchActionId(
1102 matchActionId++),
Sangho Shin5b8f5452014-10-20 11:46:01 -07001103 new SwitchPort((long) 0, (short) 0), policyMatch, priority,
1104 actions);
Sangho Shin15273b62014-10-16 22:22:05 -07001105 MatchActionOperationEntry maEntry =
1106 new MatchActionOperationEntry(Operator.ADD, matchAction);
1107
1108 IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
1109 getSwId(srcDpid));
1110
1111 if (sw13 != null) {
1112 printMatchActionOperationEntry(sw13, maEntry);
1113 try {
1114 sw13.pushFlow(maEntry);
1115 } catch (IOException e) {
1116 e.printStackTrace();
1117 }
1118 }
1119 }
Sangho Shin5b8f5452014-10-20 11:46:01 -07001120
1121 PolicyInfo policyInfo = new PolicyInfo(pid, policyMatch, priority, tid);
Sangho Shine020cc32014-10-20 13:28:02 -07001122 policyTable.put(pid, policyInfo);
Sangho Shin15273b62014-10-16 22:22:05 -07001123 }
1124
1125 /**
Sangho Shin15273b62014-10-16 22:22:05 -07001126 *
1127 *
1128 * @param srcSw
1129 * @param dstSw
1130 * @param route
1131 * @return
1132 */
Sangho Shine020cc32014-10-20 13:28:02 -07001133 private HashMap<String, TunnelRouteInfo> getStitchingRule(List<String> route) {
Sangho Shin15273b62014-10-16 22:22:05 -07001134
1135 if (route.isEmpty() || route.size() < 2)
1136 return null;
1137
Sangho Shine020cc32014-10-20 13:28:02 -07001138 HashMap<String, TunnelRouteInfo> rules = new HashMap<String, TunnelRouteInfo>();
Sangho Shin15273b62014-10-16 22:22:05 -07001139
1140 Switch srcSw = this.getSwitchFromNodeId(route.get(0));
1141 String srcDpid = srcSw.getDpid().toString();
1142
1143 if (route.size() <= MAX_NUM_LABELS+1) {
Sangho Shine020cc32014-10-20 13:28:02 -07001144 TunnelRouteInfo info = new TunnelRouteInfo();
Sangho Shin15273b62014-10-16 22:22:05 -07001145 info.setSrcDpid(srcSw.getDpid().toString());
1146 List<Dpid> fwdSwDpids = getForwardingSwitchForNodeId(srcSw, route.get(1));
1147 info.setFwdSwDpid(fwdSwDpids);
1148 route.remove(0);
1149 info.setRoute(route);
1150 rules.put(srcDpid, info);
1151 return rules;
1152 }
1153
1154 int i = 0;
Sangho Shine020cc32014-10-20 13:28:02 -07001155 TunnelRouteInfo routeInfo = new TunnelRouteInfo();
Sangho Shin15273b62014-10-16 22:22:05 -07001156 String prevNodeId = null;
1157 boolean checkNeighbor = true;
1158
1159 for (String nodeId: route) {
1160 if (i == 0) {
1161 routeInfo.setSrcDpid(srcDpid);
1162 srcSw = getSwitchFromNodeId(nodeId);
1163 i++;
1164 }
1165 else if (i == 1) {
1166 if (checkNeighbor) {
1167 // Check if next node is the neighbor SW of the source SW
1168 List<Dpid> fwdSwDpids = getForwardingSwitchForNodeId(srcSw, nodeId);
1169 if (fwdSwDpids == null || fwdSwDpids.isEmpty()) {
1170 log.debug("There is no route from node {} to node {}", srcSw.getDpid(), nodeId);
1171 return null;
1172 }
1173 // If first Id is one of the neighbors, do not include it to route, but set it as a fwd SW.
1174 boolean match = false;
1175 for (Dpid dpid: fwdSwDpids) {
1176 if (getMplsLabel(dpid.toString()).toString().equals(nodeId)) {
1177 List<Dpid> fwdSws = new ArrayList<Dpid>();
1178 fwdSws.add(dpid);
1179 routeInfo.setFwdSwDpid(fwdSws);
1180 match = true;
1181 break;
1182 }
1183 }
1184 if (!match) {
1185 routeInfo.addRoute(nodeId);
1186 routeInfo.setFwdSwDpid(fwdSwDpids);
1187 i++;
1188 }
1189
1190 checkNeighbor = false;
1191 }
1192 else {
1193 routeInfo.addRoute(nodeId);
1194 i++;
1195 }
1196 }
1197 else {
1198 routeInfo.addRoute(nodeId);
1199 i++;
1200 }
1201
1202 if (i == MAX_NUM_LABELS+1) {
1203 rules.put(srcDpid, routeInfo);
Sangho Shine020cc32014-10-20 13:28:02 -07001204 routeInfo = new TunnelRouteInfo();
Sangho Shin15273b62014-10-16 22:22:05 -07001205 srcSw = getSwitchFromNodeId(nodeId);
1206 srcDpid = getSwitchFromNodeId(nodeId).getDpid().toString();
1207 routeInfo.setSrcDpid(srcDpid);
1208 i = 1;
1209 checkNeighbor = true;
1210 }
1211 }
1212
1213 if (i < MAX_NUM_LABELS+1) {
1214 rules.put(srcDpid, routeInfo);
1215 }
1216
1217 return rules;
1218 }
1219
Sangho Shin5b8f5452014-10-20 11:46:01 -07001220 /**
1221 * Remove all policies applied to specific tunnel.
1222 *
1223 * @param srcMac
1224 * @param dstMac
1225 * @param etherType
1226 * @param srcIp
1227 * @param dstIp
1228 * @param ipProto
1229 * @param srcTcpPort
1230 * @param dstTcpPort
1231 * @param tid
1232 */
Sangho Shine020cc32014-10-20 13:28:02 -07001233 public void removePolicy(String pid) {
1234 PolicyInfo policyInfo = policyTable.get(pid);
Sangho Shin5b8f5452014-10-20 11:46:01 -07001235 PacketMatch policyMatch = policyInfo.match;
Sangho Shine020cc32014-10-20 13:28:02 -07001236 String tid = policyInfo.tunnelId;
Sangho Shin5b8f5452014-10-20 11:46:01 -07001237 int priority = policyInfo.priority;
1238
1239 List<Action> actions = new ArrayList<>();
1240 int gropuId = 0; // dummy group ID
1241 GroupAction groupAction = new GroupAction();
1242 groupAction.setGroupId(gropuId);
1243 actions.add(groupAction);
1244
1245 MatchAction matchAction = new MatchAction(new MatchActionId(
1246 matchActionId++),
1247 new SwitchPort((long) 0, (short) 0), policyMatch, priority,
1248 actions);
1249 MatchActionOperationEntry maEntry =
1250 new MatchActionOperationEntry(Operator.REMOVE, matchAction);
1251
Sangho Shine020cc32014-10-20 13:28:02 -07001252 HashMap<String, Integer> groupInfo = tunnelGroupMap.get(tid);
Sangho Shin5b8f5452014-10-20 11:46:01 -07001253
1254 for (String dpid: groupInfo.keySet()) {
1255 IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch(
1256 getSwId(dpid));
1257
1258 if (sw13 != null) {
1259 printMatchActionOperationEntry(sw13, maEntry);
1260 try {
1261 sw13.pushFlow(maEntry);
1262 } catch (IOException e) {
1263 e.printStackTrace();
1264 log.debug("policy remove failed due to pushFlow() exception");
1265 return;
1266 }
1267 }
1268 }
1269
1270 log.debug("Policy {} is removed.", pid);
Sangho Shine020cc32014-10-20 13:28:02 -07001271 }
Sangho Shin5b8f5452014-10-20 11:46:01 -07001272
Sangho Shin7330c032014-10-20 10:34:51 -07001273
Sangho Shine020cc32014-10-20 13:28:02 -07001274 public void removeTunnel(String tunnelId) {
Sangho Shin55d00e12014-10-20 12:13:07 -07001275
1276
1277 }
1278
Sangho Shin7330c032014-10-20 10:34:51 -07001279 // ************************************
1280 // Utility functions
1281 // ************************************
1282
Sangho Shin15273b62014-10-16 22:22:05 -07001283 /**
Sangho Shin7330c032014-10-20 10:34:51 -07001284 * Get the forwarding Switch DPIDs to send packets to a node
Sangho Shin15273b62014-10-16 22:22:05 -07001285 *
Sangho Shin7330c032014-10-20 10:34:51 -07001286 * @param srcSw source switch
1287 * @param nodeId destination node Id
1288 * @return list of switch DPID to forward packets to
Sangho Shin15273b62014-10-16 22:22:05 -07001289 */
Sangho Shin7330c032014-10-20 10:34:51 -07001290 private List<Dpid> getForwardingSwitchForNodeId(Switch srcSw, String nodeId) {
Sangho Shin15273b62014-10-16 22:22:05 -07001291
Sangho Shin7330c032014-10-20 10:34:51 -07001292 List<Dpid> fwdSws = new ArrayList<Dpid>();
1293 Switch destSw = null;
Sangho Shin15273b62014-10-16 22:22:05 -07001294
Sangho Shin7330c032014-10-20 10:34:51 -07001295 destSw = getSwitchFromNodeId(nodeId);
1296
1297 if (destSw == null) {
1298 log.debug("Cannot find the switch with ID {}", nodeId);
1299 return null;
1300 }
1301
1302 ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(srcSw);
1303
1304 HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>> switchVia =
1305 ecmpSPG.getAllLearnedSwitchesAndVia();
1306 for (Integer itrIdx : switchVia.keySet()) {
1307 HashMap<Switch, ArrayList<ArrayList<Dpid>>> swViaMap =
1308 switchVia.get(itrIdx);
1309 for (Switch targetSw : swViaMap.keySet()) {
1310 String destSwDpid = destSw.getDpid().toString();
1311 if (targetSw.getDpid().toString().equals(destSwDpid)) {
1312 for (ArrayList<Dpid> via : swViaMap.get(targetSw)) {
1313 if (via.isEmpty()) {
1314 fwdSws.add(destSw.getDpid());
1315 }
1316 else {
1317 fwdSws.add(via.get(0));
1318 }
1319 }
1320 }
1321 }
1322 }
1323
1324 return fwdSws;
Sangho Shin15273b62014-10-16 22:22:05 -07001325 }
1326
Sangho Shin7330c032014-10-20 10:34:51 -07001327 /**
1328 * Get switch for the node Id specified
1329 *
1330 * @param nodeId node ID for switch
1331 * @return Switch
1332 */
1333 private Switch getSwitchFromNodeId(String nodeId) {
Fahad Naeem Khan4444b952014-10-18 22:30:50 -07001334
Sangho Shin7330c032014-10-20 10:34:51 -07001335 for (Switch sw : mutableTopology.getSwitches()) {
1336 String id = sw.getStringAttribute("nodeSid");
1337 if (id.equals(nodeId)) {
1338 return sw;
1339 }
1340 }
1341
1342 return null;
1343 }
Fahad Naeem Khan4444b952014-10-18 22:30:50 -07001344
Sangho Shin43cee112014-09-25 16:43:34 -07001345 /**
Sangho Shin7330c032014-10-20 10:34:51 -07001346 * Convert a string DPID to its Switch Id (integer)
Sangho Shinfbc572c2014-10-02 16:37:05 -07001347 *
Sangho Shin7330c032014-10-20 10:34:51 -07001348 * @param dpid
1349 * @return
Sangho Shin43cee112014-09-25 16:43:34 -07001350 */
Sangho Shin7330c032014-10-20 10:34:51 -07001351 private long getSwId(String dpid) {
Sangho Shin43cee112014-09-25 16:43:34 -07001352
Sangho Shin7330c032014-10-20 10:34:51 -07001353 long swId = 0;
Sangho Shin43cee112014-09-25 16:43:34 -07001354
Sangho Shin7330c032014-10-20 10:34:51 -07001355 String swIdHexStr = "0x"+dpid.substring(dpid.lastIndexOf(":") + 1);
1356 if (swIdHexStr != null)
1357 swId = Integer.decode(swIdHexStr);
Sangho Shin43cee112014-09-25 16:43:34 -07001358
Sangho Shin7330c032014-10-20 10:34:51 -07001359 return swId;
1360 }
Sangho Shin43cee112014-09-25 16:43:34 -07001361
Sangho Shin7330c032014-10-20 10:34:51 -07001362 /**
1363 * Check if the switch is the edge router or not.
1364 *
1365 * @param dpid Dpid of the switch to check
1366 * @return true if it is an edge router, otherwise false
1367 */
1368 private boolean IsEdgeRouter(String dpid) {
Sangho Shin0df01982014-09-25 17:11:18 -07001369
Sangho Shin7330c032014-10-20 10:34:51 -07001370 for (Switch sw : mutableTopology.getSwitches()) {
1371 String dpidStr = sw.getDpid().toString();
1372 if (dpid.equals(dpidStr)) {
1373 /*
1374 String subnetInfo = sw.getStringAttribute("subnets");
1375 if (subnetInfo == null || subnetInfo.equals("[]")) {
1376 return false;
1377 }
1378 else
1379 return true;
1380 */
1381 String isEdge = sw.getStringAttribute("isEdgeRouter");
1382 if (isEdge != null) {
1383 if (isEdge.equals("true"))
1384 return true;
1385 else
1386 return false;
1387 }
Sangho Shin43cee112014-09-25 16:43:34 -07001388 }
1389 }
1390
Sangho Shin7330c032014-10-20 10:34:51 -07001391 return false;
Sangho Shineb083032014-09-22 16:11:34 -07001392 }
1393
1394 /**
1395 * Get MPLS label reading the config file
Sangho Shinfbc572c2014-10-02 16:37:05 -07001396 *
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -07001397 * @param dipid DPID of the switch
Sangho Shineb083032014-09-22 16:11:34 -07001398 * @return MPLS label for the switch
1399 */
Sangho Shin43cee112014-09-25 16:43:34 -07001400 private String getMplsLabel(String dpid) {
Sangho Shineb083032014-09-22 16:11:34 -07001401
1402 String mplsLabel = null;
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -07001403 for (Switch sw : mutableTopology.getSwitches()) {
Sangho Shin43cee112014-09-25 16:43:34 -07001404 String dpidStr = sw.getDpid().toString();
1405 if (dpid.equals(dpidStr)) {
Sangho Shineb083032014-09-22 16:11:34 -07001406 mplsLabel = sw.getStringAttribute("nodeSid");
1407 break;
Sangho Shin1aa93542014-09-22 09:49:44 -07001408 }
1409 }
1410
Sangho Shineb083032014-09-22 16:11:34 -07001411 return mplsLabel;
Sangho Shin1aa93542014-09-22 09:49:44 -07001412 }
1413
Sangho Shineb083032014-09-22 16:11:34 -07001414 /**
Sangho Shin1aa93542014-09-22 09:49:44 -07001415 * The function checks if given IP matches to the given subnet mask
Sangho Shinfbc572c2014-10-02 16:37:05 -07001416 *
Sangho Shin1aa93542014-09-22 09:49:44 -07001417 * @param addr - subnet address to match
1418 * @param addr1 - IP address to check
1419 * @return true if the IP address matches to the subnet, otherwise false
1420 */
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -07001421 public boolean netMatch(String addr, String addr1) { // addr is subnet
1422 // address and addr1 is
1423 // ip address. Function
1424 // will return true, if
1425 // addr1 is within
1426 // addr(subnet)
Sangho Shin1aa93542014-09-22 09:49:44 -07001427
1428 String[] parts = addr.split("/");
1429 String ip = parts[0];
1430 int prefix;
1431
1432 if (parts.length < 2) {
1433 prefix = 0;
1434 } else {
1435 prefix = Integer.parseInt(parts[1]);
1436 }
1437
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -07001438 Inet4Address a = null;
1439 Inet4Address a1 = null;
Sangho Shin1aa93542014-09-22 09:49:44 -07001440 try {
1441 a = (Inet4Address) InetAddress.getByName(ip);
1442 a1 = (Inet4Address) InetAddress.getByName(addr1);
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -07001443 } catch (UnknownHostException e) {
1444 }
Sangho Shin1aa93542014-09-22 09:49:44 -07001445
1446 byte[] b = a.getAddress();
1447 int ipInt = ((b[0] & 0xFF) << 24) |
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -07001448 ((b[1] & 0xFF) << 16) |
1449 ((b[2] & 0xFF) << 8) |
1450 ((b[3] & 0xFF) << 0);
Sangho Shin1aa93542014-09-22 09:49:44 -07001451
1452 byte[] b1 = a1.getAddress();
1453 int ipInt1 = ((b1[0] & 0xFF) << 24) |
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -07001454 ((b1[1] & 0xFF) << 16) |
1455 ((b1[2] & 0xFF) << 8) |
1456 ((b1[3] & 0xFF) << 0);
Sangho Shin1aa93542014-09-22 09:49:44 -07001457
1458 int mask = ~((1 << (32 - prefix)) - 1);
1459
1460 if ((ipInt & mask) == (ipInt1 & mask)) {
1461 return true;
1462 }
1463 else {
1464 return false;
1465 }
1466 }
Sangho Shineb083032014-09-22 16:11:34 -07001467
Sangho Shinac5ee2b2014-09-28 21:27:20 -07001468 /**
1469 * Add a routing rule for the host
Sangho Shinfbc572c2014-10-02 16:37:05 -07001470 *
Sangho Shinac5ee2b2014-09-28 21:27:20 -07001471 * @param sw - Switch to add the rule
1472 * @param hostIpAddress Destination host IP address
1473 * @param hostMacAddress Destination host MAC address
1474 */
Sangho Shineb083032014-09-22 16:11:34 -07001475 public void addRouteToHost(Switch sw, int hostIpAddress, byte[] hostMacAddress) {
1476 ipHandler.addRouteToHost(sw, hostIpAddress, hostMacAddress);
Sangho Shineb083032014-09-22 16:11:34 -07001477 }
Sangho Shin9c0f4c32014-09-26 16:02:38 -07001478
Sangho Shin463bee52014-09-29 15:14:43 -07001479 /**
1480 * Add IP packet to a buffer queue
Sangho Shinfbc572c2014-10-02 16:37:05 -07001481 *
Sangho Shin463bee52014-09-29 15:14:43 -07001482 * @param ipv4
1483 */
Sangho Shin7330c032014-10-20 10:34:51 -07001484 public void addPacketToPacketBuffer(IPv4 ipv4) {
Sangho Shin61535402014-10-01 11:37:14 -07001485 ipPacketQueue.add(ipv4);
Sangho Shin463bee52014-09-29 15:14:43 -07001486 }
1487
1488 /**
1489 * Retrieve all packets whose destination is the given address.
Sangho Shinfbc572c2014-10-02 16:37:05 -07001490 *
Sangho Shin463bee52014-09-29 15:14:43 -07001491 * @param destIp Destination address of packets to retrieve
1492 */
1493 public List<IPv4> getIpPacketFromQueue(byte[] destIp) {
1494
1495 List<IPv4> bufferedPackets = new ArrayList<IPv4>();
1496
Sangho Shin61535402014-10-01 11:37:14 -07001497 if (!ipPacketQueue.isEmpty()) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -07001498 for (IPv4 ip : ipPacketQueue) {
Sangho Shin61535402014-10-01 11:37:14 -07001499 int dest = ip.getDestinationAddress();
1500 IPv4Address ip1 = IPv4Address.of(dest);
1501 IPv4Address ip2 = IPv4Address.of(destIp);
1502 if (ip1.equals(ip2)) {
Srikanth Vavilapallif25c7b02014-10-01 14:30:43 -07001503 bufferedPackets.add((IPv4) (ipPacketQueue.poll()).clone());
Sangho Shin463bee52014-09-29 15:14:43 -07001504 }
1505 }
1506 }
1507
1508 return bufferedPackets;
1509 }
1510
Sangho Shin7330c032014-10-20 10:34:51 -07001511 /**
1512 * Get MAC address to known hosts
1513 *
1514 * @param destinationAddress IP address to get MAC address
1515 * @return MAC Address to given IP address
1516 */
1517 public byte[] getMacAddressFromIpAddress(int destinationAddress) {
1518
1519 // Can't we get the host IP address from the TopologyService ??
1520
1521 Iterator<ArpEntry> iterator = arpEntries.iterator();
1522
1523 IPv4Address ipAddress = IPv4Address.of(destinationAddress);
1524 byte[] ipAddressInByte = ipAddress.getBytes();
1525
1526 while (iterator.hasNext()) {
1527 ArpEntry arpEntry = iterator.next();
1528 byte[] address = arpEntry.targetIpAddress;
1529
1530 IPv4Address a = IPv4Address.of(address);
1531 IPv4Address b = IPv4Address.of(ipAddressInByte);
1532
1533 if (a.equals(b)) {
1534 log.debug("Found an arp entry");
1535 return arpEntry.targetMacAddress;
1536 }
1537 }
1538
1539 return null;
1540 }
1541
1542 /**
1543 * Send an ARP request via ArpHandler
1544 *
1545 * @param destinationAddress
1546 * @param sw
1547 * @param inPort
1548 *
1549 */
1550 public void sendArpRequest(Switch sw, int destinationAddress, Port inPort) {
1551 arpHandler.sendArpRequest(sw, destinationAddress, inPort);
1552 }
1553
1554
1555 // ************************************
1556 // Test functions
1557 // ************************************
1558
Sangho Shin55d00e12014-10-20 12:13:07 -07001559 /*
Sangho Shin7330c032014-10-20 10:34:51 -07001560 private void runTest() {
1561
Sangho Shin5b8f5452014-10-20 11:46:01 -07001562 if (testMode == POLICY_ADD1) {
1563 String[] routeArray = {"101", "105", "110"};
1564 List<String> routeList = new ArrayList<String>();
1565 for (int i = 0; i < routeArray.length; i++)
1566 routeList.add(routeArray[i]);
Sangho Shin7330c032014-10-20 10:34:51 -07001567
Sangho Shin5b8f5452014-10-20 11:46:01 -07001568 if (createTunnel(1, routeList)) {
1569 IPv4Net srcIp = new IPv4Net("10.0.1.1/24");
1570 IPv4Net dstIp = new IPv4Net("10.1.2.1/24");
1571
1572 log.debug("Set the policy 1");
1573 this.setPolicyTable(1, null, null, Ethernet.TYPE_IPV4, srcIp,
1574 dstIp, IPv4.PROTOCOL_ICMP, (short)-1, (short)-1, 10000,
1575 1);
1576 testMode = POLICY_ADD2;
1577 testTask.reschedule(10, TimeUnit.SECONDS);
1578 }
1579 else {
1580 // retry it
1581 testTask.reschedule(5, TimeUnit.SECONDS);
1582 }
Sangho Shin7330c032014-10-20 10:34:51 -07001583 }
Sangho Shin5b8f5452014-10-20 11:46:01 -07001584 else if (testMode == POLICY_ADD2) {
1585 String[] routeArray = {"101", "102", "103", "104", "105", "108",
1586 "110"};
1587 List<String> routeList = new ArrayList<String>();
1588 for (int i = 0; i < routeArray.length; i++)
1589 routeList.add(routeArray[i]);
1590
1591 if (createTunnel(2, routeList)) {
1592 IPv4Net srcIp = new IPv4Net("10.0.1.1/24");
1593 IPv4Net dstIp = new IPv4Net("10.1.2.1/24");
1594
1595 log.debug("Set the policy 2");
1596 this.setPolicyTable(2, null, null, Ethernet.TYPE_IPV4, srcIp,
1597 dstIp, IPv4.PROTOCOL_ICMP, (short)-1, (short)-1, 20000,
1598 2);
1599 testMode = POLICY_REMOVE2;
1600 testTask.reschedule(10, TimeUnit.SECONDS);
1601 }
1602 else {
1603 log.debug("Retry it");
1604 testTask.reschedule(5, TimeUnit.SECONDS);
1605 }
Sangho Shin7330c032014-10-20 10:34:51 -07001606 }
Sangho Shin5b8f5452014-10-20 11:46:01 -07001607 else if (testMode == POLICY_REMOVE2){
1608 log.debug("Remove the policy 2");
1609 this.removePolicy(2);
1610 testMode = POLICY_REMOVE1;
1611 testTask.reschedule(10, TimeUnit.SECONDS);
1612 }
1613 else if (testMode == POLICY_REMOVE1){
1614 log.debug("Remove the policy 1");
1615 this.removePolicy(1);
1616 }
1617
Sangho Shin7330c032014-10-20 10:34:51 -07001618 }
Sangho Shin55d00e12014-10-20 12:13:07 -07001619 */
1620
1621 private void runTest() {
1622
1623 if (testMode == POLICY_ADD1) {
1624 String[] routeArray = {"101", "105", "110"};
1625 List<Dpid> routeList = new ArrayList<Dpid>();
1626 for (int i = 0; i < routeArray.length; i++) {
1627 Dpid dpid = getSwitchFromNodeId(routeArray[i]).getDpid();
1628 routeList.add(dpid);
1629 }
1630
1631 if (createTunnel("1", routeList)) {
1632 IPv4Net srcIp = new IPv4Net("10.0.1.1/24");
1633 IPv4Net dstIp = new IPv4Net("10.1.2.1/24");
1634
1635 log.debug("Set the policy 1");
Sangho Shine020cc32014-10-20 13:28:02 -07001636 this.createPolicy("1", null, null, Ethernet.TYPE_IPV4, srcIp,
Sangho Shin55d00e12014-10-20 12:13:07 -07001637 dstIp, IPv4.PROTOCOL_ICMP, (short)-1, (short)-1, 10000,
Sangho Shine020cc32014-10-20 13:28:02 -07001638 "1");
Sangho Shin55d00e12014-10-20 12:13:07 -07001639 testMode = POLICY_ADD2;
1640 testTask.reschedule(10, TimeUnit.SECONDS);
1641 }
1642 else {
1643 // retry it
1644 testTask.reschedule(5, TimeUnit.SECONDS);
1645 }
1646 }
1647 else if (testMode == POLICY_ADD2) {
1648 String[] routeArray = {"101", "102", "103", "104", "105", "108",
1649 "110"};
1650 List<Dpid> routeList = new ArrayList<Dpid>();
1651 for (int i = 0; i < routeArray.length; i++) {
1652 Dpid dpid = getSwitchFromNodeId(routeArray[i]).getDpid();
1653 routeList.add(dpid);
1654 }
1655
1656 if (createTunnel("2", routeList)) {
1657 IPv4Net srcIp = new IPv4Net("10.0.1.1/24");
1658 IPv4Net dstIp = new IPv4Net("10.1.2.1/24");
1659
1660 log.debug("Set the policy 2");
Sangho Shine020cc32014-10-20 13:28:02 -07001661 this.createPolicy("2", null, null, Ethernet.TYPE_IPV4, srcIp,
Sangho Shin55d00e12014-10-20 12:13:07 -07001662 dstIp, IPv4.PROTOCOL_ICMP, (short)-1, (short)-1, 20000,
Sangho Shine020cc32014-10-20 13:28:02 -07001663 "2");
Sangho Shin55d00e12014-10-20 12:13:07 -07001664 testMode = POLICY_REMOVE2;
1665 testTask.reschedule(10, TimeUnit.SECONDS);
1666 }
1667 else {
1668 log.debug("Retry it");
1669 testTask.reschedule(5, TimeUnit.SECONDS);
1670 }
1671 }
1672 else if (testMode == POLICY_REMOVE2){
1673 log.debug("Remove the policy 2");
Sangho Shine020cc32014-10-20 13:28:02 -07001674 this.removePolicy("2");
Sangho Shin55d00e12014-10-20 12:13:07 -07001675 testMode = POLICY_REMOVE1;
1676 testTask.reschedule(10, TimeUnit.SECONDS);
1677 }
1678 else if (testMode == POLICY_REMOVE1){
1679 log.debug("Remove the policy 1");
Sangho Shine020cc32014-10-20 13:28:02 -07001680 this.removePolicy("1");
Sangho Shin55d00e12014-10-20 12:13:07 -07001681 }
1682
1683 }
Sangho Shin7330c032014-10-20 10:34:51 -07001684
1685 private void runTest1() {
1686
1687 String dpid1 = "00:00:00:00:00:00:00:01";
1688 String dpid2 = "00:00:00:00:00:00:00:0a";
1689 Switch srcSw = mutableTopology.getSwitch(new Dpid(dpid1));
1690 Switch dstSw = mutableTopology.getSwitch(new Dpid(dpid2));
1691
1692 if (srcSw == null || dstSw == null) {
1693 testTask.reschedule(1, TimeUnit.SECONDS);
1694 log.debug("Switch is gone. Reschedule the test");
1695 return;
1696 }
1697
1698 String[] routeArray = {"101", "102", "105", "108", "110"};
1699 List<String> routeList = new ArrayList<String>();
1700 for (int i = 0; i < routeArray.length; i++)
1701 routeList.add(routeArray[i]);
1702
1703 List<String> optimizedRoute = this.getOptimizedPath(srcSw, dstSw, routeList);
1704
1705 log.debug("Test set is {}", routeList.toString());
1706 log.debug("Result set is {}", optimizedRoute.toString());
1707
1708
1709 }
1710
1711 /**
1712 * print tunnel info - used only for debugging.
1713 * @param targetSw
1714 *
1715 * @param fwdSwDpids
1716 * @param ids
1717 * @param tunnelId
1718 */
Sangho Shine020cc32014-10-20 13:28:02 -07001719 private void printTunnelInfo(IOF13Switch targetSw, String tunnelId,
Sangho Shin7330c032014-10-20 10:34:51 -07001720 List<String> ids, NeighborSet ns) {
1721 StringBuilder logStr = new StringBuilder("In switch " +
1722 targetSw.getId() + ", create a tunnel " + tunnelId + " " + " of push ");
1723 for (String id: ids)
1724 logStr.append(id + "-");
1725 logStr.append(" output to ");
1726 for (Dpid dpid: ns.getDpids())
1727 logStr.append(dpid + " - ");
1728
1729 log.debug(logStr.toString());
1730
1731 }
1732
1733 /**
1734 * Debugging function to print out the Match Action Entry
1735 * @param sw13
1736 *
1737 * @param maEntry
1738 */
1739 private void printMatchActionOperationEntry(
1740 IOF13Switch sw13, MatchActionOperationEntry maEntry) {
1741
1742 StringBuilder logStr = new StringBuilder("In switch " + sw13.getId() + ", ");
1743
1744 MatchAction ma = maEntry.getTarget();
1745 Match m = ma.getMatch();
1746 List<Action> actions = ma.getActions();
1747
1748 if (m instanceof Ipv4Match) {
1749 logStr.append("If the IP matches with ");
1750 IPv4Net ip = ((Ipv4Match) m).getDestination();
1751 logStr.append(ip.toString());
1752 logStr.append(" then ");
1753 }
1754 else if (m instanceof MplsMatch) {
1755 logStr.append("If the MPLS label matches with ");
1756 int mplsLabel = ((MplsMatch) m).getMplsLabel();
1757 logStr.append(mplsLabel);
1758 logStr.append(" then ");
1759 }
1760 else if (m instanceof PacketMatch) {
1761 GroupAction ga = (GroupAction)actions.get(0);
1762 logStr.append("if the policy match is XXX then go to group " +
1763 ga.getGroupId());
1764 log.debug(logStr.toString());
1765 return;
1766 }
1767
1768 logStr.append(" do { ");
1769 for (Action action : actions) {
1770 if (action instanceof CopyTtlInAction) {
1771 logStr.append("copy ttl In, ");
1772 }
1773 else if (action instanceof CopyTtlOutAction) {
1774 logStr.append("copy ttl Out, ");
1775 }
1776 else if (action instanceof DecMplsTtlAction) {
1777 logStr.append("Dec MPLS TTL , ");
1778 }
1779 else if (action instanceof GroupAction) {
1780 logStr.append("Forward packet to < ");
1781 NeighborSet dpids = ((GroupAction) action).getDpids();
1782 logStr.append(dpids.toString() + ",");
1783
1784 }
1785 else if (action instanceof PopMplsAction) {
1786 logStr.append("Pop MPLS label, ");
1787 }
1788 else if (action instanceof PushMplsAction) {
1789 logStr.append("Push MPLS label, ");
1790 }
1791 else if (action instanceof SetMplsIdAction) {
1792 int id = ((SetMplsIdAction) action).getMplsId();
1793 logStr.append("Set MPLS ID as " + id + ", ");
1794 }
1795 }
1796
1797 log.debug(logStr.toString());
1798
1799 }
1800
1801
1802 // ************************************
1803 // Unused classes and functions
1804 // ************************************
1805
1806 /**
1807 * Temporary class to to keep ARP entry
1808 *
1809 */
1810 private class ArpEntry {
1811
1812 byte[] targetMacAddress;
1813 byte[] targetIpAddress;
1814
1815 private ArpEntry(byte[] macAddress, byte[] ipAddress) {
1816 this.targetMacAddress = macAddress;
1817 this.targetIpAddress = ipAddress;
1818 }
1819 }
1820
1821 /**
1822 * This class is used only for link recovery optimization in
1823 * modifyEcmpRoutingRules() function.
1824 * TODO: please remove if the optimization is not used at all
1825 */
1826 private class SwitchPair {
1827 private Switch src;
1828 private Switch dst;
1829
1830 public SwitchPair(Switch src, Switch dst) {
1831 this.src = src;
1832 this.dst = dst;
1833 }
1834
1835 public Switch getSource() {
1836 return src;
1837 }
1838
1839 public Switch getDestination() {
1840 return dst;
1841 }
1842 }
1843
1844 /**
1845 * Update ARP Cache using ARP packets It is used to set destination MAC
1846 * address to forward packets to known hosts. But, it will be replace with
1847 * Host information of Topology service later.
1848 *
1849 * @param arp APR packets to use for updating ARP entries
1850 */
1851 public void updateArpCache(ARP arp) {
1852
1853 ArpEntry arpEntry = new ArpEntry(arp.getSenderHardwareAddress(),
1854 arp.getSenderProtocolAddress());
1855 // TODO: Need to check the duplication
1856 arpEntries.add(arpEntry);
1857 }
1858
1859 /**
1860 * Modify the routing rules for the lost links
1861 * - Recompute the path if the link failed is included in the path
1862 * (including src and dest).
1863 *
1864 * @param newLink
1865 */
1866 private void modifyEcmpRoutingRules(LinkData linkRemoved) {
1867
1868 //HashMap<Switch, SwitchPair> linksToRecompute = new HashMap<Switch, SwitchPair>();
1869 Set<SwitchPair> linksToRecompute = new HashSet<SwitchPair>();
1870
1871 for (ECMPShortestPathGraph ecmpSPG : graphs.values()) {
1872 Switch rootSw = ecmpSPG.getRootSwitch();
1873 HashMap<Integer, HashMap<Switch, ArrayList<Path>>> paths =
1874 ecmpSPG.getCompleteLearnedSwitchesAndPaths();
1875 for (HashMap<Switch, ArrayList<Path>> p: paths.values()) {
1876 for (Switch destSw: p.keySet()) {
1877 ArrayList<Path> path = p.get(destSw);
1878 if (checkPath(path, linkRemoved)) {
1879 boolean found = false;
1880 for (SwitchPair pair: linksToRecompute) {
1881 if (pair.getSource().getDpid() == rootSw.getDpid() &&
1882 pair.getSource().getDpid() == destSw.getDpid()) {
1883 found = true;
1884 }
1885 }
1886 if (!found) {
1887 linksToRecompute.add(new SwitchPair(rootSw, destSw));
1888 }
1889 }
1890 }
1891 }
1892 }
1893
1894 // Recompute the path for the specific route
1895 for (SwitchPair pair: linksToRecompute) {
1896
1897 log.debug("Recompute path from {} to {}", pair.getSource(), pair.getDestination());
1898 // We need the following function for optimization
1899 //ECMPShortestPathGraph ecmpSPG =
1900 // new ECMPShortestPathGraph(pair.getSource(), pair.getDestination());
1901 ECMPShortestPathGraph ecmpSPG =
1902 new ECMPShortestPathGraph(pair.getSource());
1903 populateEcmpRoutingRulesForPath(pair.getSource(), ecmpSPG, true);
1904 }
1905 }
1906
1907 /**
1908 * Optimize the mpls label
1909 * The feature will be used only for policy of "avoid a specific switch".
1910 * Check route to each router in route backward.
1911 * If there is only one route to the router and the routers are included in
1912 * the route, remove the id from the path.
1913 * A-B-C-D-E => A-B-C-D-E -> A-E
1914 * | | => A-B-H-I -> A-I
Sangho Shin5b8f5452014-10-20 11:46:01 -07001915 * F-G-H-I => A-D-I > A-D-I
Sangho Shin7330c032014-10-20 10:34:51 -07001916 */
1917 private List<String> getOptimizedPath(Switch srcSw, Switch dstSw, List<String> route) {
1918
1919 List<String> optimizedPath = new ArrayList<String>();
1920 optimizedPath.addAll(route);
1921 ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(srcSw);
1922
1923 HashMap<Integer, HashMap<Switch, ArrayList<Path>>> paths =
1924 ecmpSPG.getCompleteLearnedSwitchesAndPaths();
1925 for (HashMap<Switch, ArrayList<Path>> p: paths.values()) {
1926 for (Switch s: p.keySet()) {
1927 if (s.getDpid().toString().equals(dstSw.getDpid().toString())) {
1928 ArrayList<Path> ecmpPaths = p.get(s);
1929 if (ecmpPaths!= null && ecmpPaths.size() == 1) {
1930 for (Path path: ecmpPaths) {
1931 for (LinkData link: path) {
1932 String srcId = getMplsLabel(link.getSrc().getDpid().toString());
1933 String dstId = getMplsLabel(link.getSrc().getDpid().toString());
1934 if (optimizedPath.contains(srcId)) {
1935 optimizedPath.remove(srcId);
1936 }
1937 if (optimizedPath.contains(dstId)) {
1938 optimizedPath.remove(dstId);
1939 }
1940 }
1941 }
1942 }
1943 }
1944 }
1945 }
1946
1947 return optimizedPath;
1948
1949 }
1950
1951 /**
1952 * Check if the path is affected from the link removed
1953 *
1954 * @param path Path to check
1955 * @param linkRemoved link removed
1956 * @return true if the path contains the link removed
1957 */
1958 private boolean checkPath(ArrayList<Path> path, LinkData linkRemoved) {
1959
1960 for (Path ppp: path) {
1961 // TODO: need to check if this is a bidirectional or
1962 // unidirectional
1963 for (LinkData link: ppp) {
1964 if (link.getDst().getDpid().equals(linkRemoved.getDst().getDpid()) &&
1965 link.getSrc().getDpid().equals(linkRemoved.getSrc().getDpid()))
1966 return true;
1967 }
1968 }
1969
1970 return false;
1971 }
Sangho Shin15273b62014-10-16 22:22:05 -07001972
1973
Sangho Shin2f263692014-09-15 14:09:41 -07001974}