Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.segmentrouting; |
| 2 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 3 | import java.io.IOException; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 4 | import java.net.Inet4Address; |
| 5 | import java.net.InetAddress; |
| 6 | import java.net.UnknownHostException; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 7 | import java.util.ArrayList; |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 8 | import java.util.Arrays; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 9 | import java.util.Collection; |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 10 | import java.util.HashMap; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 11 | import java.util.HashSet; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 12 | import java.util.Iterator; |
| 13 | import java.util.List; |
| 14 | import java.util.Map; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 15 | import java.util.Set; |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 16 | import java.util.concurrent.ConcurrentLinkedQueue; |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 17 | import java.util.concurrent.ExecutionException; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 18 | import java.util.concurrent.ScheduledExecutorService; |
| 19 | import java.util.concurrent.TimeUnit; |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 20 | import java.util.concurrent.TimeoutException; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 21 | |
| 22 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 23 | import net.floodlightcontroller.core.IOF13Switch; |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 24 | import net.floodlightcontroller.core.IOF13Switch.NeighborSet; |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 25 | import net.floodlightcontroller.core.internal.OFBarrierReplyFuture; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 26 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 27 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 28 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 29 | import net.floodlightcontroller.core.module.IFloodlightService; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 30 | import net.floodlightcontroller.core.util.SingletonTask; |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 31 | import net.floodlightcontroller.restserver.IRestApiService; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 32 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 33 | import net.floodlightcontroller.util.MACAddress; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 34 | import net.onrc.onos.api.packet.IPacketListener; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 35 | import net.onrc.onos.api.packet.IPacketService; |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 36 | import net.onrc.onos.apps.segmentrouting.web.SegmentRoutingWebRoutable; |
Srikanth Vavilapalli | 8a661e7 | 2014-10-27 15:40:22 -0700 | [diff] [blame] | 37 | import net.onrc.onos.core.drivermanager.OFSwitchImplDellOSR; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 38 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 39 | import net.onrc.onos.core.intent.Path; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 40 | import net.onrc.onos.core.main.config.IConfigInfoService; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 41 | import net.onrc.onos.core.matchaction.MatchAction; |
| 42 | import net.onrc.onos.core.matchaction.MatchActionId; |
| 43 | import net.onrc.onos.core.matchaction.MatchActionOperationEntry; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 44 | import net.onrc.onos.core.matchaction.MatchActionOperations.Operator; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 45 | import net.onrc.onos.core.matchaction.action.Action; |
| 46 | import net.onrc.onos.core.matchaction.action.CopyTtlInAction; |
| 47 | import net.onrc.onos.core.matchaction.action.CopyTtlOutAction; |
| 48 | import net.onrc.onos.core.matchaction.action.DecMplsTtlAction; |
| 49 | import net.onrc.onos.core.matchaction.action.DecNwTtlAction; |
| 50 | import net.onrc.onos.core.matchaction.action.GroupAction; |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 51 | import net.onrc.onos.core.matchaction.action.ModifyDstMacAction; |
| 52 | import net.onrc.onos.core.matchaction.action.ModifySrcMacAction; |
| 53 | import net.onrc.onos.core.matchaction.action.OutputAction; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 54 | import net.onrc.onos.core.matchaction.action.PopMplsAction; |
| 55 | import net.onrc.onos.core.matchaction.action.PushMplsAction; |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 56 | import net.onrc.onos.core.matchaction.action.SetDAAction; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 57 | import net.onrc.onos.core.matchaction.action.SetMplsIdAction; |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 58 | import net.onrc.onos.core.matchaction.action.SetSAAction; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 59 | import net.onrc.onos.core.matchaction.match.Ipv4Match; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 60 | import net.onrc.onos.core.matchaction.match.Match; |
| 61 | import net.onrc.onos.core.matchaction.match.MplsMatch; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 62 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
| 63 | import net.onrc.onos.core.matchaction.match.PacketMatchBuilder; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 64 | import net.onrc.onos.core.packet.ARP; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 65 | import net.onrc.onos.core.packet.Ethernet; |
| 66 | import net.onrc.onos.core.packet.IPv4; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 67 | import net.onrc.onos.core.topology.ITopologyListener; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 68 | import net.onrc.onos.core.topology.ITopologyService; |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 69 | import net.onrc.onos.core.topology.Link; |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 70 | import net.onrc.onos.core.topology.LinkData; |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 71 | import net.onrc.onos.core.topology.MastershipData; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 72 | import net.onrc.onos.core.topology.MutableTopology; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 73 | import net.onrc.onos.core.topology.Port; |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 74 | import net.onrc.onos.core.topology.PortData; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 75 | import net.onrc.onos.core.topology.Switch; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 76 | import net.onrc.onos.core.topology.SwitchData; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 77 | import net.onrc.onos.core.topology.TopologyEvents; |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 78 | import net.onrc.onos.core.util.Dpid; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 79 | import net.onrc.onos.core.util.IPv4Net; |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 80 | import net.onrc.onos.core.util.PortNumber; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 81 | import net.onrc.onos.core.util.SwitchPort; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 82 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 83 | import org.json.JSONArray; |
| 84 | import org.json.JSONException; |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 85 | import org.projectfloodlight.openflow.protocol.OFBarrierReply; |
Saurav Das | bc594a4 | 2014-09-25 20:13:50 -0700 | [diff] [blame] | 86 | import org.projectfloodlight.openflow.types.EthType; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 87 | import org.projectfloodlight.openflow.types.IPv4Address; |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 88 | import org.projectfloodlight.openflow.types.MacAddress; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 89 | import org.slf4j.Logger; |
| 90 | import org.slf4j.LoggerFactory; |
| 91 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 92 | public class SegmentRoutingManager implements IFloodlightModule, |
Srikanth Vavilapalli | 8c49db7 | 2014-10-20 13:35:08 -0700 | [diff] [blame] | 93 | ITopologyListener, IPacketListener, ISegmentRoutingService { |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 94 | |
| 95 | private static final Logger log = LoggerFactory |
| 96 | .getLogger(SegmentRoutingManager.class); |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 97 | |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 98 | private ITopologyService topologyService; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 99 | private IPacketService packetService; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 100 | private MutableTopology mutableTopology; |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 101 | private ConcurrentLinkedQueue<IPv4> ipPacketQueue; |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 102 | private IRestApiService restApi; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 103 | private List<ArpEntry> arpEntries; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 104 | private ArpHandler arpHandler; |
| 105 | private GenericIpHandler ipHandler; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 106 | private IcmpHandler icmpHandler; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 107 | private IThreadPoolService threadPool; |
| 108 | private SingletonTask discoveryTask; |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 109 | private SingletonTask linkAddTask; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 110 | private SingletonTask testTask; |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 111 | private IFloodlightProviderService floodlightProvider; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 112 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 113 | private HashMap<Switch, ECMPShortestPathGraph> graphs; |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 114 | private HashMap<String, LinkData> linksDown; |
| 115 | private HashMap<String, LinkData> linksToAdd; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 116 | private ConcurrentLinkedQueue<TopologyEvents> topologyEventQueue; |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 117 | private HashMap<String, PolicyInfo> policyTable; |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 118 | private HashMap<String, TunnelInfo> tunnelTable; |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 119 | private HashMap<Integer, HashMap<Integer, List<Integer>>> adjacencySidTable; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 120 | |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 121 | // Flag whether transit router supports ECMP or not |
Srikanth Vavilapalli | 8a661e7 | 2014-10-27 15:40:22 -0700 | [diff] [blame] | 122 | // private boolean supportTransitECMP = true; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 123 | |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 124 | private int testMode = 0; |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 125 | |
| 126 | private int numOfEvents = 0; |
| 127 | private int numOfEventProcess = 0; |
| 128 | private int numOfPopulation = 0; |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 129 | private long matchActionId = 0L; |
Sangho Shin | 5818267 | 2014-10-21 13:23:38 -0700 | [diff] [blame] | 130 | |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 131 | private final int DELAY_TO_ADD_LINK = 10; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 132 | private final int MAX_NUM_LABELS = 3; |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 133 | |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 134 | private final int POLICY_ADD1 = 1; |
| 135 | private final int POLICY_ADD2 = 2; |
| 136 | private final int POLICY_REMOVE1 = 3; |
| 137 | private final int POLICY_REMOVE2 = 4; |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 138 | private final int TUNNEL_REMOVE1 = 5; |
| 139 | private final int TUNNEL_REMOVE2 = 6; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 140 | |
| 141 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 142 | // ************************************ |
| 143 | // IFloodlightModule implementation |
| 144 | // ************************************ |
| 145 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 146 | @Override |
| 147 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Srikanth Vavilapalli | 8c49db7 | 2014-10-20 13:35:08 -0700 | [diff] [blame] | 148 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(); |
| 149 | l.add(ISegmentRoutingService.class); |
| 150 | return l; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Srikanth Vavilapalli | 8c49db7 | 2014-10-20 13:35:08 -0700 | [diff] [blame] | 155 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>(); |
| 156 | m.put(ISegmentRoutingService.class, this); |
| 157 | return m; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 162 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>(); |
| 163 | |
| 164 | l.add(IFloodlightProviderService.class); |
| 165 | l.add(IConfigInfoService.class); |
| 166 | l.add(ITopologyService.class); |
| 167 | l.add(IPacketService.class); |
| 168 | l.add(IFlowPusherService.class); |
| 169 | l.add(ITopologyService.class); |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 170 | l.add(IRestApiService.class); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 171 | |
| 172 | return l; |
| 173 | |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 178 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 179 | arpHandler = new ArpHandler(context, this); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 180 | icmpHandler = new IcmpHandler(context, this); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 181 | ipHandler = new GenericIpHandler(context, this); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 182 | arpEntries = new ArrayList<ArpEntry>(); |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 183 | topologyService = context.getServiceImpl(ITopologyService.class); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 184 | threadPool = context.getServiceImpl(IThreadPoolService.class); |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 185 | mutableTopology = topologyService.getTopology(); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 186 | ipPacketQueue = new ConcurrentLinkedQueue<IPv4>(); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 187 | graphs = new HashMap<Switch, ECMPShortestPathGraph>(); |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 188 | linksDown = new HashMap<String, LinkData>(); |
| 189 | linksToAdd = new HashMap<String, LinkData>(); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 190 | topologyEventQueue = new ConcurrentLinkedQueue<TopologyEvents>(); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 191 | packetService = context.getServiceImpl(IPacketService.class); |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 192 | restApi = context.getServiceImpl(IRestApiService.class); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 193 | policyTable = new HashMap<String, PolicyInfo>(); |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 194 | tunnelTable = new HashMap<String, TunnelInfo>(); |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 195 | adjacencySidTable = new HashMap<Integer,HashMap<Integer, List<Integer>>>(); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 196 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 197 | packetService.registerPacketListener(this); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 198 | topologyService.addListener(this, false); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 199 | |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 200 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void startUp(FloodlightModuleContext context) throws FloodlightModuleException { |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 205 | ScheduledExecutorService ses = threadPool.getScheduledExecutor(); |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 206 | restApi.addRestletRoutable(new SegmentRoutingWebRoutable()); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 207 | |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 208 | discoveryTask = new SingletonTask(ses, new Runnable() { |
| 209 | @Override |
| 210 | public void run() { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 211 | handleTopologyChangeEvents(); |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 212 | } |
| 213 | }); |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 214 | |
| 215 | linkAddTask = new SingletonTask(ses, new Runnable() { |
| 216 | @Override |
| 217 | public void run() { |
| 218 | delayedAddLink(); |
| 219 | } |
| 220 | }); |
| 221 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 222 | testTask = new SingletonTask(ses, new Runnable() { |
| 223 | @Override |
| 224 | public void run() { |
| 225 | runTest(); |
| 226 | } |
| 227 | }); |
| 228 | |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 229 | testMode = POLICY_ADD1; |
Sangho Shin | 69229fb | 2014-10-27 16:17:04 -0700 | [diff] [blame] | 230 | //testTask.reschedule(20, TimeUnit.SECONDS); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 233 | @Override |
| 234 | public void receive(Switch sw, Port inPort, Ethernet payload) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 235 | if (payload.getEtherType() == Ethernet.TYPE_ARP) |
| 236 | arpHandler.processPacketIn(sw, inPort, payload); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 237 | if (payload.getEtherType() == Ethernet.TYPE_IPV4) { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 238 | addPacketToPacketBuffer((IPv4) payload.getPayload()); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 239 | if (((IPv4) payload.getPayload()).getProtocol() == IPv4.PROTOCOL_ICMP) |
| 240 | icmpHandler.processPacketIn(sw, inPort, payload); |
| 241 | else |
| 242 | ipHandler.processPacketIn(sw, inPort, payload); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 243 | } |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 244 | else { |
| 245 | log.debug("{}", payload.toString()); |
| 246 | } |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 247 | } |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 248 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 249 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 250 | // ************************************ |
| 251 | // Topology event handlers |
| 252 | // ************************************ |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 253 | |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 254 | /** |
| 255 | * Topology events that have been generated. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 256 | * |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 257 | * @param topologyEvents the generated Topology Events |
| 258 | * @see TopologyEvents |
| 259 | */ |
| 260 | public void topologyEvents(TopologyEvents topologyEvents) |
| 261 | { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 262 | topologyEventQueue.add(topologyEvents); |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 263 | discoveryTask.reschedule(100, TimeUnit.MILLISECONDS); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 264 | } |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 265 | |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 266 | /** |
| 267 | * Process the multiple topology events with some delay (100MS at most for now) |
| 268 | * |
| 269 | */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 270 | private void handleTopologyChangeEvents() { |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 271 | numOfEventProcess ++; |
| 272 | |
Sangho Shin | 5162534 | 2014-10-17 09:30:48 -0700 | [diff] [blame] | 273 | Collection<LinkData> linkEntriesAddedAll = new ArrayList<LinkData>(); |
| 274 | Collection<PortData> portEntriesAddedAll = new ArrayList<PortData>(); |
| 275 | Collection<PortData> portEntriesRemovedAll = new ArrayList<PortData>(); |
| 276 | Collection<LinkData> linkEntriesRemovedAll = new ArrayList<LinkData>(); |
| 277 | Collection<SwitchData> switchAddedAll = new ArrayList<SwitchData>(); |
| 278 | Collection<SwitchData> switchRemovedAll = new ArrayList<SwitchData>(); |
| 279 | Collection<MastershipData> mastershipRemovedAll = new ArrayList<MastershipData>(); |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 280 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 281 | while (!topologyEventQueue.isEmpty()) { |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 282 | // We should handle the events in the order of when they happen |
| 283 | // TODO: We need to simulate the final results of multiple events |
| 284 | // and shoot only the final state. |
| 285 | // Ex: link s1-s2 down, link s1-s2 up --> Do nothing |
| 286 | // Ex: ink s1-s2 up, s1-p1,p2 down --> link s1-s2 down |
Sangho Shin | 5162534 | 2014-10-17 09:30:48 -0700 | [diff] [blame] | 287 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 288 | TopologyEvents topologyEvents = topologyEventQueue.poll(); |
Sangho Shin | 5162534 | 2014-10-17 09:30:48 -0700 | [diff] [blame] | 289 | |
| 290 | Collection<LinkData> linkEntriesAdded = topologyEvents.getAddedLinkDataEntries(); |
| 291 | Collection<PortData> portEntriesAdded = topologyEvents.getAddedPortDataEntries(); |
| 292 | Collection<PortData> portEntriesRemoved = topologyEvents.getRemovedPortDataEntries(); |
| 293 | Collection<LinkData> linkEntriesRemoved = topologyEvents.getRemovedLinkDataEntries(); |
| 294 | Collection<SwitchData> switchAdded = topologyEvents.getAddedSwitchDataEntries(); |
| 295 | Collection<SwitchData> switchRemoved = topologyEvents.getRemovedSwitchDataEntries(); |
| 296 | Collection<MastershipData> mastershipRemoved = topologyEvents.getRemovedMastershipDataEntries(); |
| 297 | |
| 298 | linkEntriesAddedAll.addAll(linkEntriesAdded); |
| 299 | portEntriesAddedAll.addAll(portEntriesAdded); |
| 300 | portEntriesRemovedAll.addAll(portEntriesRemoved); |
| 301 | linkEntriesRemovedAll.addAll(linkEntriesRemoved); |
| 302 | switchAddedAll.addAll(switchAdded); |
| 303 | switchRemovedAll.addAll(switchRemoved); |
| 304 | mastershipRemovedAll.addAll(mastershipRemoved); |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 305 | numOfEvents++; |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 306 | |
| 307 | if (!portEntriesRemoved.isEmpty()) { |
| 308 | processPortRemoval(portEntriesRemoved); |
| 309 | } |
| 310 | |
| 311 | if (!linkEntriesRemoved.isEmpty()) { |
| 312 | processLinkRemoval(linkEntriesRemoved); |
| 313 | } |
| 314 | |
| 315 | if (!switchRemoved.isEmpty()) { |
| 316 | processSwitchRemoved(switchRemoved); |
| 317 | } |
| 318 | |
| 319 | if (!mastershipRemoved.isEmpty()) { |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 320 | log.debug("Mastership is removed. Check if ports are down also."); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if (!linkEntriesAdded.isEmpty()) { |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 324 | processLinkAdd(linkEntriesAdded, false); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (!portEntriesAdded.isEmpty()) { |
| 328 | processPortAdd(portEntriesAdded); |
| 329 | } |
| 330 | |
| 331 | if (!switchAdded.isEmpty()) { |
| 332 | processSwitchAdd(switchAdded); |
| 333 | } |
Sangho Shin | 5162534 | 2014-10-17 09:30:48 -0700 | [diff] [blame] | 334 | |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 337 | // TODO: 100ms is enough to check both mastership removed events |
| 338 | // and the port removed events? What if the PORT_STATUS packets comes late? |
Sangho Shin | 5162534 | 2014-10-17 09:30:48 -0700 | [diff] [blame] | 339 | if (!mastershipRemovedAll.isEmpty()) { |
| 340 | if (portEntriesRemovedAll.isEmpty()) { |
Saurav Das | 82e6297 | 2014-10-16 14:53:57 -0700 | [diff] [blame] | 341 | log.debug("Just mastership is removed. Do not do anthing."); |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 342 | } |
| 343 | else { |
| 344 | HashMap<String, MastershipData> mastershipToRemove = |
| 345 | new HashMap<String, MastershipData>(); |
Sangho Shin | 5162534 | 2014-10-17 09:30:48 -0700 | [diff] [blame] | 346 | for (MastershipData ms: mastershipRemovedAll) { |
| 347 | for (PortData port: portEntriesRemovedAll) { |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 348 | // TODO: check ALL ports of the switch are dead .. |
| 349 | if (port.getDpid().equals(ms.getDpid())) { |
| 350 | mastershipToRemove.put(ms.getDpid().toString(), ms); |
| 351 | } |
| 352 | } |
| 353 | log.debug("Swtich {} is really down.", ms.getDpid()); |
| 354 | } |
| 355 | processMastershipRemoved(mastershipToRemove.values()); |
| 356 | } |
| 357 | } |
| 358 | |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 359 | log.debug("num events {}, num of process {}, " |
| 360 | + "num of Population {}", numOfEvents, numOfEventProcess, |
| 361 | numOfPopulation); |
| 362 | } |
| 363 | |
| 364 | /** |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 365 | * Process the SwitchAdded events from topologyMananger. |
| 366 | * It does nothing. When a switch is added, then link will be added too. |
| 367 | * LinkAdded event will handle process all re-computation. |
| 368 | * |
| 369 | * @param switchAdded |
| 370 | */ |
| 371 | private void processSwitchAdd(Collection<SwitchData> switchAdded) { |
| 372 | |
| 373 | } |
| 374 | |
| 375 | /** |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 376 | * Remove all ports connected to the switch removed |
| 377 | * |
| 378 | * @param mastershipRemoved master switch info removed |
| 379 | */ |
| 380 | private void processMastershipRemoved(Collection<MastershipData> |
| 381 | mastershipRemoved) { |
| 382 | for (MastershipData mastership: mastershipRemoved) { |
| 383 | Switch sw = mutableTopology.getSwitch(mastership.getDpid()); |
| 384 | for (Link link: sw.getOutgoingLinks()) { |
| 385 | Port dstPort = link.getDstPort(); |
| 386 | IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 387 | getSwId(dstPort.getDpid().toString())); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 388 | if (dstSw != null) { |
| 389 | dstSw.removePortFromGroups(dstPort.getNumber()); |
| 390 | log.debug("MasterSwitch {} is gone: remove port {}", sw.getDpid(), dstPort); |
| 391 | } |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 392 | } |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 393 | } |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 394 | |
| 395 | linksToAdd.clear(); |
| 396 | linksDown.clear(); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 397 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 398 | |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 399 | /** |
| 400 | * Remove all ports connected to the switch removed |
| 401 | * |
| 402 | * @param switchRemoved Switch removed |
| 403 | */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 404 | private void processSwitchRemoved(Collection<SwitchData> switchRemoved) { |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 405 | log.debug("SwitchRemoved event occurred !!!"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 408 | /** |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 409 | * Report ports added to driver |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 410 | * |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 411 | * @param portEntries |
| 412 | */ |
| 413 | private void processPortAdd(Collection<PortData> portEntries) { |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 414 | // TODO: do we need to add ports with delay? |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 415 | for (PortData port : portEntries) { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 416 | Dpid dpid = port.getDpid(); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 417 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 418 | IOF13Switch sw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 419 | getSwId(port.getDpid().toString())); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 420 | if (sw != null) { |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 421 | sw.addPortToGroups(port.getPortNumber()); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 422 | //log.debug("Add port {} to switch {}", port, dpid); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 423 | } |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Reports ports of new links to driver and recalculate ECMP SPG |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 429 | * If the link to add was removed before, then we just schedule the add link |
| 430 | * event and do not recompute the path now. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 431 | * |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 432 | * @param linkEntries |
| 433 | */ |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 434 | private void processLinkAdd(Collection<LinkData> linkEntries, boolean delayed) { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 435 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 436 | for (LinkData link : linkEntries) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 437 | |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 438 | SwitchPort srcPort = link.getSrc(); |
| 439 | SwitchPort dstPort = link.getDst(); |
| 440 | |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 441 | String key = srcPort.getDpid().toString() + |
| 442 | dstPort.getDpid().toString(); |
| 443 | if (!delayed) { |
| 444 | if (linksDown.containsKey(key)) { |
| 445 | linksToAdd.put(key, link); |
| 446 | linksDown.remove(key); |
| 447 | linkAddTask.reschedule(DELAY_TO_ADD_LINK, TimeUnit.SECONDS); |
| 448 | log.debug("Add link {} with 5 sec delay", link); |
| 449 | // TODO: What if we have multiple events of add link: |
| 450 | // one is new link add, the other one is link up for |
| 451 | // broken link? ECMPSPG function cannot deal with it for now |
| 452 | return; |
| 453 | } |
| 454 | } |
| 455 | else { |
| 456 | if (linksDown.containsKey(key)) { |
| 457 | linksToAdd.remove(key); |
| 458 | log.debug("Do not add the link {}: it is down again!", link); |
| 459 | return; |
| 460 | } |
| 461 | } |
| 462 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 463 | IOF13Switch srcSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 464 | getSwId(srcPort.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 465 | IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 466 | getSwId(dstPort.getDpid().toString())); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 467 | |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 468 | if ((srcSw == null) || (dstSw == null)) |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 469 | continue; |
| 470 | |
| 471 | srcSw.addPortToGroups(srcPort.getPortNumber()); |
| 472 | dstSw.addPortToGroups(dstPort.getPortNumber()); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 473 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 474 | //log.debug("Add a link port {} to switch {} to add link {}", srcPort, srcSw, |
| 475 | // link); |
| 476 | //log.debug("Add a link port {} to switch {} to add link {}", dstPort, dstSw, |
| 477 | // link); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 478 | |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 479 | } |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 480 | populateEcmpRoutingRules(false); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 484 | * Check if all links are gone b/w the two switches. If all links are gone, |
| 485 | * then we need to recalculate the path. Otherwise, just report link failure |
Sangho Shin | 370e17b | 2014-10-27 11:39:08 -0700 | [diff] [blame] | 486 | * to the driver. IF the switches do not support ECMP in transit routers and |
| 487 | * the link removed is between transit routers, then just recompute the path |
| 488 | * regardless of ECMP. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 489 | * |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 490 | * @param linkEntries |
| 491 | */ |
| 492 | private void processLinkRemoval(Collection<LinkData> linkEntries) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 493 | for (LinkData link : linkEntries) { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 494 | SwitchPort srcPort = link.getSrc(); |
| 495 | SwitchPort dstPort = link.getDst(); |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 496 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 497 | IOF13Switch srcSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 498 | getSwId(srcPort.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 499 | IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 500 | getSwId(dstPort.getDpid().toString())); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 501 | if ((srcSw == null) || (dstSw == null)) |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 502 | /* If this link is not between two switches, ignore it */ |
| 503 | continue; |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 504 | |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 505 | srcSw.removePortFromGroups(srcPort.getPortNumber()); |
| 506 | dstSw.removePortFromGroups(dstPort.getPortNumber()); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 507 | log.debug("Remove port {} from switch {}", srcPort, srcSw); |
| 508 | log.debug("Remove port {} from switch {}", dstPort, dstSw); |
| 509 | |
Sangho Shin | f66aa26 | 2014-10-27 16:03:42 -0700 | [diff] [blame] | 510 | if ((srcSw instanceof OFSwitchImplDellOSR && |
| 511 | dstSw instanceof OFSwitchImplDellOSR) && |
Sangho Shin | 370e17b | 2014-10-27 11:39:08 -0700 | [diff] [blame] | 512 | isTransitRouter(mutableTopology.getSwitch(srcPort.getDpid())) && |
| 513 | isTransitRouter(mutableTopology.getSwitch(dstPort.getDpid()))) { |
| 514 | populateEcmpRoutingRules(false); |
| 515 | } |
| 516 | else { |
| 517 | Switch srcSwitch = mutableTopology.getSwitch(srcPort.getDpid()); |
| 518 | if (srcSwitch.getLinkToNeighbor(dstPort.getDpid()) == null) { |
| 519 | log.debug("All links are gone b/w {} and {}", srcPort.getDpid(), |
| 520 | dstPort.getDpid()); |
| 521 | log.debug("All paths will recomputed regardless of the rest " |
| 522 | + "of the event"); |
| 523 | populateEcmpRoutingRules(false); |
| 524 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 525 | } |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 526 | |
| 527 | String key = link.getSrc().getDpid().toString()+ |
| 528 | link.getDst().getDpid().toString(); |
| 529 | if (!linksDown.containsKey(key)) { |
| 530 | linksDown.put(key, link); |
| 531 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 532 | } |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 533 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 534 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 535 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 536 | /** |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 537 | * report ports removed to the driver immediately |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 538 | * |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 539 | * @param portEntries |
| 540 | */ |
| 541 | private void processPortRemoval(Collection<PortData> portEntries) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 542 | for (PortData port : portEntries) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 543 | Dpid dpid = port.getDpid(); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 544 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 545 | IOF13Switch sw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 546 | getSwId(port.getDpid().toString())); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 547 | if (sw != null) { |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 548 | sw.removePortFromGroups(port.getPortNumber()); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 549 | log.debug("Remove port {} from switch {}", port, dpid); |
| 550 | } |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 551 | } |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 552 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 553 | |
| 554 | /** |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 555 | * Add the link immediately |
| 556 | * The function is scheduled when link add event happens and called |
| 557 | * DELAY_TO_ADD_LINK seconds after the event to avoid link flip-flop. |
| 558 | */ |
| 559 | private void delayedAddLink() { |
| 560 | |
| 561 | processLinkAdd(linksToAdd.values(), true); |
| 562 | |
| 563 | } |
| 564 | |
| 565 | |
| 566 | // ************************************ |
| 567 | // ECMP shorted path routing functions |
| 568 | // ************************************ |
| 569 | |
| 570 | /** |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 571 | * Populate routing rules walking through the ECMP shortest paths |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 572 | * |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 573 | * @param modified if true, it "modifies" the rules |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 574 | */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 575 | private void populateEcmpRoutingRules(boolean modified) { |
| 576 | graphs.clear(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 577 | Iterable<Switch> switches = mutableTopology.getSwitches(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 578 | for (Switch sw : switches) { |
| 579 | ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(sw); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 580 | graphs.put(sw, ecmpSPG); |
| 581 | //log.debug("ECMPShortestPathGraph is computed for switch {}", |
| 582 | // HexString.toHexString(sw.getDpid().value())); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 583 | populateEcmpRoutingRulesForPath(sw, ecmpSPG, modified); |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 584 | |
| 585 | // Set adjacency routing rule for all switches |
| 586 | try { |
| 587 | populateAdjacencyncyRule(sw); |
| 588 | } catch (JSONException e) { |
| 589 | // TODO Auto-generated catch block |
| 590 | e.printStackTrace(); |
| 591 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 592 | } |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 593 | numOfPopulation++; |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 594 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 595 | |
Sangho Shin | 204b997 | 2014-10-22 11:08:10 -0700 | [diff] [blame] | 596 | /** |
| 597 | * populate the MPLS rules to handle Adjacency IDs |
| 598 | * |
| 599 | * @param sw Switch |
| 600 | * @throws JSONException |
| 601 | */ |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 602 | private void populateAdjacencyncyRule(Switch sw) throws JSONException { |
| 603 | String adjInfo = sw.getStringAttribute("adjacencySids"); |
Sangho Shin | 204b997 | 2014-10-22 11:08:10 -0700 | [diff] [blame] | 604 | String nodeSidStr = sw.getStringAttribute("nodeSid"); |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 605 | String srcMac = sw.getStringAttribute("routerMac"); |
Sangho Shin | 204b997 | 2014-10-22 11:08:10 -0700 | [diff] [blame] | 606 | String autoAdjInfo = sw.getStringAttribute("autogenAdjSids"); |
| 607 | |
Sangho Shin | ced05b6 | 2014-10-22 11:23:14 -0700 | [diff] [blame] | 608 | if (autoAdjInfo == null || srcMac == null || nodeSidStr == null) |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 609 | return; |
| 610 | |
Sangho Shin | ced05b6 | 2014-10-22 11:23:14 -0700 | [diff] [blame] | 611 | // parse adjacency Id |
Sangho Shin | cfef392 | 2014-10-22 12:04:16 -0700 | [diff] [blame] | 612 | HashMap<Integer, List<Integer>> adjacencyInfo = null; |
Sangho Shin | ced05b6 | 2014-10-22 11:23:14 -0700 | [diff] [blame] | 613 | if (adjInfo != null) { |
Sangho Shin | cfef392 | 2014-10-22 12:04:16 -0700 | [diff] [blame] | 614 | adjacencyInfo = parseAdjacencySidInfo(adjInfo); |
Sangho Shin | ced05b6 | 2014-10-22 11:23:14 -0700 | [diff] [blame] | 615 | } |
Sangho Shin | cfef392 | 2014-10-22 12:04:16 -0700 | [diff] [blame] | 616 | // parse auto generated adjacency Id |
| 617 | adjacencyInfo.putAll(parseAdjacencySidInfo(autoAdjInfo)); |
Sangho Shin | ced05b6 | 2014-10-22 11:23:14 -0700 | [diff] [blame] | 618 | |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 619 | adjacencySidTable.put(Integer.parseInt(nodeSidStr), adjacencyInfo); |
Sangho Shin | 204b997 | 2014-10-22 11:08:10 -0700 | [diff] [blame] | 620 | |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 621 | for (Integer adjId: adjacencyInfo.keySet()) { |
| 622 | List<Integer> ports = adjacencyInfo.get(adjId); |
| 623 | if (ports.size() == 1) { |
| 624 | setAdjacencyRuleOfOutput(sw, adjId, srcMac, ports.get(0)); |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 625 | } |
| 626 | else { |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 627 | setAdjacencyRuleOfGroup(sw, adjId, ports); |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 628 | } |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 629 | } |
| 630 | } |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 631 | |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 632 | /** |
| 633 | * Set Adjacency Rule to MPLS table for adjacency Ids attached to multiple |
| 634 | * ports |
| 635 | * |
| 636 | * @param sw Switch |
| 637 | * @param adjId Adjacency ID |
| 638 | * @param ports List of ports assigned to the Adjacency ID |
| 639 | */ |
| 640 | private void setAdjacencyRuleOfGroup(Switch sw, Integer adjId, List<Integer> ports) { |
| 641 | |
| 642 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 643 | getSwId(sw.getDpid().toString())); |
| 644 | |
| 645 | int groupId = -1; |
| 646 | if (sw13 != null) { |
| 647 | List<PortNumber> portList = new ArrayList<PortNumber>(); |
| 648 | for (Integer port: ports) |
| 649 | portList.add(PortNumber.uint32(port)); |
| 650 | groupId = sw13.createGroup(new ArrayList<Integer>(), portList); |
| 651 | } |
| 652 | |
| 653 | if (groupId < 0) { |
| 654 | log.debug("Failed to create a group at driver for adj ID {}", adjId); |
| 655 | } |
| 656 | |
| 657 | pushAdjRule(sw, adjId, null, null, groupId, true); |
| 658 | pushAdjRule(sw, adjId, null, null, groupId, false); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Set Adjacency Rule to MPLS table for adjacency Ids attached to single port |
| 663 | * |
| 664 | * @param sw Switch |
| 665 | * @param adjId Adjacency ID |
| 666 | * @param ports List of ports assigned to the Adjacency ID |
| 667 | */ |
| 668 | private void setAdjacencyRuleOfOutput(Switch sw, Integer adjId, String srcMac, Integer portNo) { |
| 669 | |
| 670 | Dpid dstDpid = null; |
| 671 | for (Link link: sw.getOutgoingLinks()) { |
| 672 | if (link.getSrcPort().getPortNumber().value() == portNo) { |
| 673 | dstDpid = link.getDstPort().getDpid(); |
| 674 | break; |
| 675 | } |
| 676 | } |
| 677 | if (dstDpid == null) { |
| 678 | log.debug("Cannot find the destination switch for the adjacency ID {}", adjId); |
| 679 | return; |
| 680 | } |
| 681 | Switch dstSw = mutableTopology.getSwitch(dstDpid); |
| 682 | String dstMac = null; |
| 683 | if (dstSw == null) { |
| 684 | log.debug("Cannot find SW {}", dstDpid.toString()); |
| 685 | return; |
| 686 | } |
| 687 | else { |
| 688 | dstMac = dstSw.getStringAttribute("routerMac"); |
| 689 | } |
| 690 | |
| 691 | pushAdjRule(sw, adjId, srcMac, dstMac, portNo, true); // BoS = 1 |
| 692 | pushAdjRule(sw, adjId, srcMac, dstMac, portNo, false); // BoS = 0 |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 693 | |
| 694 | } |
| 695 | |
Sangho Shin | 204b997 | 2014-10-22 11:08:10 -0700 | [diff] [blame] | 696 | /** |
| 697 | * Push the MPLS rule for Adjacency ID |
| 698 | * |
| 699 | * @param sw Switch to push the rule |
| 700 | * @param id Adjacency ID |
| 701 | * @param srcMac source MAC address |
| 702 | * @param dstMac destination MAC address |
| 703 | * @param portNo port number assigned to the ID |
| 704 | * @param bos BoS option |
| 705 | */ |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 706 | private void pushAdjRule(Switch sw, int id, String srcMac, String dstMac, |
| 707 | int num, boolean bos) { |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 708 | |
| 709 | MplsMatch mplsMatch = new MplsMatch(id, bos); |
| 710 | List<Action> actions = new ArrayList<Action>(); |
| 711 | |
| 712 | if (bos) { |
| 713 | PopMplsAction popAction = new PopMplsAction(EthType.IPv4); |
| 714 | CopyTtlInAction copyTtlInAction = new CopyTtlInAction(); |
| 715 | DecNwTtlAction decNwTtlAction = new DecNwTtlAction(1); |
| 716 | actions.add(copyTtlInAction); |
| 717 | actions.add(popAction); |
| 718 | actions.add(decNwTtlAction); |
| 719 | } |
| 720 | else { |
| 721 | PopMplsAction popAction = new PopMplsAction(EthType.MPLS_UNICAST); |
| 722 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
| 723 | actions.add(popAction); |
| 724 | actions.add(decMplsTtlAction); |
| 725 | } |
| 726 | |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 727 | // Output action |
| 728 | if (srcMac != null && dstMac != null) { |
Sangho Shin | d6d8a7e | 2014-10-28 14:51:03 -0700 | [diff] [blame] | 729 | ModifyDstMacAction setDstAction = new ModifyDstMacAction(MACAddress.valueOf(dstMac)); |
| 730 | ModifySrcMacAction setSrcAction = new ModifySrcMacAction(MACAddress.valueOf(srcMac)); |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 731 | OutputAction outportAction = new OutputAction(PortNumber.uint32(num)); |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 732 | |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 733 | actions.add(setDstAction); |
| 734 | actions.add(setSrcAction); |
| 735 | actions.add(outportAction); |
| 736 | } |
| 737 | // Group Action |
| 738 | else { |
| 739 | GroupAction groupAction = new GroupAction(); |
| 740 | groupAction.setGroupId(num); |
| 741 | actions.add(groupAction); |
| 742 | } |
Sangho Shin | 6d3c2f0 | 2014-10-22 10:10:55 -0700 | [diff] [blame] | 743 | |
| 744 | MatchAction matchAction = new MatchAction(new MatchActionId(matchActionId++), |
| 745 | new SwitchPort((long) 0, (short) 0), mplsMatch, actions); |
| 746 | Operator operator = Operator.ADD; |
| 747 | MatchActionOperationEntry maEntry = |
| 748 | new MatchActionOperationEntry(operator, matchAction); |
| 749 | |
| 750 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 751 | getSwId(sw.getDpid().toString())); |
| 752 | |
| 753 | if (sw13 != null) { |
| 754 | try { |
| 755 | //printMatchActionOperationEntry(sw, maEntry); |
| 756 | sw13.pushFlow(maEntry); |
| 757 | } catch (IOException e) { |
| 758 | e.printStackTrace(); |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 763 | /** |
| 764 | * populate routing rules to forward packets from the switch given to |
| 765 | * all other switches. |
| 766 | * |
| 767 | * @param sw source switch |
| 768 | * @param ecmpSPG shortest path from the the source switch to all others |
| 769 | * @param modified modification flag |
| 770 | */ |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 771 | private void populateEcmpRoutingRulesForPath(Switch sw, |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 772 | ECMPShortestPathGraph ecmpSPG, boolean modified) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 773 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 774 | HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>> switchVia = |
| 775 | ecmpSPG.getAllLearnedSwitchesAndVia(); |
| 776 | for (Integer itrIdx : switchVia.keySet()) { |
| 777 | //log.debug("ECMPShortestPathGraph:Switches learned in " |
| 778 | // + "Iteration{} from switch {}:", |
| 779 | // itrIdx, |
| 780 | // HexString.toHexString(sw.getDpid().value())); |
| 781 | HashMap<Switch, ArrayList<ArrayList<Dpid>>> swViaMap = |
| 782 | switchVia.get(itrIdx); |
| 783 | for (Switch targetSw : swViaMap.keySet()) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 784 | //log.debug("ECMPShortestPathGraph:****switch {} via:", |
| 785 | // HexString.toHexString(targetSw.getDpid().value())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 786 | String destSw = sw.getDpid().toString(); |
| 787 | List<String> fwdToSw = new ArrayList<String>(); |
| 788 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 789 | for (ArrayList<Dpid> via : swViaMap.get(targetSw)) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 790 | //log.debug("ECMPShortestPathGraph:******{}) {}", ++i, via); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 791 | if (via.isEmpty()) { |
| 792 | fwdToSw.add(destSw); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 793 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 794 | else { |
| 795 | fwdToSw.add(via.get(0).toString()); |
| 796 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 797 | } |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 798 | setRoutingRule(targetSw, destSw, fwdToSw, modified); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 799 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 800 | |
| 801 | // Send Barrier Message and make sure all rules are set |
| 802 | // before we set the rules to next routers |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 803 | // TODO: barriers to all switches in this update stage |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 804 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 805 | getSwId(sw.getDpid().toString())); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 806 | if (sw13 != null) { |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 807 | OFBarrierReplyFuture replyFuture = null; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 808 | try { |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 809 | replyFuture = sw13.sendBarrier(); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 810 | } catch (IOException e) { |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 811 | log.error("Error sending barrier request to switch {}", |
| 812 | sw13.getId(), e.getCause()); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 813 | } |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 814 | OFBarrierReply br = null; |
| 815 | try { |
| 816 | br = replyFuture.get(2, TimeUnit.SECONDS); |
| 817 | } catch (TimeoutException | InterruptedException | ExecutionException e) { |
| 818 | // XXX for some reason these exceptions are not being thrown |
| 819 | } |
| 820 | if (br == null) { |
| 821 | log.warn("Did not receive barrier-reply from {}", sw13.getId()); |
| 822 | // XXX take corrective action |
| 823 | } |
| 824 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
| 828 | } |
| 829 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 830 | /** |
| 831 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 832 | * Set routing rules in targetSw {forward packets to fwdToSw switches in |
| 833 | * order to send packets to destSw} - If the target switch is an edge router |
| 834 | * and final destnation switch is also an edge router, then set IP |
| 835 | * forwarding rules to subnets - If only the target switch is an edge |
| 836 | * router, then set IP forwarding rule to the transit router loopback IP |
| 837 | * address - If the target is a transit router, then just set the MPLS |
| 838 | * forwarding rule |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 839 | * |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 840 | * @param targetSw Switch to set the rules |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 841 | * @param destSw Final destination switches |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 842 | * @param fwdToSw next hop switches |
| 843 | */ |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 844 | private void setRoutingRule(Switch targetSw, String destSw, |
| 845 | List<String> fwdToSw, boolean modified) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 846 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 847 | if (fwdToSw.isEmpty()) { |
| 848 | fwdToSw.add(destSw); |
| 849 | } |
| 850 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 851 | // if both target SW and dest SW are an edge router, then set IP table |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 852 | if (IsEdgeRouter(targetSw.getDpid().toString()) && |
| 853 | IsEdgeRouter(destSw)) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 854 | // We assume that there is at least one transit router b/w edge |
| 855 | // routers |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 856 | Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw)); |
| 857 | String subnets = destSwitch.getStringAttribute("subnets"); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 858 | setIpTableRouterSubnet(targetSw, subnets, getMplsLabel(destSw) |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 859 | , fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 860 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 861 | String routerIp = destSwitch.getStringAttribute("routerIp"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 862 | setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw, |
| 863 | null, modified); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 864 | // Edge router can be a transit router |
| 865 | setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 866 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 867 | // Only if the target switch is the edge router, then set the IP rules |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 868 | else if (IsEdgeRouter(targetSw.getDpid().toString())) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 869 | // We assume that there is at least one transit router b/w edge |
| 870 | // routers |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 871 | Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw)); |
| 872 | String routerIp = destSwitch.getStringAttribute("routerIp"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 873 | setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw, |
| 874 | null, modified); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 875 | // Edge router can be a transit router |
| 876 | setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 877 | } |
| 878 | // if it is a transit router, then set rules in the MPLS table |
| 879 | else { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 880 | setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | } |
| 884 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 885 | /** |
| 886 | * Set IP forwarding rule to the gateway of each subnet of switches |
| 887 | * |
| 888 | * @param targetSw Switch to set rules |
| 889 | * @param subnets subnet information |
| 890 | * @param mplsLabel destination MPLS label |
| 891 | * @param fwdToSw router to forward packets to |
| 892 | */ |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 893 | private void setIpTableRouterSubnet(Switch targetSw, String subnets, |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 894 | String mplsLabel, List<String> fwdToSw, boolean modified) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 895 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 896 | Collection<MatchActionOperationEntry> entries = |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 897 | new ArrayList<MatchActionOperationEntry>(); |
| 898 | |
| 899 | try { |
| 900 | JSONArray arry = new JSONArray(subnets); |
| 901 | for (int i = 0; i < arry.length(); i++) { |
| 902 | String subnetIp = (String) arry.getJSONObject(i).get("subnetIp"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 903 | setIpTableRouter(targetSw, subnetIp, mplsLabel, fwdToSw, entries, |
| 904 | modified); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 905 | } |
| 906 | } catch (JSONException e) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 907 | e.printStackTrace(); |
| 908 | } |
| 909 | |
| 910 | if (!entries.isEmpty()) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 911 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 912 | getSwId(targetSw.getDpid().toString())); |
| 913 | |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 914 | if (sw13 != null) { |
| 915 | try { |
| 916 | sw13.pushFlows(entries); |
| 917 | } catch (IOException e) { |
| 918 | e.printStackTrace(); |
| 919 | } |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
| 923 | } |
| 924 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 925 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 926 | * Set IP forwarding rule - If the destination is the next hop, then do not |
| 927 | * push MPLS, just decrease the NW TTL - Otherwise, push MPLS label and set |
| 928 | * the MPLS ID |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 929 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 930 | * @param sw target switch to set rules |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 931 | * @param subnetIp Match IP address |
| 932 | * @param mplsLabel MPLS label of final destination router |
| 933 | * @param fwdToSws next hop routers |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 934 | * @param entries |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 935 | */ |
| 936 | private void setIpTableRouter(Switch sw, String subnetIp, String mplsLabel, |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 937 | List<String> fwdToSws, Collection<MatchActionOperationEntry> entries, |
| 938 | boolean modified) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 939 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 940 | Ipv4Match ipMatch = new Ipv4Match(subnetIp); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 941 | List<Action> actions = new ArrayList<>(); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 942 | GroupAction groupAction = new GroupAction(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 943 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 944 | // If destination SW is the same as the fwd SW, then do not push MPLS |
| 945 | // label |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 946 | if (fwdToSws.size() > 1) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 947 | PushMplsAction pushMplsAction = new PushMplsAction(); |
| 948 | SetMplsIdAction setIdAction = new SetMplsIdAction(Integer.parseInt(mplsLabel)); |
| 949 | CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 950 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 951 | |
Sangho Shin | 62ce5c1 | 2014-10-08 16:24:40 -0700 | [diff] [blame] | 952 | //actions.add(pushMplsAction); |
| 953 | //actions.add(copyTtlOutAction); |
| 954 | //actions.add(decMplsTtlAction); |
Saurav Das | 82e6297 | 2014-10-16 14:53:57 -0700 | [diff] [blame] | 955 | // actions.add(setIdAction); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 956 | groupAction.setEdgeLabel(Integer.parseInt(mplsLabel)); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 957 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 958 | else { |
| 959 | String fwdToSw = fwdToSws.get(0); |
| 960 | if (getMplsLabel(fwdToSw).equals(mplsLabel)) { |
| 961 | DecNwTtlAction decTtlAction = new DecNwTtlAction(1); |
| 962 | actions.add(decTtlAction); |
| 963 | } |
| 964 | else { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 965 | SetMplsIdAction setIdAction = new SetMplsIdAction( |
| 966 | Integer.parseInt(mplsLabel)); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 967 | CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 968 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 969 | |
Sangho Shin | 62ce5c1 | 2014-10-08 16:24:40 -0700 | [diff] [blame] | 970 | //actions.add(pushMplsAction); |
| 971 | //actions.add(copyTtlOutAction); |
| 972 | //actions.add(decMplsTtlAction); |
Saurav Das | 82e6297 | 2014-10-16 14:53:57 -0700 | [diff] [blame] | 973 | // actions.add(setIdAction); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 974 | groupAction.setEdgeLabel(Integer.parseInt(mplsLabel)); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 975 | } |
| 976 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 977 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 978 | for (String fwdSw : fwdToSws) { |
| 979 | groupAction.addSwitch(new Dpid(fwdSw)); |
| 980 | } |
| 981 | actions.add(groupAction); |
| 982 | |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 983 | MatchAction matchAction = new MatchAction(new MatchActionId(matchActionId++), |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 984 | new SwitchPort((long) 0, (short) 0), ipMatch, actions); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 985 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 986 | Operator operator = null; |
| 987 | if (modified) |
| 988 | operator = Operator.MODIFY; |
| 989 | else |
| 990 | operator = Operator.ADD; |
| 991 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 992 | MatchActionOperationEntry maEntry = |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 993 | new MatchActionOperationEntry(operator, matchAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 994 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 995 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 996 | getSwId(sw.getDpid().toString())); |
| 997 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 998 | if (sw13 != null) { |
| 999 | try { |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 1000 | //printMatchActionOperationEntry(sw, maEntry); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 1001 | if (entries != null) |
| 1002 | entries.add(maEntry); |
| 1003 | else |
| 1004 | sw13.pushFlow(maEntry); |
| 1005 | } catch (IOException e) { |
| 1006 | e.printStackTrace(); |
| 1007 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 1012 | /** |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 1013 | * Set MPLS forwarding rules to MPLS table |
| 1014 | * </p> |
| 1015 | * If the destination is the same as the next hop to forward packets then, |
| 1016 | * pop the MPLS label according to PHP rule. Here, if BoS is set, then |
| 1017 | * copy TTL In and decrement NW TTL. Otherwise, it just decrement the MPLS |
| 1018 | * TTL of the another MPLS header. |
| 1019 | * If the next hop is not the destination, just forward packets to next |
| 1020 | * hops using Group action. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1021 | * |
Sangho Shin | 204b997 | 2014-10-22 11:08:10 -0700 | [diff] [blame] | 1022 | * TODO: refactoring required |
| 1023 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1024 | * @param sw Switch to set the rules |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1025 | * @param mplsLabel destination MPLS label |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1026 | * @param fwdSws next hop switches |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 1027 | * */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 1028 | private void setMplsTable(Switch sw, String mplsLabel, List<String> fwdSws, |
| 1029 | boolean modified) { |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1030 | |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 1031 | if (fwdSws.isEmpty()) |
| 1032 | return; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1033 | |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 1034 | Collection<MatchActionOperationEntry> maEntries = |
| 1035 | new ArrayList<MatchActionOperationEntry>(); |
| 1036 | String fwdSw1 = fwdSws.get(0); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1037 | |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 1038 | //If the next hop is the destination router, do PHP |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 1039 | if (fwdSws.size() == 1 && mplsLabel.equals(getMplsLabel(fwdSw1))) { |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 1040 | maEntries.add(buildMAEntry(sw, mplsLabel, fwdSws, true, true)); |
| 1041 | maEntries.add(buildMAEntry(sw, mplsLabel, fwdSws, true, false)); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 1042 | } |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 1043 | else { |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 1044 | maEntries.add(buildMAEntry(sw, mplsLabel, fwdSws, false, true)); |
| 1045 | maEntries.add(buildMAEntry(sw, mplsLabel, fwdSws, false, false)); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 1046 | } |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1047 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 1048 | getSwId(sw.getDpid().toString())); |
| 1049 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 1050 | if (sw13 != null) { |
| 1051 | try { |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 1052 | //printMatchActionOperationEntry(sw, maEntry); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 1053 | sw13.pushFlows(maEntries); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 1054 | } catch (IOException e) { |
| 1055 | e.printStackTrace(); |
| 1056 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 1057 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1060 | |
| 1061 | // ************************************ |
| 1062 | // Policy routing classes and functions |
| 1063 | // ************************************ |
| 1064 | |
Fahad Naeem Khan | 1e712ad | 2014-10-27 16:48:07 -0700 | [diff] [blame] | 1065 | /** |
| 1066 | * Enums for policy type |
| 1067 | * |
| 1068 | */ |
| 1069 | public enum PolicyType{ |
| 1070 | TUNNEL_FLOW, |
| 1071 | LOADBALANCE, |
| 1072 | AVOID, |
| 1073 | DENY |
| 1074 | } |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1075 | public class PolicyInfo { |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1076 | private String policyId; |
| 1077 | private PacketMatch match; |
| 1078 | private int priority; |
| 1079 | private String tunnelId; |
Fahad Naeem Khan | 1e712ad | 2014-10-27 16:48:07 -0700 | [diff] [blame] | 1080 | private PolicyType type; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1081 | |
Fahad Naeem Khan | 1e712ad | 2014-10-27 16:48:07 -0700 | [diff] [blame] | 1082 | public PolicyInfo(String pid, PolicyType type, PacketMatch match, int priority, |
Sangho Shin | 5818267 | 2014-10-21 13:23:38 -0700 | [diff] [blame] | 1083 | String tid) { |
| 1084 | this.policyId = pid; |
| 1085 | this.match = match; |
| 1086 | this.priority = priority; |
| 1087 | this.tunnelId = tid; |
| 1088 | this.type = type; |
| 1089 | } |
Sangho Shin | 204b997 | 2014-10-22 11:08:10 -0700 | [diff] [blame] | 1090 | |
Fahad Naeem Khan | 95aa401 | 2014-10-21 14:07:00 -0700 | [diff] [blame] | 1091 | public PolicyInfo(String pid, PacketMatch match, int priority, |
| 1092 | String tid) { |
| 1093 | this.policyId = pid; |
| 1094 | this.match = match; |
| 1095 | this.priority = priority; |
| 1096 | this.tunnelId = tid; |
Fahad Naeem Khan | 1e712ad | 2014-10-27 16:48:07 -0700 | [diff] [blame] | 1097 | this.type = PolicyType.TUNNEL_FLOW; |
Fahad Naeem Khan | 95aa401 | 2014-10-21 14:07:00 -0700 | [diff] [blame] | 1098 | } |
| 1099 | public String getPolicyId(){ |
| 1100 | return this.policyId; |
| 1101 | } |
| 1102 | public PacketMatch getMatch(){ |
| 1103 | return this.match; |
| 1104 | } |
| 1105 | public int getPriority(){ |
| 1106 | return this.priority; |
| 1107 | } |
| 1108 | public String getTunnelId(){ |
| 1109 | return this.tunnelId; |
| 1110 | } |
Fahad Naeem Khan | 1e712ad | 2014-10-27 16:48:07 -0700 | [diff] [blame] | 1111 | public PolicyType getType(){ |
Fahad Naeem Khan | 95aa401 | 2014-10-21 14:07:00 -0700 | [diff] [blame] | 1112 | return this.type; |
| 1113 | } |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1116 | public class TunnelInfo { |
| 1117 | private String tunnelId; |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 1118 | private List<Integer> labelIds; |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1119 | private List<TunnelRouteInfo> routes; |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1120 | |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1121 | public TunnelInfo(String tid, List<Integer> labelIds, |
| 1122 | List<TunnelRouteInfo> routes) { |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1123 | this.tunnelId = tid; |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 1124 | this.labelIds = labelIds; |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1125 | this.routes = routes; |
| 1126 | } |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1127 | public String getTunnelId(){ |
| 1128 | return this.tunnelId; |
| 1129 | } |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 1130 | |
| 1131 | public List<Integer> getLabelids() { |
| 1132 | return this.labelIds; |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1133 | } |
| 1134 | public List<TunnelRouteInfo> getRoutes(){ |
| 1135 | return this.routes; |
| 1136 | } |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1139 | public class TunnelRouteInfo { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1140 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1141 | private String srcSwDpid; |
| 1142 | private List<Dpid> fwdSwDpids; |
| 1143 | private List<String> route; |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1144 | private int gropuId; |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1145 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1146 | public TunnelRouteInfo() { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1147 | fwdSwDpids = new ArrayList<Dpid>(); |
| 1148 | route = new ArrayList<String>(); |
| 1149 | } |
| 1150 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1151 | private void setSrcDpid(String dpid) { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1152 | this.srcSwDpid = dpid; |
| 1153 | } |
| 1154 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1155 | private void setFwdSwDpid(List<Dpid> dpid) { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1156 | this.fwdSwDpids = dpid; |
| 1157 | } |
| 1158 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1159 | private void addRoute(String id) { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1160 | route.add(id); |
| 1161 | } |
| 1162 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1163 | private void setRoute(List<String> r) { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1164 | this.route = r; |
| 1165 | } |
| 1166 | |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1167 | private void setGroupId(int groupId) { |
| 1168 | this.gropuId = groupId; |
| 1169 | } |
| 1170 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1171 | public String getSrcSwDpid() { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1172 | return this.srcSwDpid; |
| 1173 | } |
| 1174 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1175 | public List<Dpid> getFwdSwDpid() { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1176 | return this.fwdSwDpids; |
| 1177 | } |
| 1178 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1179 | public List<String> getRoute() { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1180 | return this.route; |
| 1181 | } |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1182 | |
| 1183 | public int getGroupId() { |
| 1184 | return this.gropuId; |
| 1185 | } |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1188 | /** |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1189 | * Return the Tunnel table |
| 1190 | * |
| 1191 | * @return collection of TunnelInfo |
| 1192 | */ |
| 1193 | public Collection<TunnelInfo> getTunnelTable() { |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1194 | return this.tunnelTable.values(); |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1195 | } |
Sangho Shin | 204b997 | 2014-10-22 11:08:10 -0700 | [diff] [blame] | 1196 | |
Fahad Naeem Khan | 12fa63a | 2014-10-21 17:01:27 -0700 | [diff] [blame] | 1197 | public Collection<PolicyInfo> getPoclicyTable() { |
| 1198 | return this.policyTable.values(); |
| 1199 | } |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1200 | |
| 1201 | /** |
Sangho Shin | 5671cbb | 2014-10-20 22:35:41 -0700 | [diff] [blame] | 1202 | * Return router DPIDs for the tunnel |
| 1203 | * |
| 1204 | * @param tid tunnel ID |
| 1205 | * @return List of DPID |
| 1206 | */ |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 1207 | public List<Integer> getTunnelInfo(String tid) { |
Sangho Shin | 5671cbb | 2014-10-20 22:35:41 -0700 | [diff] [blame] | 1208 | TunnelInfo tunnelInfo = tunnelTable.get(tid); |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 1209 | return tunnelInfo.labelIds; |
Sangho Shin | 5671cbb | 2014-10-20 22:35:41 -0700 | [diff] [blame] | 1210 | |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * Get the first group ID for the tunnel for specific source router |
| 1215 | * If Segment Stitching was required to create the tunnel, there are |
| 1216 | * mutiple source routers. |
| 1217 | * |
| 1218 | * @param tunnelId ID for the tunnel |
| 1219 | * @param dpid source router DPID |
| 1220 | * @return the first group ID of the tunnel |
| 1221 | */ |
| 1222 | public int getTunnelGroupId(String tunnelId, String dpid) { |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1223 | TunnelInfo tunnelInfo = tunnelTable.get(tunnelId); |
| 1224 | for (TunnelRouteInfo routeInfo: tunnelInfo.getRoutes()) { |
| 1225 | String tunnelSrcDpid = routeInfo.getSrcSwDpid(); |
| 1226 | if (tunnelSrcDpid.equals(dpid)) |
| 1227 | return routeInfo.getGroupId(); |
| 1228 | } |
Sangho Shin | 5671cbb | 2014-10-20 22:35:41 -0700 | [diff] [blame] | 1229 | |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1230 | return -1; |
Sangho Shin | 5671cbb | 2014-10-20 22:35:41 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | /** |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1234 | * Create a tunnel for policy routing |
| 1235 | * It delivers the node IDs of tunnels to driver. |
| 1236 | * Split the node IDs if number of IDs exceeds the limit for stitching. |
| 1237 | * |
| 1238 | * @param tunnelId Node IDs for the tunnel |
| 1239 | * @param Ids tunnel ID |
| 1240 | */ |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 1241 | public boolean createTunnel(String tunnelId, List<Integer> labelIds) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1242 | |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 1243 | if (labelIds.isEmpty() || labelIds.size() < 2) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1244 | log.debug("Wrong tunnel information"); |
| 1245 | return false; |
| 1246 | } |
| 1247 | |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1248 | List<String> Ids = new ArrayList<String>(); |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 1249 | for (Integer label : labelIds) { |
| 1250 | Ids.add(label.toString()); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1253 | List<TunnelRouteInfo> stitchingRule = getStitchingRule(Ids); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1254 | if (stitchingRule == null) { |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1255 | log.debug("Failed to get a tunnel rule."); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1256 | return false; |
| 1257 | } |
Sangho Shin | 5590871 | 2014-10-27 15:16:48 -0700 | [diff] [blame] | 1258 | |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1259 | for (TunnelRouteInfo route: stitchingRule) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1260 | NeighborSet ns = new NeighborSet(); |
| 1261 | for (Dpid dpid: route.getFwdSwDpid()) |
| 1262 | ns.addDpid(dpid); |
| 1263 | |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1264 | printTunnelInfo(route.srcSwDpid, tunnelId, route.getRoute(), ns); |
| 1265 | int groupId = -1; |
| 1266 | if ((groupId =createGroupsForTunnel(tunnelId, route, ns)) < 0) { |
| 1267 | log.debug("Failed to create a tunnel at driver."); |
| 1268 | return false; |
| 1269 | } |
| 1270 | route.setGroupId(groupId); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1273 | TunnelInfo tunnelInfo = new TunnelInfo(tunnelId, labelIds, |
| 1274 | stitchingRule); |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1275 | tunnelTable.put(tunnelId, tunnelInfo); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1276 | |
| 1277 | return true; |
| 1278 | } |
| 1279 | |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1280 | /** |
| 1281 | * Create groups for the tunnel |
| 1282 | * |
| 1283 | * @param tunnelId tunnel ID |
| 1284 | * @param routeInfo label stacks for the tunnel |
| 1285 | * @param ns NeighborSet to forward packets |
| 1286 | * @return group ID, return -1 if it fails |
| 1287 | */ |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1288 | private int createGroupsForTunnel(String tunnelId, TunnelRouteInfo routeInfo, |
| 1289 | NeighborSet ns) { |
| 1290 | |
| 1291 | IOF13Switch targetSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 1292 | getSwId(routeInfo.srcSwDpid)); |
| 1293 | |
| 1294 | if (targetSw == null) { |
| 1295 | log.debug("Switch {} is gone.", routeInfo.srcSwDpid); |
| 1296 | return -1; |
| 1297 | } |
| 1298 | |
| 1299 | List<Integer> Ids = new ArrayList<Integer>(); |
| 1300 | for (String IdStr: routeInfo.route) |
| 1301 | Ids.add(Integer.parseInt(IdStr)); |
| 1302 | |
| 1303 | List<PortNumber> ports = getPortsFromNeighborSet(routeInfo.srcSwDpid, ns); |
| 1304 | int groupId = targetSw.createGroup(Ids, ports); |
| 1305 | |
| 1306 | return groupId; |
| 1307 | } |
| 1308 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1309 | /** |
| 1310 | * Set policy table for policy routing |
| 1311 | * |
| 1312 | * @param sw |
| 1313 | * @param mplsLabel |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1314 | * @return |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1315 | */ |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1316 | public boolean createPolicy(String pid, MACAddress srcMac, MACAddress dstMac, |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1317 | Short etherType, IPv4Net srcIp, IPv4Net dstIp, Byte ipProto, |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1318 | Short srcTcpPort, Short dstTcpPort, int priority, String tid) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1319 | |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1320 | PacketMatchBuilder packetBuilder = new PacketMatchBuilder(); |
| 1321 | |
| 1322 | if (srcMac != null) |
| 1323 | packetBuilder.setSrcMac(srcMac); |
| 1324 | if (dstMac != null) |
| 1325 | packetBuilder.setDstMac(dstMac); |
Sangho Shin | 5818267 | 2014-10-21 13:23:38 -0700 | [diff] [blame] | 1326 | if (etherType == null) // Cqpd requires the type of IPV4 |
| 1327 | packetBuilder.setEtherType(Ethernet.TYPE_IPV4); |
| 1328 | else |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1329 | packetBuilder.setEtherType(etherType); |
| 1330 | if (srcIp != null) |
| 1331 | packetBuilder.setSrcIp(srcIp.address(), srcIp.prefixLen()); |
| 1332 | if (dstIp != null) |
| 1333 | packetBuilder.setDstIp(dstIp.address(), dstIp.prefixLen()); |
| 1334 | if (ipProto != null) |
| 1335 | packetBuilder.setIpProto(ipProto); |
| 1336 | if (srcTcpPort > 0) |
| 1337 | packetBuilder.setSrcTcpPort(srcTcpPort); |
| 1338 | if (dstTcpPort > 0) |
| 1339 | packetBuilder.setDstTcpPort(dstTcpPort); |
| 1340 | PacketMatch policyMatch = packetBuilder.build(); |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1341 | TunnelInfo tunnelInfo = tunnelTable.get(tid); |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1342 | if (tunnelInfo == null) { |
| 1343 | log.debug("Tunnel {} is not defined", tid); |
| 1344 | return false; |
| 1345 | } |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1346 | List<TunnelRouteInfo> routes = tunnelInfo.routes; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1347 | |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1348 | for (TunnelRouteInfo route : routes) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1349 | List<Action> actions = new ArrayList<>(); |
Sangho Shin | c5a38a0 | 2014-10-28 16:09:38 -0700 | [diff] [blame] | 1350 | |
| 1351 | // Check PHP was done by stitching |
| 1352 | // If no MPLS label is added, then NW TTL needs to be decremented |
| 1353 | if (route.getRoute().isEmpty()) { |
| 1354 | DecNwTtlAction decNwTtlAction = new DecNwTtlAction(1); |
| 1355 | actions.add(decNwTtlAction); |
| 1356 | } |
| 1357 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1358 | GroupAction groupAction = new GroupAction(); |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1359 | groupAction.setGroupId(route.getGroupId()); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1360 | actions.add(groupAction); |
| 1361 | |
| 1362 | MatchAction matchAction = new MatchAction(new MatchActionId( |
| 1363 | matchActionId++), |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1364 | new SwitchPort((long) 0, (short) 0), policyMatch, priority, |
| 1365 | actions); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1366 | MatchActionOperationEntry maEntry = |
| 1367 | new MatchActionOperationEntry(Operator.ADD, matchAction); |
| 1368 | |
| 1369 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1370 | getSwId(route.srcSwDpid)); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1371 | |
| 1372 | if (sw13 != null) { |
| 1373 | printMatchActionOperationEntry(sw13, maEntry); |
| 1374 | try { |
| 1375 | sw13.pushFlow(maEntry); |
| 1376 | } catch (IOException e) { |
| 1377 | e.printStackTrace(); |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1378 | return false; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1379 | } |
| 1380 | } |
| 1381 | } |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1382 | |
| 1383 | PolicyInfo policyInfo = new PolicyInfo(pid, policyMatch, priority, tid); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1384 | policyTable.put(pid, policyInfo); |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1385 | |
| 1386 | return true; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | /** |
Sangho Shin | 1ad7be0 | 2014-10-20 16:56:49 -0700 | [diff] [blame] | 1390 | * Split the nodes IDs into multiple tunnel if Segment Stitching is required. |
| 1391 | * We assume that the first node ID is the one of source router, and the last |
| 1392 | * node ID is that of the destination router. |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1393 | * |
Sangho Shin | 1ad7be0 | 2014-10-20 16:56:49 -0700 | [diff] [blame] | 1394 | * @param route list of node IDs |
| 1395 | * @return List of the TunnelRoutInfo |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1396 | */ |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1397 | private List<TunnelRouteInfo> getStitchingRule(List<String> route) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1398 | |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1399 | if (route.isEmpty() || route.size() < 3) |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1400 | return null; |
| 1401 | |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1402 | List<TunnelRouteInfo> rules = new ArrayList<TunnelRouteInfo>(); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1403 | |
| 1404 | Switch srcSw = this.getSwitchFromNodeId(route.get(0)); |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1405 | if (srcSw == null) { |
| 1406 | log.warn("Switch is not found for Node SID {}", route.get(0)); |
| 1407 | return null; |
| 1408 | } |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1409 | String srcDpid = srcSw.getDpid().toString(); |
| 1410 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1411 | int i = 0; |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1412 | TunnelRouteInfo routeInfo = new TunnelRouteInfo(); |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1413 | boolean checkNeighbor = false; |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1414 | String prevAdjacencySid = null; |
| 1415 | String prevNodeId = null; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1416 | |
| 1417 | for (String nodeId: route) { |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1418 | // The first node ID is always the source router. |
| 1419 | // We assume that the first ID cannot be an Adjacency SID. |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1420 | if (i == 0) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1421 | srcSw = getSwitchFromNodeId(nodeId); |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1422 | if (srcDpid == null) |
| 1423 | srcDpid = srcSw.getDpid().toString(); |
| 1424 | routeInfo.setSrcDpid(srcDpid); |
| 1425 | checkNeighbor = true; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1426 | i++; |
| 1427 | } |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1428 | // if this is the first node ID to put the label stack.. |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1429 | else if (i == 1) { |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1430 | if (checkNeighbor) { |
| 1431 | List<Dpid> fwdSws = getDpidIfNeighborOf(nodeId, srcSw); |
| 1432 | // if nodeId is NOT the neighbor of srcSw.. |
| 1433 | if (fwdSws.isEmpty()) { |
| 1434 | fwdSws = getForwardingSwitchForNodeId(srcSw,nodeId); |
| 1435 | if (fwdSws == null || fwdSws.isEmpty()) { |
| 1436 | log.warn("There is no route from node {} to node {}", |
| 1437 | srcSw.getDpid(), nodeId); |
| 1438 | return null; |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1439 | } |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1440 | routeInfo.addRoute(nodeId); |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1441 | i++; |
| 1442 | } |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1443 | routeInfo.setFwdSwDpid(fwdSws); |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1444 | // we check only the next node ID of the source router |
| 1445 | checkNeighbor = false; |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1446 | } |
| 1447 | // if neighbor check is already done, then just add it |
| 1448 | else { |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1449 | routeInfo.addRoute(nodeId); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1450 | i++; |
| 1451 | } |
| 1452 | } |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1453 | // if i > 1 |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1454 | else { |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1455 | // If the adjacency SID is pushed and the next SID is the destination |
| 1456 | // of the adjacency SID, then do not add the SID. |
| 1457 | if (prevAdjacencySid != null) { |
| 1458 | if (isAdjacencySidNeighborOf(prevNodeId, prevAdjacencySid, nodeId)) { |
| 1459 | prevAdjacencySid = null; |
Sangho Shin | d6d8a7e | 2014-10-28 14:51:03 -0700 | [diff] [blame] | 1460 | prevNodeId = nodeId; |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1461 | continue; |
| 1462 | } |
| 1463 | prevAdjacencySid = null; |
| 1464 | } |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1465 | routeInfo.addRoute(nodeId); |
| 1466 | i++; |
| 1467 | } |
| 1468 | |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1469 | // If the adjacency ID is added the label stack, |
| 1470 | // then we need to check if the next node is the destination of the adjacency SID |
| 1471 | if (isAdjacencySid(nodeId)) |
| 1472 | prevAdjacencySid = nodeId; |
| 1473 | |
Sangho Shin | 1ad7be0 | 2014-10-20 16:56:49 -0700 | [diff] [blame] | 1474 | // If the number of labels reaches the limit, start over the procedure |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1475 | if (i == MAX_NUM_LABELS+1) { |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1476 | |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1477 | rules.add(routeInfo); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1478 | routeInfo = new TunnelRouteInfo(); |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1479 | |
Sangho Shin | 6232558 | 2014-10-24 10:36:09 -0700 | [diff] [blame] | 1480 | if (isAdjacencySid(nodeId)) { |
| 1481 | // If the previous sub tunnel finishes with adjacency SID, |
| 1482 | // then we need to start the procedure from the adjacency |
| 1483 | // destination ID. |
| 1484 | List<Switch> destNodeList = |
| 1485 | getAdjacencyDestinationNode(prevNodeId, nodeId); |
| 1486 | if (destNodeList == null || destNodeList.isEmpty()) { |
| 1487 | log.warn("Cannot find destination node for adjacencySID {}", |
| 1488 | nodeId); |
| 1489 | return null; |
| 1490 | } |
| 1491 | // If the previous sub tunnel finishes with adjacency SID with |
| 1492 | // multiple ports, then we need to remove the adjacency Sid |
| 1493 | // from the previous sub tunnel and start the new sub tunnel |
| 1494 | // with the adjacency Sid. Technically, the new subtunnel |
| 1495 | // forward packets to the port assigned to the adjacency Sid |
| 1496 | // and the label stack starts with the next ID. |
| 1497 | // This is to avoid to install new policy rule to multiple nodes for stitching when the |
| 1498 | // adjacency Sid that has more than one port. |
| 1499 | if (destNodeList.size() > 1) { |
| 1500 | rules.get(rules.size()-1).route.remove(nodeId); |
| 1501 | srcSw = getSwitchFromNodeId(prevNodeId); |
| 1502 | List<Dpid> fwdSws = getDpidIfNeighborOf(nodeId, srcSw); |
| 1503 | routeInfo.setFwdSwDpid(fwdSws); |
| 1504 | routeInfo.setSrcDpid(srcSw.getDpid().toString()); |
| 1505 | i = 1; |
| 1506 | checkNeighbor = false; |
| 1507 | continue; |
| 1508 | } |
| 1509 | else { |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1510 | srcSw = destNodeList.get(0); |
Sangho Shin | 6232558 | 2014-10-24 10:36:09 -0700 | [diff] [blame] | 1511 | } |
| 1512 | } |
| 1513 | else { |
| 1514 | srcSw = getSwitchFromNodeId(nodeId); |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1515 | } |
| 1516 | srcDpid = srcSw.getDpid().toString(); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1517 | routeInfo.setSrcDpid(srcDpid); |
| 1518 | i = 1; |
| 1519 | checkNeighbor = true; |
| 1520 | } |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1521 | |
| 1522 | if (prevAdjacencySid == null) |
| 1523 | prevNodeId = nodeId; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1526 | |
Sangho Shin | eb148ea | 2014-10-24 12:44:15 -0700 | [diff] [blame] | 1527 | if (i < MAX_NUM_LABELS+1 && (routeInfo.getFwdSwDpid() != null && |
| 1528 | !routeInfo.getFwdSwDpid().isEmpty())) { |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1529 | rules.add(routeInfo); |
Sangho Shin | eb148ea | 2014-10-24 12:44:15 -0700 | [diff] [blame] | 1530 | // NOTE: empty label stack can happen, but forwarding destination should be set |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | return rules; |
| 1534 | } |
| 1535 | |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1536 | /** |
| 1537 | * Remove all policies applied to specific tunnel. |
| 1538 | * |
| 1539 | * @param srcMac |
| 1540 | * @param dstMac |
| 1541 | * @param etherType |
| 1542 | * @param srcIp |
| 1543 | * @param dstIp |
| 1544 | * @param ipProto |
| 1545 | * @param srcTcpPort |
| 1546 | * @param dstTcpPort |
| 1547 | * @param tid |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1548 | * @return |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1549 | */ |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1550 | public boolean removePolicy(String pid) { |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1551 | PolicyInfo policyInfo = policyTable.get(pid); |
Sangho Shin | 5671cbb | 2014-10-20 22:35:41 -0700 | [diff] [blame] | 1552 | if (policyInfo == null) |
| 1553 | return false; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1554 | PacketMatch policyMatch = policyInfo.match; |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1555 | String tid = policyInfo.tunnelId; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1556 | int priority = policyInfo.priority; |
| 1557 | |
| 1558 | List<Action> actions = new ArrayList<>(); |
| 1559 | int gropuId = 0; // dummy group ID |
| 1560 | GroupAction groupAction = new GroupAction(); |
| 1561 | groupAction.setGroupId(gropuId); |
| 1562 | actions.add(groupAction); |
| 1563 | |
| 1564 | MatchAction matchAction = new MatchAction(new MatchActionId( |
| 1565 | matchActionId++), |
| 1566 | new SwitchPort((long) 0, (short) 0), policyMatch, priority, |
| 1567 | actions); |
| 1568 | MatchActionOperationEntry maEntry = |
| 1569 | new MatchActionOperationEntry(Operator.REMOVE, matchAction); |
| 1570 | |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1571 | TunnelInfo tunnelInfo = tunnelTable.get(tid); |
Sangho Shin | 5671cbb | 2014-10-20 22:35:41 -0700 | [diff] [blame] | 1572 | if (tunnelInfo == null) |
| 1573 | return false; |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1574 | List<TunnelRouteInfo> routes = tunnelInfo.routes; |
| 1575 | |
| 1576 | for (TunnelRouteInfo route : routes) { |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1577 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1578 | getSwId(route.srcSwDpid)); |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1579 | |
Sangho Shin | 5671cbb | 2014-10-20 22:35:41 -0700 | [diff] [blame] | 1580 | if (sw13 == null) { |
| 1581 | return false; |
| 1582 | } |
| 1583 | else { |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1584 | printMatchActionOperationEntry(sw13, maEntry); |
| 1585 | try { |
| 1586 | sw13.pushFlow(maEntry); |
| 1587 | } catch (IOException e) { |
| 1588 | e.printStackTrace(); |
| 1589 | log.debug("policy remove failed due to pushFlow() exception"); |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1590 | return false; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1595 | policyTable.remove(pid); |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1596 | log.debug("Policy {} is removed.", pid); |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1597 | return true; |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1598 | } |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1599 | |
Fahad Naeem Khan | 4ff83d6 | 2014-10-28 16:18:07 -0700 | [diff] [blame^] | 1600 | public enum removeTunnelMessages{ |
| 1601 | SUCCESS(0, "Tunnel is removed successfully."), |
| 1602 | ERROR_REFERENCED(1, "Can't remove tunnel as its referenced by other policy(s)"), |
| 1603 | ERROR_SWITCH(2, "Switch not found in the tunnel route"), |
| 1604 | ERROR_DRIVER(3, "Can't remove tunnel at driver"), |
| 1605 | ERROR_TUNNEL(4, "Tunnel not found"); |
| 1606 | |
| 1607 | private final int code; |
| 1608 | private final String description; |
| 1609 | |
| 1610 | private removeTunnelMessages(int code, String description) { |
| 1611 | this.code = code; |
| 1612 | this.description = description; |
| 1613 | } |
| 1614 | |
| 1615 | public String getDescription() { |
| 1616 | return this.description; |
| 1617 | } |
| 1618 | |
| 1619 | public int getCode() { |
| 1620 | return this.code; |
| 1621 | } |
| 1622 | |
| 1623 | @Override |
| 1624 | public String toString() { |
| 1625 | return "[" + this.code + ": " + this.description + "]"; |
| 1626 | } |
| 1627 | |
| 1628 | } |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1629 | /** |
| 1630 | * Remove a tunnel |
| 1631 | * It removes all groups for the tunnel if the tunnel is not used for any |
| 1632 | * policy. |
| 1633 | * |
| 1634 | * @param tunnelId tunnel ID to remove |
| 1635 | */ |
Fahad Naeem Khan | 4ff83d6 | 2014-10-28 16:18:07 -0700 | [diff] [blame^] | 1636 | public removeTunnelMessages removeTunnel(String tunnelId) { |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1637 | |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1638 | // Check if the tunnel is used for any policy |
| 1639 | for (PolicyInfo policyInfo: policyTable.values()) { |
Sangho Shin | a000c61 | 2014-10-21 14:17:59 -0700 | [diff] [blame] | 1640 | if (policyInfo.tunnelId.equals(tunnelId)) { |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1641 | log.debug("Tunnel {} is still used for the policy {}.", |
| 1642 | policyInfo.policyId, tunnelId); |
Fahad Naeem Khan | 4ff83d6 | 2014-10-28 16:18:07 -0700 | [diff] [blame^] | 1643 | return removeTunnelMessages.ERROR_REFERENCED; |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | TunnelInfo tunnelInfo = tunnelTable.get(tunnelId); |
Sangho Shin | 5671cbb | 2014-10-20 22:35:41 -0700 | [diff] [blame] | 1648 | if (tunnelInfo == null) |
Fahad Naeem Khan | 4ff83d6 | 2014-10-28 16:18:07 -0700 | [diff] [blame^] | 1649 | return removeTunnelMessages.ERROR_TUNNEL; |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1650 | |
| 1651 | List<TunnelRouteInfo> routes = tunnelInfo.routes; |
| 1652 | for (TunnelRouteInfo route: routes) { |
| 1653 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 1654 | getSwId(route.srcSwDpid)); |
| 1655 | |
Sangho Shin | 5671cbb | 2014-10-20 22:35:41 -0700 | [diff] [blame] | 1656 | if (sw13 == null) { |
Fahad Naeem Khan | 4ff83d6 | 2014-10-28 16:18:07 -0700 | [diff] [blame^] | 1657 | return removeTunnelMessages.ERROR_SWITCH; |
Sangho Shin | 5671cbb | 2014-10-20 22:35:41 -0700 | [diff] [blame] | 1658 | } |
| 1659 | else { |
Sangho Shin | 93f623c | 2014-10-24 12:59:53 -0700 | [diff] [blame] | 1660 | if (!sw13.removeGroup(route.getGroupId())) { |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 1661 | log.warn("Faied to remove the tunnel {} at driver", |
| 1662 | tunnelId); |
Fahad Naeem Khan | 4ff83d6 | 2014-10-28 16:18:07 -0700 | [diff] [blame^] | 1663 | return removeTunnelMessages.ERROR_DRIVER; |
| 1664 | } |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | tunnelTable.remove(tunnelId); |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 1669 | log.debug("Tunnel {} was removed successfully.", tunnelId); |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1670 | |
Fahad Naeem Khan | 4ff83d6 | 2014-10-28 16:18:07 -0700 | [diff] [blame^] | 1671 | return removeTunnelMessages.SUCCESS; |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1674 | // ************************************ |
| 1675 | // Utility functions |
| 1676 | // ************************************ |
| 1677 | |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1678 | /** |
| 1679 | * Get the destination Nodes of the adjacency Sid |
| 1680 | * |
| 1681 | * @param nodeId node ID of the adjacency Sid |
| 1682 | * @param adjacencySid adjacency Sid |
| 1683 | * @return List of Switch, empty list if not found |
| 1684 | */ |
| 1685 | private List<Switch> getAdjacencyDestinationNode(String nodeId, String adjacencySid) { |
| 1686 | List<Switch> dstSwList = new ArrayList<Switch>(); |
| 1687 | |
| 1688 | HashMap<Integer, List<Integer>> adjacencySidInfo = |
| 1689 | adjacencySidTable.get(Integer.valueOf(nodeId)); |
| 1690 | List<Integer> ports = adjacencySidInfo.get(Integer.valueOf(adjacencySid)); |
| 1691 | Switch srcSw = getSwitchFromNodeId(nodeId); |
| 1692 | for (Integer port: ports) { |
| 1693 | for (Link link: srcSw.getOutgoingLinks()) { |
| 1694 | if (link.getSrcPort().getPortNumber().value() == port) { |
| 1695 | dstSwList.add(link.getDstSwitch()); |
| 1696 | } |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | return dstSwList; |
| 1701 | |
| 1702 | } |
| 1703 | |
| 1704 | /** |
| 1705 | * Get the DPID of the router with node ID IF the node ID is the neighbor of the |
| 1706 | * Switch srcSW. |
| 1707 | * If the nodeId is the adjacency Sid, then it returns the destination router DPIDs. |
| 1708 | * |
| 1709 | * @param nodeId Node ID to check |
| 1710 | * @param srcSw target Switch |
| 1711 | * @return List of DPID of nodeId, empty list if the nodeId is not the neighbor of srcSW |
| 1712 | */ |
| 1713 | private List<Dpid> getDpidIfNeighborOf(String nodeId, Switch srcSw) { |
| 1714 | List<Dpid> fwdSws = new ArrayList<Dpid>(); |
| 1715 | // if the nodeID is the adjacency ID, then we need to regard it as the |
| 1716 | // neighbor node ID and need to return the destination router DPID(s) |
| 1717 | if (isAdjacencySid(nodeId)) { |
| 1718 | String srcNodeId = this.getMplsLabel(srcSw.getDpid().toString()); |
| 1719 | HashMap<Integer, List<Integer>> adjacencySidInfo = |
| 1720 | adjacencySidTable.get(Integer.valueOf(srcNodeId)); |
| 1721 | List<Integer> ports = adjacencySidInfo.get(Integer.valueOf(nodeId)); |
| 1722 | |
| 1723 | for (Integer port: ports) { |
| 1724 | for (Link link: srcSw.getOutgoingLinks()) { |
| 1725 | if (link.getSrcPort().getPortNumber().value() == port) { |
| 1726 | fwdSws.add(link.getDstSwitch().getDpid()); |
| 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | } |
| 1731 | else { |
| 1732 | List<Dpid> fwdSwDpids = getForwardingSwitchForNodeId(srcSw,nodeId); |
| 1733 | if (fwdSwDpids == null || fwdSwDpids.isEmpty()) { |
| 1734 | log.warn("There is no route from node {} to node {}", |
| 1735 | srcSw.getDpid(), nodeId); |
Sangho Shin | 5590871 | 2014-10-27 15:16:48 -0700 | [diff] [blame] | 1736 | return fwdSws; |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | for (Dpid dpid: fwdSwDpids) { |
| 1740 | if (getMplsLabel(dpid.toString()).toString().equals(nodeId)) { |
| 1741 | fwdSws.add(dpid); |
| 1742 | break; |
| 1743 | } |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | return fwdSws; |
| 1748 | } |
| 1749 | |
| 1750 | /** |
Sangho Shin | 5590871 | 2014-10-27 15:16:48 -0700 | [diff] [blame] | 1751 | * Get port numbers of the neighbor set. |
| 1752 | * If ECMP in transit router is not supported, then only one port should be returned |
| 1753 | * regardless of number of nodes in neighbor set. |
Sangho Shin | 1a692c0 | 2014-10-23 17:05:41 -0700 | [diff] [blame] | 1754 | * |
| 1755 | * @param srcSwDpid source switch |
| 1756 | * @param ns Neighbor set of the switch |
| 1757 | * @return List of PortNumber, null if not found |
| 1758 | */ |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1759 | private List<PortNumber> getPortsFromNeighborSet(String srcSwDpid, NeighborSet ns) { |
| 1760 | |
| 1761 | List<PortNumber> portList = new ArrayList<PortNumber>(); |
| 1762 | Switch srcSwitch = mutableTopology.getSwitch(new Dpid(srcSwDpid)); |
| 1763 | if (srcSwitch == null) |
| 1764 | return null; |
Sangho Shin | f66aa26 | 2014-10-27 16:03:42 -0700 | [diff] [blame] | 1765 | IOF13Switch srcSwitch13 = (IOF13Switch)floodlightProvider.getMasterSwitch( |
| 1766 | getSwId(srcSwitch.getDpid().toString())); |
| 1767 | |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1768 | for (Dpid neighborDpid: ns.getDpids()) { |
Sangho Shin | f66aa26 | 2014-10-27 16:03:42 -0700 | [diff] [blame] | 1769 | if (srcSwitch13 instanceof OFSwitchImplDellOSR && |
| 1770 | ns.getDpids().size() == 1) { |
Sangho Shin | 5590871 | 2014-10-27 15:16:48 -0700 | [diff] [blame] | 1771 | Switch dstSwitch = mutableTopology.getSwitch(neighborDpid); |
| 1772 | if (isTransitRouter(srcSwitch) && isTransitRouter(dstSwitch)) { |
| 1773 | Link link = srcSwitch.getLinkToNeighbor(neighborDpid); |
| 1774 | portList.add(link.getSrcPort().getNumber()); |
| 1775 | break; |
| 1776 | } |
| 1777 | } |
| 1778 | else { |
| 1779 | for (Link link: srcSwitch.getOutgoingLinks()) { |
| 1780 | if (link.getDstSwitch().getDpid().equals(neighborDpid)) { |
| 1781 | portList.add(link.getSrcPort().getNumber()); |
| 1782 | } |
| 1783 | } |
| 1784 | } |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | return portList; |
| 1788 | } |
| 1789 | |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 1790 | /** |
| 1791 | * Check whether the router with preNodeid is connected to the router |
| 1792 | * with nodeId via adjacencySid or not |
| 1793 | * |
| 1794 | * @param prevNodeId the router node ID of the adjacencySid |
| 1795 | * @param adjacencySid adjacency SID |
| 1796 | * @param nodeId the router node ID to check |
| 1797 | * @return |
| 1798 | */ |
| 1799 | private boolean isAdjacencySidNeighborOf(String prevNodeId, String adjacencySid, String nodeId) { |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1800 | |
| 1801 | HashMap<Integer, List<Integer>> adjacencySidInfo = adjacencySidTable.get(Integer.valueOf(prevNodeId)); |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 1802 | List<Integer> ports = adjacencySidInfo.get(Integer.valueOf(adjacencySid)); |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1803 | |
| 1804 | for (Integer port: ports) { |
| 1805 | Switch sw = getSwitchFromNodeId(prevNodeId); |
| 1806 | for (Link link: sw.getOutgoingLinks()) { |
| 1807 | if (link.getSrcPort().getPortNumber().value() == port) { |
| 1808 | if (getMplsLabel(link.getDstPort().getDpid().toString()).equals(nodeId)) { |
| 1809 | return true; |
| 1810 | } |
| 1811 | } |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | return false; |
| 1816 | } |
| 1817 | |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 1818 | /** |
| 1819 | * Check if the node ID is the adjacency ID or not |
| 1820 | * |
| 1821 | * @param nodeId to check |
| 1822 | * @return true if the node ID is the adjacency ID, false otherwise |
| 1823 | */ |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1824 | private boolean isAdjacencySid(String nodeId) { |
| 1825 | // XXX The rule might change |
| 1826 | if (Integer.parseInt(nodeId) > 10000) |
| 1827 | return true; |
| 1828 | |
| 1829 | return false; |
| 1830 | } |
| 1831 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1832 | /** |
Sangho Shin | ced05b6 | 2014-10-22 11:23:14 -0700 | [diff] [blame] | 1833 | * Returns the Adjacency IDs for the node |
| 1834 | * |
| 1835 | * @param nodeSid Node SID |
Sangho Shin | cfef392 | 2014-10-22 12:04:16 -0700 | [diff] [blame] | 1836 | * @return Collection of Adjacency ID |
Sangho Shin | ced05b6 | 2014-10-22 11:23:14 -0700 | [diff] [blame] | 1837 | */ |
Sangho Shin | cfef392 | 2014-10-22 12:04:16 -0700 | [diff] [blame] | 1838 | public Collection<Integer> getAdjacencyIds(int nodeSid) { |
| 1839 | HashMap<Integer, List<Integer>> adjecencyInfo = |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1840 | adjacencySidTable.get(Integer.valueOf(nodeSid)); |
Sangho Shin | cfef392 | 2014-10-22 12:04:16 -0700 | [diff] [blame] | 1841 | |
| 1842 | return adjecencyInfo.keySet(); |
| 1843 | } |
| 1844 | |
| 1845 | /** |
| 1846 | * Returns the Adjacency Info for the node |
| 1847 | * |
| 1848 | * @param nodeSid Node SID |
| 1849 | * @return HashMap of <AdjacencyID, list of ports> |
| 1850 | */ |
| 1851 | public HashMap<Integer, List<Integer>> getAdjacencyInfo(int nodeSid) { |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 1852 | return adjacencySidTable.get(Integer.valueOf(nodeSid)); |
Sangho Shin | cfef392 | 2014-10-22 12:04:16 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 1855 | /** |
| 1856 | * Parse the adjacency jason string and build the adjacency Table |
| 1857 | * |
| 1858 | * @param adjInfo adjacency info jason string |
| 1859 | * @return HashMap<Adjacency ID, List of ports> object |
| 1860 | * @throws JSONException |
| 1861 | */ |
| 1862 | private HashMap<Integer, List<Integer>> parseAdjacencySidInfo(String adjInfo) |
| 1863 | throws JSONException { |
Sangho Shin | cfef392 | 2014-10-22 12:04:16 -0700 | [diff] [blame] | 1864 | JSONArray arry = new JSONArray(adjInfo); |
| 1865 | HashMap<Integer, List<Integer>> AdjacencyInfo = |
| 1866 | new HashMap<Integer, List<Integer>>(); |
| 1867 | |
| 1868 | for (int i = 0; i < arry.length(); i++) { |
| 1869 | Integer adjId = (Integer) arry.getJSONObject(i).get("adjSid"); |
| 1870 | JSONArray portNos = (JSONArray) arry.getJSONObject(i).get("ports"); |
| 1871 | if (adjId == null || portNos == null) |
| 1872 | continue; |
| 1873 | |
| 1874 | List<Integer> portNoList = new ArrayList<Integer>(); |
| 1875 | for (int j = 0; j < portNos.length(); j++) { |
Sangho Shin | 6232558 | 2014-10-24 10:36:09 -0700 | [diff] [blame] | 1876 | portNoList.add(Integer.valueOf(portNos.getInt(j))); |
Sangho Shin | cfef392 | 2014-10-22 12:04:16 -0700 | [diff] [blame] | 1877 | } |
| 1878 | AdjacencyInfo.put(adjId, portNoList); |
| 1879 | } |
| 1880 | return AdjacencyInfo; |
Sangho Shin | ced05b6 | 2014-10-22 11:23:14 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | /** |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 1884 | * Build the MatchActionOperationEntry according to the flag |
| 1885 | * |
| 1886 | * @param sw node ID to push for MPLS label |
| 1887 | * @param mplsLabel List of Switch DPIDs to forwards packets to |
| 1888 | * @param fwdSws PHP flag |
| 1889 | * @param Bos BoS flag |
| 1890 | * @param isTransitRouter |
| 1891 | * @return MatchiACtionOperationEntry object |
| 1892 | */ |
| 1893 | private MatchActionOperationEntry buildMAEntry(Switch sw, |
| 1894 | String mplsLabel, List<String> fwdSws, boolean php, |
| 1895 | boolean Bos) { |
| 1896 | MplsMatch mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel), Bos); |
| 1897 | List<Action> actions = new ArrayList<Action>(); |
| 1898 | |
| 1899 | PopMplsAction popActionBos = new PopMplsAction(EthType.IPv4); |
| 1900 | PopMplsAction popAction = new PopMplsAction(EthType.MPLS_UNICAST); |
| 1901 | CopyTtlInAction copyTtlInAction = new CopyTtlInAction(); |
| 1902 | DecNwTtlAction decNwTtlAction = new DecNwTtlAction(1); |
| 1903 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
| 1904 | |
| 1905 | if (php) { |
| 1906 | actions.add(copyTtlInAction); |
| 1907 | if (Bos) { |
| 1908 | actions.add(popActionBos); |
| 1909 | actions.add(decNwTtlAction); |
| 1910 | } |
| 1911 | else { |
| 1912 | actions.add(popAction); |
| 1913 | actions.add(decMplsTtlAction); |
| 1914 | } |
| 1915 | } |
| 1916 | else { |
| 1917 | actions.add(decMplsTtlAction); |
| 1918 | } |
| 1919 | |
Sangho Shin | 402354e | 2014-10-27 16:53:05 -0700 | [diff] [blame] | 1920 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 1921 | getSwId(sw.getDpid().toString())); |
| 1922 | if ((sw13 instanceof OFSwitchImplDellOSR) && isTransitRouter(sw) && !php) { |
Sangho Shin | e842cad | 2014-10-24 16:07:35 -0700 | [diff] [blame] | 1923 | PortNumber port = pickOnePort(sw, fwdSws); |
| 1924 | if (port == null) { |
| 1925 | log.warn("Failed to get a port from NeightborSet"); |
| 1926 | return null; |
| 1927 | } |
| 1928 | OutputAction outputAction = new OutputAction(port); |
| 1929 | Switch destSwitch = |
| 1930 | mutableTopology.getSwitch(new Dpid(fwdSws.get(0))); |
| 1931 | MacAddress srcMac = |
| 1932 | MacAddress.of(sw.getStringAttribute("routerMac")); |
| 1933 | MacAddress dstMac = |
| 1934 | MacAddress.of(destSwitch.getStringAttribute("routerMac")); |
| 1935 | SetSAAction setSAAction = new SetSAAction(srcMac); |
| 1936 | SetDAAction setDAAction = new SetDAAction(dstMac); |
| 1937 | actions.add(outputAction); |
| 1938 | actions.add(setSAAction); |
| 1939 | actions.add(setDAAction); |
| 1940 | } |
| 1941 | else { |
| 1942 | GroupAction groupAction = new GroupAction(); |
| 1943 | for (String dpid: fwdSws) |
| 1944 | groupAction.addSwitch(new Dpid(dpid)); |
| 1945 | actions.add(groupAction); |
| 1946 | } |
| 1947 | |
| 1948 | MatchAction matchAction = new MatchAction(new MatchActionId(matchActionId++), |
| 1949 | new SwitchPort((long) 0, (short) 0), mplsMatch, actions); |
| 1950 | Operator operator = Operator.ADD; |
| 1951 | MatchActionOperationEntry maEntry = |
| 1952 | new MatchActionOperationEntry(operator, matchAction); |
| 1953 | |
| 1954 | return maEntry; |
| 1955 | } |
| 1956 | |
| 1957 | /** |
| 1958 | * Pick a router from the neighbor set and return the port |
| 1959 | * connected to the router. |
| 1960 | * |
| 1961 | * @param sw source switch |
| 1962 | * @param fwdSwDpids neighbor set of the switch |
| 1963 | * @return PortNumber connected to one of the neighbors |
| 1964 | */ |
| 1965 | private PortNumber pickOnePort(Switch sw, List<String> fwdSwDpids) { |
| 1966 | for (Link link: sw.getOutgoingLinks()) { |
| 1967 | if (link.getDstSwitch().getDpid().toString().equals(fwdSwDpids.get(0))) |
| 1968 | return link.getSrcPort().getNumber(); |
| 1969 | } |
| 1970 | |
| 1971 | return null; |
| 1972 | } |
| 1973 | |
| 1974 | /** |
| 1975 | * check if the router is the transit router or not |
| 1976 | * |
| 1977 | * @param sw router switch to check |
| 1978 | * @return true if the switch is the transit router, false otherwise |
| 1979 | */ |
| 1980 | private boolean isTransitRouter(Switch sw) { |
| 1981 | int i = 0; |
| 1982 | for(Switch neighbor: sw.getNeighbors()) { |
| 1983 | i++; |
| 1984 | } |
| 1985 | if (i > 1) |
| 1986 | return true; |
| 1987 | else |
| 1988 | return false; |
| 1989 | } |
| 1990 | |
| 1991 | /** |
Sangho Shin | 5590871 | 2014-10-27 15:16:48 -0700 | [diff] [blame] | 1992 | * Get the forwarding Switch DPIDs to send packets to a node. |
| 1993 | * If ECMP in transit routers is not supported, only one switch needs to be |
| 1994 | * selected as the neighbor set to forward packets to. |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1995 | * |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1996 | * @param srcSw source switch |
| 1997 | * @param nodeId destination node Id |
| 1998 | * @return list of switch DPID to forward packets to |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1999 | */ |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2000 | private List<Dpid> getForwardingSwitchForNodeId(Switch srcSw, String nodeId) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 2001 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2002 | List<Dpid> fwdSws = new ArrayList<Dpid>(); |
| 2003 | Switch destSw = null; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 2004 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2005 | destSw = getSwitchFromNodeId(nodeId); |
| 2006 | |
| 2007 | if (destSw == null) { |
| 2008 | log.debug("Cannot find the switch with ID {}", nodeId); |
| 2009 | return null; |
| 2010 | } |
| 2011 | |
| 2012 | ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(srcSw); |
| 2013 | |
| 2014 | HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>> switchVia = |
| 2015 | ecmpSPG.getAllLearnedSwitchesAndVia(); |
| 2016 | for (Integer itrIdx : switchVia.keySet()) { |
| 2017 | HashMap<Switch, ArrayList<ArrayList<Dpid>>> swViaMap = |
| 2018 | switchVia.get(itrIdx); |
| 2019 | for (Switch targetSw : swViaMap.keySet()) { |
| 2020 | String destSwDpid = destSw.getDpid().toString(); |
| 2021 | if (targetSw.getDpid().toString().equals(destSwDpid)) { |
| 2022 | for (ArrayList<Dpid> via : swViaMap.get(targetSw)) { |
| 2023 | if (via.isEmpty()) { |
| 2024 | fwdSws.add(destSw.getDpid()); |
| 2025 | } |
| 2026 | else { |
Sangho Shin | a000c61 | 2014-10-21 14:17:59 -0700 | [diff] [blame] | 2027 | Dpid firstVia = via.get(via.size()-1); |
| 2028 | fwdSws.add(firstVia); |
Sangho Shin | f66aa26 | 2014-10-27 16:03:42 -0700 | [diff] [blame] | 2029 | IOF13Switch targetSw13 = (IOF13Switch)floodlightProvider.getMasterSwitch( |
| 2030 | getSwId(targetSw.getDpid().toString())); |
| 2031 | if (targetSw13 instanceof OFSwitchImplDellOSR && |
Sangho Shin | 5590871 | 2014-10-27 15:16:48 -0700 | [diff] [blame] | 2032 | isTransitRouter(targetSw) && |
| 2033 | isTransitRouter(mutableTopology.getSwitch(firstVia))) { |
| 2034 | return fwdSws; |
| 2035 | } |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2036 | } |
| 2037 | } |
| 2038 | } |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | return fwdSws; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 2043 | } |
| 2044 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2045 | /** |
| 2046 | * Get switch for the node Id specified |
| 2047 | * |
| 2048 | * @param nodeId node ID for switch |
| 2049 | * @return Switch |
| 2050 | */ |
| 2051 | private Switch getSwitchFromNodeId(String nodeId) { |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 2052 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2053 | for (Switch sw : mutableTopology.getSwitches()) { |
| 2054 | String id = sw.getStringAttribute("nodeSid"); |
| 2055 | if (id.equals(nodeId)) { |
| 2056 | return sw; |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | return null; |
| 2061 | } |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 2062 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 2063 | /** |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2064 | * Convert a string DPID to its Switch Id (integer) |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 2065 | * |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2066 | * @param dpid |
| 2067 | * @return |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 2068 | */ |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2069 | private long getSwId(String dpid) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 2070 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2071 | long swId = 0; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 2072 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2073 | String swIdHexStr = "0x"+dpid.substring(dpid.lastIndexOf(":") + 1); |
| 2074 | if (swIdHexStr != null) |
| 2075 | swId = Integer.decode(swIdHexStr); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 2076 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2077 | return swId; |
| 2078 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 2079 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2080 | /** |
| 2081 | * Check if the switch is the edge router or not. |
| 2082 | * |
| 2083 | * @param dpid Dpid of the switch to check |
| 2084 | * @return true if it is an edge router, otherwise false |
| 2085 | */ |
| 2086 | private boolean IsEdgeRouter(String dpid) { |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 2087 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2088 | for (Switch sw : mutableTopology.getSwitches()) { |
| 2089 | String dpidStr = sw.getDpid().toString(); |
| 2090 | if (dpid.equals(dpidStr)) { |
| 2091 | /* |
| 2092 | String subnetInfo = sw.getStringAttribute("subnets"); |
| 2093 | if (subnetInfo == null || subnetInfo.equals("[]")) { |
| 2094 | return false; |
| 2095 | } |
| 2096 | else |
| 2097 | return true; |
| 2098 | */ |
| 2099 | String isEdge = sw.getStringAttribute("isEdgeRouter"); |
| 2100 | if (isEdge != null) { |
| 2101 | if (isEdge.equals("true")) |
| 2102 | return true; |
| 2103 | else |
| 2104 | return false; |
| 2105 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2109 | return false; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | /** |
| 2113 | * Get MPLS label reading the config file |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 2114 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 2115 | * @param dipid DPID of the switch |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 2116 | * @return MPLS label for the switch |
| 2117 | */ |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 2118 | private String getMplsLabel(String dpid) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 2119 | |
| 2120 | String mplsLabel = null; |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 2121 | for (Switch sw : mutableTopology.getSwitches()) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 2122 | String dpidStr = sw.getDpid().toString(); |
| 2123 | if (dpid.equals(dpidStr)) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 2124 | mplsLabel = sw.getStringAttribute("nodeSid"); |
| 2125 | break; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 2126 | } |
| 2127 | } |
| 2128 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 2129 | return mplsLabel; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 2130 | } |
| 2131 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 2132 | /** |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 2133 | * The function checks if given IP matches to the given subnet mask |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 2134 | * |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 2135 | * @param addr - subnet address to match |
| 2136 | * @param addr1 - IP address to check |
| 2137 | * @return true if the IP address matches to the subnet, otherwise false |
| 2138 | */ |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 2139 | public boolean netMatch(String addr, String addr1) { // addr is subnet |
| 2140 | // address and addr1 is |
| 2141 | // ip address. Function |
| 2142 | // will return true, if |
| 2143 | // addr1 is within |
| 2144 | // addr(subnet) |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 2145 | |
| 2146 | String[] parts = addr.split("/"); |
| 2147 | String ip = parts[0]; |
| 2148 | int prefix; |
| 2149 | |
| 2150 | if (parts.length < 2) { |
| 2151 | prefix = 0; |
| 2152 | } else { |
| 2153 | prefix = Integer.parseInt(parts[1]); |
| 2154 | } |
| 2155 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 2156 | Inet4Address a = null; |
| 2157 | Inet4Address a1 = null; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 2158 | try { |
| 2159 | a = (Inet4Address) InetAddress.getByName(ip); |
| 2160 | a1 = (Inet4Address) InetAddress.getByName(addr1); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 2161 | } catch (UnknownHostException e) { |
| 2162 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 2163 | |
| 2164 | byte[] b = a.getAddress(); |
| 2165 | int ipInt = ((b[0] & 0xFF) << 24) | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 2166 | ((b[1] & 0xFF) << 16) | |
| 2167 | ((b[2] & 0xFF) << 8) | |
| 2168 | ((b[3] & 0xFF) << 0); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 2169 | |
| 2170 | byte[] b1 = a1.getAddress(); |
| 2171 | int ipInt1 = ((b1[0] & 0xFF) << 24) | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 2172 | ((b1[1] & 0xFF) << 16) | |
| 2173 | ((b1[2] & 0xFF) << 8) | |
| 2174 | ((b1[3] & 0xFF) << 0); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 2175 | |
| 2176 | int mask = ~((1 << (32 - prefix)) - 1); |
| 2177 | |
| 2178 | if ((ipInt & mask) == (ipInt1 & mask)) { |
| 2179 | return true; |
| 2180 | } |
| 2181 | else { |
| 2182 | return false; |
| 2183 | } |
| 2184 | } |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 2185 | |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 2186 | /** |
| 2187 | * Add a routing rule for the host |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 2188 | * |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 2189 | * @param sw - Switch to add the rule |
| 2190 | * @param hostIpAddress Destination host IP address |
| 2191 | * @param hostMacAddress Destination host MAC address |
| 2192 | */ |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 2193 | public void addRouteToHost(Switch sw, int hostIpAddress, byte[] hostMacAddress) { |
| 2194 | ipHandler.addRouteToHost(sw, hostIpAddress, hostMacAddress); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 2195 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 2196 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 2197 | /** |
| 2198 | * Add IP packet to a buffer queue |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 2199 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 2200 | * @param ipv4 |
| 2201 | */ |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2202 | public void addPacketToPacketBuffer(IPv4 ipv4) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 2203 | ipPacketQueue.add(ipv4); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | /** |
| 2207 | * Retrieve all packets whose destination is the given address. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 2208 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 2209 | * @param destIp Destination address of packets to retrieve |
| 2210 | */ |
| 2211 | public List<IPv4> getIpPacketFromQueue(byte[] destIp) { |
| 2212 | |
| 2213 | List<IPv4> bufferedPackets = new ArrayList<IPv4>(); |
| 2214 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 2215 | if (!ipPacketQueue.isEmpty()) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 2216 | for (IPv4 ip : ipPacketQueue) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 2217 | int dest = ip.getDestinationAddress(); |
| 2218 | IPv4Address ip1 = IPv4Address.of(dest); |
| 2219 | IPv4Address ip2 = IPv4Address.of(destIp); |
| 2220 | if (ip1.equals(ip2)) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 2221 | bufferedPackets.add((IPv4) (ipPacketQueue.poll()).clone()); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | return bufferedPackets; |
| 2227 | } |
| 2228 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2229 | /** |
| 2230 | * Get MAC address to known hosts |
| 2231 | * |
| 2232 | * @param destinationAddress IP address to get MAC address |
| 2233 | * @return MAC Address to given IP address |
| 2234 | */ |
| 2235 | public byte[] getMacAddressFromIpAddress(int destinationAddress) { |
| 2236 | |
| 2237 | // Can't we get the host IP address from the TopologyService ?? |
| 2238 | |
| 2239 | Iterator<ArpEntry> iterator = arpEntries.iterator(); |
| 2240 | |
| 2241 | IPv4Address ipAddress = IPv4Address.of(destinationAddress); |
| 2242 | byte[] ipAddressInByte = ipAddress.getBytes(); |
| 2243 | |
| 2244 | while (iterator.hasNext()) { |
| 2245 | ArpEntry arpEntry = iterator.next(); |
| 2246 | byte[] address = arpEntry.targetIpAddress; |
| 2247 | |
| 2248 | IPv4Address a = IPv4Address.of(address); |
| 2249 | IPv4Address b = IPv4Address.of(ipAddressInByte); |
| 2250 | |
| 2251 | if (a.equals(b)) { |
| 2252 | log.debug("Found an arp entry"); |
| 2253 | return arpEntry.targetMacAddress; |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | return null; |
| 2258 | } |
| 2259 | |
| 2260 | /** |
| 2261 | * Send an ARP request via ArpHandler |
| 2262 | * |
| 2263 | * @param destinationAddress |
| 2264 | * @param sw |
| 2265 | * @param inPort |
| 2266 | * |
| 2267 | */ |
| 2268 | public void sendArpRequest(Switch sw, int destinationAddress, Port inPort) { |
| 2269 | arpHandler.sendArpRequest(sw, destinationAddress, inPort); |
| 2270 | } |
| 2271 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2272 | // ************************************ |
| 2273 | // Test functions |
| 2274 | // ************************************ |
| 2275 | |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2276 | private void runTest() { |
| 2277 | |
| 2278 | if (testMode == POLICY_ADD1) { |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 2279 | Integer[] routeArray = {101, 105, 110}; |
| 2280 | /*List<Dpid> routeList = new ArrayList<Dpid>(); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2281 | for (int i = 0; i < routeArray.length; i++) { |
| 2282 | Dpid dpid = getSwitchFromNodeId(routeArray[i]).getDpid(); |
| 2283 | routeList.add(dpid); |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 2284 | }*/ |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2285 | |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 2286 | if (createTunnel("1", Arrays.asList(routeArray))) { |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2287 | IPv4Net srcIp = new IPv4Net("10.0.1.1/24"); |
| 2288 | IPv4Net dstIp = new IPv4Net("10.1.2.1/24"); |
| 2289 | |
| 2290 | log.debug("Set the policy 1"); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 2291 | this.createPolicy("1", null, null, Ethernet.TYPE_IPV4, srcIp, |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2292 | dstIp, IPv4.PROTOCOL_ICMP, (short)-1, (short)-1, 10000, |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 2293 | "1"); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2294 | testMode = POLICY_ADD2; |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 2295 | testTask.reschedule(5, TimeUnit.SECONDS); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2296 | } |
| 2297 | else { |
| 2298 | // retry it |
| 2299 | testTask.reschedule(5, TimeUnit.SECONDS); |
| 2300 | } |
| 2301 | } |
| 2302 | else if (testMode == POLICY_ADD2) { |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 2303 | Integer[] routeArray = {101, 102, 103, 104, 105, 108, 110}; |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2304 | |
Srikanth Vavilapalli | 1f6a574 | 2014-10-21 13:45:11 -0700 | [diff] [blame] | 2305 | if (createTunnel("2", Arrays.asList(routeArray))) { |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2306 | IPv4Net srcIp = new IPv4Net("10.0.1.1/24"); |
| 2307 | IPv4Net dstIp = new IPv4Net("10.1.2.1/24"); |
| 2308 | |
| 2309 | log.debug("Set the policy 2"); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 2310 | this.createPolicy("2", null, null, Ethernet.TYPE_IPV4, srcIp, |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2311 | dstIp, IPv4.PROTOCOL_ICMP, (short)-1, (short)-1, 20000, |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 2312 | "2"); |
Fahad Naeem Khan | 95aa401 | 2014-10-21 14:07:00 -0700 | [diff] [blame] | 2313 | //testMode = POLICY_REMOVE2; |
| 2314 | //testTask.reschedule(5, TimeUnit.SECONDS); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2315 | } |
| 2316 | else { |
| 2317 | log.debug("Retry it"); |
| 2318 | testTask.reschedule(5, TimeUnit.SECONDS); |
| 2319 | } |
| 2320 | } |
| 2321 | else if (testMode == POLICY_REMOVE2){ |
| 2322 | log.debug("Remove the policy 2"); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 2323 | this.removePolicy("2"); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2324 | testMode = POLICY_REMOVE1; |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 2325 | testTask.reschedule(5, TimeUnit.SECONDS); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2326 | } |
| 2327 | else if (testMode == POLICY_REMOVE1){ |
| 2328 | log.debug("Remove the policy 1"); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 2329 | this.removePolicy("1"); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2330 | |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 2331 | testMode = TUNNEL_REMOVE1; |
| 2332 | testTask.reschedule(5, TimeUnit.SECONDS); |
| 2333 | } |
| 2334 | else if (testMode == TUNNEL_REMOVE1) { |
| 2335 | log.debug("Remove the tunnel 1"); |
| 2336 | this.removeTunnel("1"); |
| 2337 | |
| 2338 | testMode = TUNNEL_REMOVE2; |
| 2339 | testTask.reschedule(5, TimeUnit.SECONDS); |
| 2340 | } |
| 2341 | else if (testMode == TUNNEL_REMOVE2) { |
| 2342 | log.debug("Remove the tunnel 2"); |
| 2343 | this.removeTunnel("2"); |
| 2344 | log.debug("The end of test"); |
| 2345 | } |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 2346 | } |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2347 | |
| 2348 | private void runTest1() { |
| 2349 | |
| 2350 | String dpid1 = "00:00:00:00:00:00:00:01"; |
| 2351 | String dpid2 = "00:00:00:00:00:00:00:0a"; |
| 2352 | Switch srcSw = mutableTopology.getSwitch(new Dpid(dpid1)); |
| 2353 | Switch dstSw = mutableTopology.getSwitch(new Dpid(dpid2)); |
| 2354 | |
| 2355 | if (srcSw == null || dstSw == null) { |
| 2356 | testTask.reschedule(1, TimeUnit.SECONDS); |
| 2357 | log.debug("Switch is gone. Reschedule the test"); |
| 2358 | return; |
| 2359 | } |
| 2360 | |
| 2361 | String[] routeArray = {"101", "102", "105", "108", "110"}; |
| 2362 | List<String> routeList = new ArrayList<String>(); |
| 2363 | for (int i = 0; i < routeArray.length; i++) |
| 2364 | routeList.add(routeArray[i]); |
| 2365 | |
| 2366 | List<String> optimizedRoute = this.getOptimizedPath(srcSw, dstSw, routeList); |
| 2367 | |
| 2368 | log.debug("Test set is {}", routeList.toString()); |
| 2369 | log.debug("Result set is {}", optimizedRoute.toString()); |
| 2370 | |
| 2371 | |
| 2372 | } |
| 2373 | |
| 2374 | /** |
| 2375 | * print tunnel info - used only for debugging. |
| 2376 | * @param targetSw |
| 2377 | * |
| 2378 | * @param fwdSwDpids |
| 2379 | * @param ids |
| 2380 | * @param tunnelId |
| 2381 | */ |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 2382 | private void printTunnelInfo(String targetSw, String tunnelId, |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2383 | List<String> ids, NeighborSet ns) { |
| 2384 | StringBuilder logStr = new StringBuilder("In switch " + |
Sangho Shin | 6471d20 | 2014-10-23 10:59:36 -0700 | [diff] [blame] | 2385 | targetSw + ", create a tunnel " + tunnelId + " " + " of push "); |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2386 | for (String id: ids) |
| 2387 | logStr.append(id + "-"); |
| 2388 | logStr.append(" output to "); |
| 2389 | for (Dpid dpid: ns.getDpids()) |
| 2390 | logStr.append(dpid + " - "); |
| 2391 | |
| 2392 | log.debug(logStr.toString()); |
| 2393 | |
| 2394 | } |
| 2395 | |
| 2396 | /** |
| 2397 | * Debugging function to print out the Match Action Entry |
| 2398 | * @param sw13 |
| 2399 | * |
| 2400 | * @param maEntry |
| 2401 | */ |
| 2402 | private void printMatchActionOperationEntry( |
| 2403 | IOF13Switch sw13, MatchActionOperationEntry maEntry) { |
| 2404 | |
| 2405 | StringBuilder logStr = new StringBuilder("In switch " + sw13.getId() + ", "); |
| 2406 | |
| 2407 | MatchAction ma = maEntry.getTarget(); |
| 2408 | Match m = ma.getMatch(); |
| 2409 | List<Action> actions = ma.getActions(); |
| 2410 | |
| 2411 | if (m instanceof Ipv4Match) { |
| 2412 | logStr.append("If the IP matches with "); |
| 2413 | IPv4Net ip = ((Ipv4Match) m).getDestination(); |
| 2414 | logStr.append(ip.toString()); |
| 2415 | logStr.append(" then "); |
| 2416 | } |
| 2417 | else if (m instanceof MplsMatch) { |
| 2418 | logStr.append("If the MPLS label matches with "); |
| 2419 | int mplsLabel = ((MplsMatch) m).getMplsLabel(); |
| 2420 | logStr.append(mplsLabel); |
| 2421 | logStr.append(" then "); |
| 2422 | } |
| 2423 | else if (m instanceof PacketMatch) { |
Sangho Shin | c5a38a0 | 2014-10-28 16:09:38 -0700 | [diff] [blame] | 2424 | logStr.append("if the policy match is XXX then "); |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | logStr.append(" do { "); |
| 2428 | for (Action action : actions) { |
| 2429 | if (action instanceof CopyTtlInAction) { |
| 2430 | logStr.append("copy ttl In, "); |
| 2431 | } |
| 2432 | else if (action instanceof CopyTtlOutAction) { |
| 2433 | logStr.append("copy ttl Out, "); |
| 2434 | } |
| 2435 | else if (action instanceof DecMplsTtlAction) { |
| 2436 | logStr.append("Dec MPLS TTL , "); |
| 2437 | } |
| 2438 | else if (action instanceof GroupAction) { |
| 2439 | logStr.append("Forward packet to < "); |
| 2440 | NeighborSet dpids = ((GroupAction) action).getDpids(); |
| 2441 | logStr.append(dpids.toString() + ","); |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2442 | } |
| 2443 | else if (action instanceof PopMplsAction) { |
| 2444 | logStr.append("Pop MPLS label, "); |
| 2445 | } |
| 2446 | else if (action instanceof PushMplsAction) { |
| 2447 | logStr.append("Push MPLS label, "); |
| 2448 | } |
| 2449 | else if (action instanceof SetMplsIdAction) { |
| 2450 | int id = ((SetMplsIdAction) action).getMplsId(); |
| 2451 | logStr.append("Set MPLS ID as " + id + ", "); |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | log.debug(logStr.toString()); |
| 2456 | |
| 2457 | } |
| 2458 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2459 | // ************************************ |
| 2460 | // Unused classes and functions |
| 2461 | // ************************************ |
| 2462 | |
| 2463 | /** |
| 2464 | * Temporary class to to keep ARP entry |
| 2465 | * |
| 2466 | */ |
| 2467 | private class ArpEntry { |
| 2468 | |
| 2469 | byte[] targetMacAddress; |
| 2470 | byte[] targetIpAddress; |
| 2471 | |
| 2472 | private ArpEntry(byte[] macAddress, byte[] ipAddress) { |
| 2473 | this.targetMacAddress = macAddress; |
| 2474 | this.targetIpAddress = ipAddress; |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | /** |
| 2479 | * This class is used only for link recovery optimization in |
| 2480 | * modifyEcmpRoutingRules() function. |
| 2481 | * TODO: please remove if the optimization is not used at all |
| 2482 | */ |
| 2483 | private class SwitchPair { |
| 2484 | private Switch src; |
| 2485 | private Switch dst; |
| 2486 | |
| 2487 | public SwitchPair(Switch src, Switch dst) { |
| 2488 | this.src = src; |
| 2489 | this.dst = dst; |
| 2490 | } |
| 2491 | |
| 2492 | public Switch getSource() { |
| 2493 | return src; |
| 2494 | } |
| 2495 | |
| 2496 | public Switch getDestination() { |
| 2497 | return dst; |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | /** |
| 2502 | * Update ARP Cache using ARP packets It is used to set destination MAC |
| 2503 | * address to forward packets to known hosts. But, it will be replace with |
| 2504 | * Host information of Topology service later. |
| 2505 | * |
| 2506 | * @param arp APR packets to use for updating ARP entries |
| 2507 | */ |
| 2508 | public void updateArpCache(ARP arp) { |
| 2509 | |
| 2510 | ArpEntry arpEntry = new ArpEntry(arp.getSenderHardwareAddress(), |
| 2511 | arp.getSenderProtocolAddress()); |
| 2512 | // TODO: Need to check the duplication |
| 2513 | arpEntries.add(arpEntry); |
| 2514 | } |
| 2515 | |
| 2516 | /** |
| 2517 | * Modify the routing rules for the lost links |
| 2518 | * - Recompute the path if the link failed is included in the path |
| 2519 | * (including src and dest). |
| 2520 | * |
| 2521 | * @param newLink |
| 2522 | */ |
| 2523 | private void modifyEcmpRoutingRules(LinkData linkRemoved) { |
| 2524 | |
| 2525 | //HashMap<Switch, SwitchPair> linksToRecompute = new HashMap<Switch, SwitchPair>(); |
| 2526 | Set<SwitchPair> linksToRecompute = new HashSet<SwitchPair>(); |
| 2527 | |
| 2528 | for (ECMPShortestPathGraph ecmpSPG : graphs.values()) { |
| 2529 | Switch rootSw = ecmpSPG.getRootSwitch(); |
| 2530 | HashMap<Integer, HashMap<Switch, ArrayList<Path>>> paths = |
| 2531 | ecmpSPG.getCompleteLearnedSwitchesAndPaths(); |
| 2532 | for (HashMap<Switch, ArrayList<Path>> p: paths.values()) { |
| 2533 | for (Switch destSw: p.keySet()) { |
| 2534 | ArrayList<Path> path = p.get(destSw); |
| 2535 | if (checkPath(path, linkRemoved)) { |
| 2536 | boolean found = false; |
| 2537 | for (SwitchPair pair: linksToRecompute) { |
| 2538 | if (pair.getSource().getDpid() == rootSw.getDpid() && |
| 2539 | pair.getSource().getDpid() == destSw.getDpid()) { |
| 2540 | found = true; |
| 2541 | } |
| 2542 | } |
| 2543 | if (!found) { |
| 2544 | linksToRecompute.add(new SwitchPair(rootSw, destSw)); |
| 2545 | } |
| 2546 | } |
| 2547 | } |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | // Recompute the path for the specific route |
| 2552 | for (SwitchPair pair: linksToRecompute) { |
| 2553 | |
| 2554 | log.debug("Recompute path from {} to {}", pair.getSource(), pair.getDestination()); |
| 2555 | // We need the following function for optimization |
| 2556 | //ECMPShortestPathGraph ecmpSPG = |
| 2557 | // new ECMPShortestPathGraph(pair.getSource(), pair.getDestination()); |
| 2558 | ECMPShortestPathGraph ecmpSPG = |
| 2559 | new ECMPShortestPathGraph(pair.getSource()); |
| 2560 | populateEcmpRoutingRulesForPath(pair.getSource(), ecmpSPG, true); |
| 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | /** |
| 2565 | * Optimize the mpls label |
| 2566 | * The feature will be used only for policy of "avoid a specific switch". |
| 2567 | * Check route to each router in route backward. |
| 2568 | * If there is only one route to the router and the routers are included in |
| 2569 | * the route, remove the id from the path. |
| 2570 | * A-B-C-D-E => A-B-C-D-E -> A-E |
| 2571 | * | | => A-B-H-I -> A-I |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 2572 | * F-G-H-I => A-D-I > A-D-I |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 2573 | */ |
| 2574 | private List<String> getOptimizedPath(Switch srcSw, Switch dstSw, List<String> route) { |
| 2575 | |
| 2576 | List<String> optimizedPath = new ArrayList<String>(); |
| 2577 | optimizedPath.addAll(route); |
| 2578 | ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(srcSw); |
| 2579 | |
| 2580 | HashMap<Integer, HashMap<Switch, ArrayList<Path>>> paths = |
| 2581 | ecmpSPG.getCompleteLearnedSwitchesAndPaths(); |
| 2582 | for (HashMap<Switch, ArrayList<Path>> p: paths.values()) { |
| 2583 | for (Switch s: p.keySet()) { |
| 2584 | if (s.getDpid().toString().equals(dstSw.getDpid().toString())) { |
| 2585 | ArrayList<Path> ecmpPaths = p.get(s); |
| 2586 | if (ecmpPaths!= null && ecmpPaths.size() == 1) { |
| 2587 | for (Path path: ecmpPaths) { |
| 2588 | for (LinkData link: path) { |
| 2589 | String srcId = getMplsLabel(link.getSrc().getDpid().toString()); |
| 2590 | String dstId = getMplsLabel(link.getSrc().getDpid().toString()); |
| 2591 | if (optimizedPath.contains(srcId)) { |
| 2592 | optimizedPath.remove(srcId); |
| 2593 | } |
| 2594 | if (optimizedPath.contains(dstId)) { |
| 2595 | optimizedPath.remove(dstId); |
| 2596 | } |
| 2597 | } |
| 2598 | } |
| 2599 | } |
| 2600 | } |
| 2601 | } |
| 2602 | } |
| 2603 | |
| 2604 | return optimizedPath; |
| 2605 | |
| 2606 | } |
| 2607 | |
| 2608 | /** |
| 2609 | * Check if the path is affected from the link removed |
| 2610 | * |
| 2611 | * @param path Path to check |
| 2612 | * @param linkRemoved link removed |
| 2613 | * @return true if the path contains the link removed |
| 2614 | */ |
| 2615 | private boolean checkPath(ArrayList<Path> path, LinkData linkRemoved) { |
| 2616 | |
| 2617 | for (Path ppp: path) { |
| 2618 | // TODO: need to check if this is a bidirectional or |
| 2619 | // unidirectional |
| 2620 | for (LinkData link: ppp) { |
| 2621 | if (link.getDst().getDpid().equals(linkRemoved.getDst().getDpid()) && |
| 2622 | link.getSrc().getDpid().equals(linkRemoved.getSrc().getDpid())) |
| 2623 | return true; |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | return false; |
| 2628 | } |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 2629 | |
| 2630 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 2631 | } |