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; |
| 8 | import java.util.Collection; |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 9 | import java.util.HashMap; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 10 | import java.util.HashSet; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 11 | import java.util.Iterator; |
| 12 | import java.util.List; |
| 13 | import java.util.Map; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 14 | import java.util.Set; |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 15 | import java.util.concurrent.ConcurrentLinkedQueue; |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 16 | import java.util.concurrent.ExecutionException; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 17 | import java.util.concurrent.ScheduledExecutorService; |
| 18 | import java.util.concurrent.TimeUnit; |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 19 | import java.util.concurrent.TimeoutException; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 20 | |
| 21 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 22 | import net.floodlightcontroller.core.IOF13Switch; |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 23 | import net.floodlightcontroller.core.IOF13Switch.NeighborSet; |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 24 | import net.floodlightcontroller.core.internal.OFBarrierReplyFuture; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 25 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 26 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 27 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 28 | import net.floodlightcontroller.core.module.IFloodlightService; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 29 | import net.floodlightcontroller.core.util.SingletonTask; |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 30 | import net.floodlightcontroller.restserver.IRestApiService; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 31 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 32 | import net.floodlightcontroller.util.MACAddress; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 33 | import net.onrc.onos.api.packet.IPacketListener; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 34 | import net.onrc.onos.api.packet.IPacketService; |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 35 | import net.onrc.onos.apps.segmentrouting.web.SegmentRoutingWebRoutable; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 36 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService; |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 37 | import net.onrc.onos.core.intent.Path; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 38 | import net.onrc.onos.core.main.config.IConfigInfoService; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 39 | import net.onrc.onos.core.matchaction.MatchAction; |
| 40 | import net.onrc.onos.core.matchaction.MatchActionId; |
| 41 | import net.onrc.onos.core.matchaction.MatchActionOperationEntry; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 42 | import net.onrc.onos.core.matchaction.MatchActionOperations.Operator; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 43 | import net.onrc.onos.core.matchaction.action.Action; |
| 44 | import net.onrc.onos.core.matchaction.action.CopyTtlInAction; |
| 45 | import net.onrc.onos.core.matchaction.action.CopyTtlOutAction; |
| 46 | import net.onrc.onos.core.matchaction.action.DecMplsTtlAction; |
| 47 | import net.onrc.onos.core.matchaction.action.DecNwTtlAction; |
| 48 | import net.onrc.onos.core.matchaction.action.GroupAction; |
| 49 | import net.onrc.onos.core.matchaction.action.PopMplsAction; |
| 50 | import net.onrc.onos.core.matchaction.action.PushMplsAction; |
| 51 | import net.onrc.onos.core.matchaction.action.SetMplsIdAction; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 52 | import net.onrc.onos.core.matchaction.match.Ipv4Match; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 53 | import net.onrc.onos.core.matchaction.match.Match; |
| 54 | import net.onrc.onos.core.matchaction.match.MplsMatch; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 55 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
| 56 | import net.onrc.onos.core.matchaction.match.PacketMatchBuilder; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 57 | import net.onrc.onos.core.packet.ARP; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 58 | import net.onrc.onos.core.packet.Ethernet; |
| 59 | import net.onrc.onos.core.packet.IPv4; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 60 | import net.onrc.onos.core.topology.ITopologyListener; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 61 | import net.onrc.onos.core.topology.ITopologyService; |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 62 | import net.onrc.onos.core.topology.Link; |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 63 | import net.onrc.onos.core.topology.LinkData; |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 64 | import net.onrc.onos.core.topology.MastershipData; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 65 | import net.onrc.onos.core.topology.MutableTopology; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 66 | import net.onrc.onos.core.topology.Port; |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 67 | import net.onrc.onos.core.topology.PortData; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 68 | import net.onrc.onos.core.topology.Switch; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 69 | import net.onrc.onos.core.topology.SwitchData; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 70 | import net.onrc.onos.core.topology.TopologyEvents; |
Srikanth Vavilapalli | 363f1dc | 2014-09-22 14:30:23 -0700 | [diff] [blame] | 71 | import net.onrc.onos.core.util.Dpid; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 72 | import net.onrc.onos.core.util.IPv4Net; |
| 73 | import net.onrc.onos.core.util.SwitchPort; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 74 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 75 | import org.json.JSONArray; |
| 76 | import org.json.JSONException; |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 77 | import org.projectfloodlight.openflow.protocol.OFBarrierReply; |
Saurav Das | bc594a4 | 2014-09-25 20:13:50 -0700 | [diff] [blame] | 78 | import org.projectfloodlight.openflow.types.EthType; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 79 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 80 | import org.slf4j.Logger; |
| 81 | import org.slf4j.LoggerFactory; |
| 82 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 83 | public class SegmentRoutingManager implements IFloodlightModule, |
Srikanth Vavilapalli | 8c49db7 | 2014-10-20 13:35:08 -0700 | [diff] [blame] | 84 | ITopologyListener, IPacketListener, ISegmentRoutingService { |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 85 | |
| 86 | private static final Logger log = LoggerFactory |
| 87 | .getLogger(SegmentRoutingManager.class); |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 88 | |
Fahad Naeem Khan | 5b558f2 | 2014-10-16 10:35:20 -0700 | [diff] [blame] | 89 | private ITopologyService topologyService; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 90 | private IPacketService packetService; |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 91 | private MutableTopology mutableTopology; |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 92 | private ConcurrentLinkedQueue<IPv4> ipPacketQueue; |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 93 | private IRestApiService restApi; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 94 | private List<ArpEntry> arpEntries; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 95 | private ArpHandler arpHandler; |
| 96 | private GenericIpHandler ipHandler; |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 97 | private IcmpHandler icmpHandler; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 98 | private IThreadPoolService threadPool; |
| 99 | private SingletonTask discoveryTask; |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 100 | private SingletonTask linkAddTask; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 101 | private SingletonTask testTask; |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 102 | private IFloodlightProviderService floodlightProvider; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 103 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 104 | private HashMap<Switch, ECMPShortestPathGraph> graphs; |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 105 | private HashMap<String, LinkData> linksDown; |
| 106 | private HashMap<String, LinkData> linksToAdd; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 107 | private ConcurrentLinkedQueue<TopologyEvents> topologyEventQueue; |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 108 | private HashMap<String, PolicyInfo> policyTable; |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 109 | private HashMap<String, TunnelInfo> tunnelTable; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 110 | |
| 111 | private int testMode = 0; |
| 112 | |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 113 | |
| 114 | private int numOfEvents = 0; |
| 115 | private int numOfEventProcess = 0; |
| 116 | private int numOfPopulation = 0; |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 117 | private long matchActionId = 0L; |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 118 | private final int DELAY_TO_ADD_LINK = 10; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 119 | private final int MAX_NUM_LABELS = 3; |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 120 | |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 121 | |
| 122 | private final int POLICY_ADD1 = 1; |
| 123 | private final int POLICY_ADD2 = 2; |
| 124 | private final int POLICY_REMOVE1 = 3; |
| 125 | private final int POLICY_REMOVE2 = 4; |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 126 | private final int TUNNEL_REMOVE1 = 5; |
| 127 | private final int TUNNEL_REMOVE2 = 6; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 128 | |
| 129 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 130 | // ************************************ |
| 131 | // IFloodlightModule implementation |
| 132 | // ************************************ |
| 133 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 134 | @Override |
| 135 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Srikanth Vavilapalli | 8c49db7 | 2014-10-20 13:35:08 -0700 | [diff] [blame] | 136 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(); |
| 137 | l.add(ISegmentRoutingService.class); |
| 138 | return l; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Srikanth Vavilapalli | 8c49db7 | 2014-10-20 13:35:08 -0700 | [diff] [blame] | 143 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<>(); |
| 144 | m.put(ISegmentRoutingService.class, this); |
| 145 | return m; |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 150 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>(); |
| 151 | |
| 152 | l.add(IFloodlightProviderService.class); |
| 153 | l.add(IConfigInfoService.class); |
| 154 | l.add(ITopologyService.class); |
| 155 | l.add(IPacketService.class); |
| 156 | l.add(IFlowPusherService.class); |
| 157 | l.add(ITopologyService.class); |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 158 | l.add(IRestApiService.class); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 159 | |
| 160 | return l; |
| 161 | |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void init(FloodlightModuleContext context) throws FloodlightModuleException { |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 166 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 167 | arpHandler = new ArpHandler(context, this); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 168 | icmpHandler = new IcmpHandler(context, this); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 169 | ipHandler = new GenericIpHandler(context, this); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 170 | arpEntries = new ArrayList<ArpEntry>(); |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 171 | topologyService = context.getServiceImpl(ITopologyService.class); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 172 | threadPool = context.getServiceImpl(IThreadPoolService.class); |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 173 | mutableTopology = topologyService.getTopology(); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 174 | ipPacketQueue = new ConcurrentLinkedQueue<IPv4>(); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 175 | graphs = new HashMap<Switch, ECMPShortestPathGraph>(); |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 176 | linksDown = new HashMap<String, LinkData>(); |
| 177 | linksToAdd = new HashMap<String, LinkData>(); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 178 | topologyEventQueue = new ConcurrentLinkedQueue<TopologyEvents>(); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 179 | packetService = context.getServiceImpl(IPacketService.class); |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 180 | restApi = context.getServiceImpl(IRestApiService.class); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 181 | policyTable = new HashMap<String, PolicyInfo>(); |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 182 | tunnelTable = new HashMap<String, TunnelInfo>(); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 183 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 184 | packetService.registerPacketListener(this); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 185 | topologyService.addListener(this, false); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 186 | |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 187 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void startUp(FloodlightModuleContext context) throws FloodlightModuleException { |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 192 | ScheduledExecutorService ses = threadPool.getScheduledExecutor(); |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 193 | restApi.addRestletRoutable(new SegmentRoutingWebRoutable()); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 194 | |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 195 | discoveryTask = new SingletonTask(ses, new Runnable() { |
| 196 | @Override |
| 197 | public void run() { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 198 | handleTopologyChangeEvents(); |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 199 | } |
| 200 | }); |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 201 | |
| 202 | linkAddTask = new SingletonTask(ses, new Runnable() { |
| 203 | @Override |
| 204 | public void run() { |
| 205 | delayedAddLink(); |
| 206 | } |
| 207 | }); |
| 208 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 209 | testTask = new SingletonTask(ses, new Runnable() { |
| 210 | @Override |
| 211 | public void run() { |
| 212 | runTest(); |
| 213 | } |
| 214 | }); |
| 215 | |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 216 | testMode = POLICY_ADD1; |
Sangho Shin | 00bd946 | 2014-10-20 17:00:54 -0700 | [diff] [blame] | 217 | //testTask.reschedule(20, TimeUnit.SECONDS); |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 220 | @Override |
| 221 | public void receive(Switch sw, Port inPort, Ethernet payload) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 222 | if (payload.getEtherType() == Ethernet.TYPE_ARP) |
| 223 | arpHandler.processPacketIn(sw, inPort, payload); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 224 | if (payload.getEtherType() == Ethernet.TYPE_IPV4) { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 225 | addPacketToPacketBuffer((IPv4) payload.getPayload()); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 226 | if (((IPv4) payload.getPayload()).getProtocol() == IPv4.PROTOCOL_ICMP) |
| 227 | icmpHandler.processPacketIn(sw, inPort, payload); |
| 228 | else |
| 229 | ipHandler.processPacketIn(sw, inPort, payload); |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 230 | } |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 231 | else { |
| 232 | log.debug("{}", payload.toString()); |
| 233 | } |
Srikanth Vavilapalli | b1fce73 | 2014-09-24 14:09:50 -0700 | [diff] [blame] | 234 | } |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 235 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 236 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 237 | // ************************************ |
| 238 | // Topology event handlers |
| 239 | // ************************************ |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 240 | |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 241 | /** |
| 242 | * Topology events that have been generated. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 243 | * |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 244 | * @param topologyEvents the generated Topology Events |
| 245 | * @see TopologyEvents |
| 246 | */ |
| 247 | public void topologyEvents(TopologyEvents topologyEvents) |
| 248 | { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 249 | topologyEventQueue.add(topologyEvents); |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 250 | discoveryTask.reschedule(100, TimeUnit.MILLISECONDS); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 251 | } |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 252 | |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Process the multiple topology events with some delay (100MS at most for now) |
| 255 | * |
| 256 | */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 257 | private void handleTopologyChangeEvents() { |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 258 | numOfEventProcess ++; |
| 259 | |
Sangho Shin | 5162534 | 2014-10-17 09:30:48 -0700 | [diff] [blame] | 260 | Collection<LinkData> linkEntriesAddedAll = new ArrayList<LinkData>(); |
| 261 | Collection<PortData> portEntriesAddedAll = new ArrayList<PortData>(); |
| 262 | Collection<PortData> portEntriesRemovedAll = new ArrayList<PortData>(); |
| 263 | Collection<LinkData> linkEntriesRemovedAll = new ArrayList<LinkData>(); |
| 264 | Collection<SwitchData> switchAddedAll = new ArrayList<SwitchData>(); |
| 265 | Collection<SwitchData> switchRemovedAll = new ArrayList<SwitchData>(); |
| 266 | Collection<MastershipData> mastershipRemovedAll = new ArrayList<MastershipData>(); |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 267 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 268 | while (!topologyEventQueue.isEmpty()) { |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 269 | // We should handle the events in the order of when they happen |
| 270 | // TODO: We need to simulate the final results of multiple events |
| 271 | // and shoot only the final state. |
| 272 | // Ex: link s1-s2 down, link s1-s2 up --> Do nothing |
| 273 | // 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] | 274 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 275 | TopologyEvents topologyEvents = topologyEventQueue.poll(); |
Sangho Shin | 5162534 | 2014-10-17 09:30:48 -0700 | [diff] [blame] | 276 | |
| 277 | Collection<LinkData> linkEntriesAdded = topologyEvents.getAddedLinkDataEntries(); |
| 278 | Collection<PortData> portEntriesAdded = topologyEvents.getAddedPortDataEntries(); |
| 279 | Collection<PortData> portEntriesRemoved = topologyEvents.getRemovedPortDataEntries(); |
| 280 | Collection<LinkData> linkEntriesRemoved = topologyEvents.getRemovedLinkDataEntries(); |
| 281 | Collection<SwitchData> switchAdded = topologyEvents.getAddedSwitchDataEntries(); |
| 282 | Collection<SwitchData> switchRemoved = topologyEvents.getRemovedSwitchDataEntries(); |
| 283 | Collection<MastershipData> mastershipRemoved = topologyEvents.getRemovedMastershipDataEntries(); |
| 284 | |
| 285 | linkEntriesAddedAll.addAll(linkEntriesAdded); |
| 286 | portEntriesAddedAll.addAll(portEntriesAdded); |
| 287 | portEntriesRemovedAll.addAll(portEntriesRemoved); |
| 288 | linkEntriesRemovedAll.addAll(linkEntriesRemoved); |
| 289 | switchAddedAll.addAll(switchAdded); |
| 290 | switchRemovedAll.addAll(switchRemoved); |
| 291 | mastershipRemovedAll.addAll(mastershipRemoved); |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 292 | numOfEvents++; |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 293 | |
| 294 | if (!portEntriesRemoved.isEmpty()) { |
| 295 | processPortRemoval(portEntriesRemoved); |
| 296 | } |
| 297 | |
| 298 | if (!linkEntriesRemoved.isEmpty()) { |
| 299 | processLinkRemoval(linkEntriesRemoved); |
| 300 | } |
| 301 | |
| 302 | if (!switchRemoved.isEmpty()) { |
| 303 | processSwitchRemoved(switchRemoved); |
| 304 | } |
| 305 | |
| 306 | if (!mastershipRemoved.isEmpty()) { |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 307 | log.debug("Mastership is removed. Check if ports are down also."); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | if (!linkEntriesAdded.isEmpty()) { |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 311 | processLinkAdd(linkEntriesAdded, false); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (!portEntriesAdded.isEmpty()) { |
| 315 | processPortAdd(portEntriesAdded); |
| 316 | } |
| 317 | |
| 318 | if (!switchAdded.isEmpty()) { |
| 319 | processSwitchAdd(switchAdded); |
| 320 | } |
Sangho Shin | 5162534 | 2014-10-17 09:30:48 -0700 | [diff] [blame] | 321 | |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 324 | // TODO: 100ms is enough to check both mastership removed events |
| 325 | // 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] | 326 | if (!mastershipRemovedAll.isEmpty()) { |
| 327 | if (portEntriesRemovedAll.isEmpty()) { |
Saurav Das | 82e6297 | 2014-10-16 14:53:57 -0700 | [diff] [blame] | 328 | log.debug("Just mastership is removed. Do not do anthing."); |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 329 | } |
| 330 | else { |
| 331 | HashMap<String, MastershipData> mastershipToRemove = |
| 332 | new HashMap<String, MastershipData>(); |
Sangho Shin | 5162534 | 2014-10-17 09:30:48 -0700 | [diff] [blame] | 333 | for (MastershipData ms: mastershipRemovedAll) { |
| 334 | for (PortData port: portEntriesRemovedAll) { |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 335 | // TODO: check ALL ports of the switch are dead .. |
| 336 | if (port.getDpid().equals(ms.getDpid())) { |
| 337 | mastershipToRemove.put(ms.getDpid().toString(), ms); |
| 338 | } |
| 339 | } |
| 340 | log.debug("Swtich {} is really down.", ms.getDpid()); |
| 341 | } |
| 342 | processMastershipRemoved(mastershipToRemove.values()); |
| 343 | } |
| 344 | } |
| 345 | |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 346 | log.debug("num events {}, num of process {}, " |
| 347 | + "num of Population {}", numOfEvents, numOfEventProcess, |
| 348 | numOfPopulation); |
| 349 | } |
| 350 | |
| 351 | /** |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 352 | * Process the SwitchAdded events from topologyMananger. |
| 353 | * It does nothing. When a switch is added, then link will be added too. |
| 354 | * LinkAdded event will handle process all re-computation. |
| 355 | * |
| 356 | * @param switchAdded |
| 357 | */ |
| 358 | private void processSwitchAdd(Collection<SwitchData> switchAdded) { |
| 359 | |
| 360 | } |
| 361 | |
| 362 | /** |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 363 | * Remove all ports connected to the switch removed |
| 364 | * |
| 365 | * @param mastershipRemoved master switch info removed |
| 366 | */ |
| 367 | private void processMastershipRemoved(Collection<MastershipData> |
| 368 | mastershipRemoved) { |
| 369 | for (MastershipData mastership: mastershipRemoved) { |
| 370 | Switch sw = mutableTopology.getSwitch(mastership.getDpid()); |
| 371 | for (Link link: sw.getOutgoingLinks()) { |
| 372 | Port dstPort = link.getDstPort(); |
| 373 | IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 374 | getSwId(dstPort.getDpid().toString())); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 375 | if (dstSw != null) { |
| 376 | dstSw.removePortFromGroups(dstPort.getNumber()); |
| 377 | log.debug("MasterSwitch {} is gone: remove port {}", sw.getDpid(), dstPort); |
| 378 | } |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 379 | } |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 380 | } |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 381 | |
| 382 | linksToAdd.clear(); |
| 383 | linksDown.clear(); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 384 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 385 | |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 386 | /** |
| 387 | * Remove all ports connected to the switch removed |
| 388 | * |
| 389 | * @param switchRemoved Switch removed |
| 390 | */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 391 | private void processSwitchRemoved(Collection<SwitchData> switchRemoved) { |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 392 | log.debug("SwitchRemoved event occurred !!!"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 395 | /** |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 396 | * Report ports added to driver |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 397 | * |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 398 | * @param portEntries |
| 399 | */ |
| 400 | private void processPortAdd(Collection<PortData> portEntries) { |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 401 | // TODO: do we need to add ports with delay? |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 402 | for (PortData port : portEntries) { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 403 | Dpid dpid = port.getDpid(); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 404 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 405 | IOF13Switch sw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 406 | getSwId(port.getDpid().toString())); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 407 | if (sw != null) { |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 408 | sw.addPortToGroups(port.getPortNumber()); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 409 | //log.debug("Add port {} to switch {}", port, dpid); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 410 | } |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Reports ports of new links to driver and recalculate ECMP SPG |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 416 | * If the link to add was removed before, then we just schedule the add link |
| 417 | * event and do not recompute the path now. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 418 | * |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 419 | * @param linkEntries |
| 420 | */ |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 421 | private void processLinkAdd(Collection<LinkData> linkEntries, boolean delayed) { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 422 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 423 | for (LinkData link : linkEntries) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 424 | |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 425 | SwitchPort srcPort = link.getSrc(); |
| 426 | SwitchPort dstPort = link.getDst(); |
| 427 | |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 428 | String key = srcPort.getDpid().toString() + |
| 429 | dstPort.getDpid().toString(); |
| 430 | if (!delayed) { |
| 431 | if (linksDown.containsKey(key)) { |
| 432 | linksToAdd.put(key, link); |
| 433 | linksDown.remove(key); |
| 434 | linkAddTask.reschedule(DELAY_TO_ADD_LINK, TimeUnit.SECONDS); |
| 435 | log.debug("Add link {} with 5 sec delay", link); |
| 436 | // TODO: What if we have multiple events of add link: |
| 437 | // one is new link add, the other one is link up for |
| 438 | // broken link? ECMPSPG function cannot deal with it for now |
| 439 | return; |
| 440 | } |
| 441 | } |
| 442 | else { |
| 443 | if (linksDown.containsKey(key)) { |
| 444 | linksToAdd.remove(key); |
| 445 | log.debug("Do not add the link {}: it is down again!", link); |
| 446 | return; |
| 447 | } |
| 448 | } |
| 449 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 450 | IOF13Switch srcSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 451 | getSwId(srcPort.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 452 | IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 453 | getSwId(dstPort.getDpid().toString())); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 454 | |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 455 | if ((srcSw == null) || (dstSw == null)) |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 456 | continue; |
| 457 | |
| 458 | srcSw.addPortToGroups(srcPort.getPortNumber()); |
| 459 | dstSw.addPortToGroups(dstPort.getPortNumber()); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 460 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 461 | //log.debug("Add a link port {} to switch {} to add link {}", srcPort, srcSw, |
| 462 | // link); |
| 463 | //log.debug("Add a link port {} to switch {} to add link {}", dstPort, dstSw, |
| 464 | // link); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 465 | |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 466 | } |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 467 | populateEcmpRoutingRules(false); |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 471 | * Check if all links are gone b/w the two switches. If all links are gone, |
| 472 | * then we need to recalculate the path. Otherwise, just report link failure |
| 473 | * to the driver. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 474 | * |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 475 | * @param linkEntries |
| 476 | */ |
| 477 | private void processLinkRemoval(Collection<LinkData> linkEntries) { |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 478 | boolean recomputationRequired = false; |
| 479 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 480 | for (LinkData link : linkEntries) { |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 481 | SwitchPort srcPort = link.getSrc(); |
| 482 | SwitchPort dstPort = link.getDst(); |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 483 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 484 | IOF13Switch srcSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 485 | getSwId(srcPort.getDpid().toString())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 486 | IOF13Switch dstSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 487 | getSwId(dstPort.getDpid().toString())); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 488 | if ((srcSw == null) || (dstSw == null)) |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 489 | /* If this link is not between two switches, ignore it */ |
| 490 | continue; |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 491 | |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 492 | srcSw.removePortFromGroups(srcPort.getPortNumber()); |
| 493 | dstSw.removePortFromGroups(dstPort.getPortNumber()); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 494 | log.debug("Remove port {} from switch {}", srcPort, srcSw); |
| 495 | log.debug("Remove port {} from switch {}", dstPort, dstSw); |
| 496 | |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 497 | Switch srcSwitch = mutableTopology.getSwitch(srcPort.getDpid()); |
| 498 | if (srcSwitch.getLinkToNeighbor(dstPort.getDpid()) == null) { |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 499 | // TODO: it is only for debugging purpose. |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 500 | // We just need to call populateEcmpRoutingRules() and return; |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 501 | recomputationRequired = true; |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 502 | log.debug("All links are gone b/w {} and {}", srcPort.getDpid(), |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 503 | dstPort.getDpid()); |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 504 | } |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 505 | |
| 506 | String key = link.getSrc().getDpid().toString()+ |
| 507 | link.getDst().getDpid().toString(); |
| 508 | if (!linksDown.containsKey(key)) { |
| 509 | linksDown.put(key, link); |
| 510 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 511 | } |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 512 | |
| 513 | if (recomputationRequired) |
| 514 | populateEcmpRoutingRules(false); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 515 | } |
Sangho Shin | c8d2f59 | 2014-09-30 16:53:57 -0700 | [diff] [blame] | 516 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 517 | /** |
Sangho Shin | 3a5fcad | 2014-10-01 14:14:49 -0700 | [diff] [blame] | 518 | * report ports removed to the driver immediately |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 519 | * |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 520 | * @param portEntries |
| 521 | */ |
| 522 | private void processPortRemoval(Collection<PortData> portEntries) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 523 | for (PortData port : portEntries) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 524 | Dpid dpid = port.getDpid(); |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 525 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 526 | IOF13Switch sw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 527 | getSwId(port.getDpid().toString())); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 528 | if (sw != null) { |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 529 | sw.removePortFromGroups(port.getPortNumber()); |
Sangho Shin | 815af0c | 2014-10-10 13:05:45 -0700 | [diff] [blame] | 530 | log.debug("Remove port {} from switch {}", port, dpid); |
| 531 | } |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 532 | } |
Srikanth Vavilapalli | b7e5c5e | 2014-09-18 07:38:27 -0700 | [diff] [blame] | 533 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 534 | |
| 535 | /** |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 536 | * Add the link immediately |
| 537 | * The function is scheduled when link add event happens and called |
| 538 | * DELAY_TO_ADD_LINK seconds after the event to avoid link flip-flop. |
| 539 | */ |
| 540 | private void delayedAddLink() { |
| 541 | |
| 542 | processLinkAdd(linksToAdd.values(), true); |
| 543 | |
| 544 | } |
| 545 | |
| 546 | |
| 547 | // ************************************ |
| 548 | // ECMP shorted path routing functions |
| 549 | // ************************************ |
| 550 | |
| 551 | /** |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 552 | * Populate routing rules walking through the ECMP shortest paths |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 553 | * |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 554 | * @param modified if true, it "modifies" the rules |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 555 | */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 556 | private void populateEcmpRoutingRules(boolean modified) { |
| 557 | graphs.clear(); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 558 | Iterable<Switch> switches = mutableTopology.getSwitches(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 559 | for (Switch sw : switches) { |
| 560 | ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(sw); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 561 | graphs.put(sw, ecmpSPG); |
| 562 | //log.debug("ECMPShortestPathGraph is computed for switch {}", |
| 563 | // HexString.toHexString(sw.getDpid().value())); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 564 | populateEcmpRoutingRulesForPath(sw, ecmpSPG, modified); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 565 | } |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 566 | numOfPopulation++; |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 567 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 568 | |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 569 | /** |
| 570 | * populate routing rules to forward packets from the switch given to |
| 571 | * all other switches. |
| 572 | * |
| 573 | * @param sw source switch |
| 574 | * @param ecmpSPG shortest path from the the source switch to all others |
| 575 | * @param modified modification flag |
| 576 | */ |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 577 | private void populateEcmpRoutingRulesForPath(Switch sw, |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 578 | ECMPShortestPathGraph ecmpSPG, boolean modified) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 579 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 580 | HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>> switchVia = |
| 581 | ecmpSPG.getAllLearnedSwitchesAndVia(); |
| 582 | for (Integer itrIdx : switchVia.keySet()) { |
| 583 | //log.debug("ECMPShortestPathGraph:Switches learned in " |
| 584 | // + "Iteration{} from switch {}:", |
| 585 | // itrIdx, |
| 586 | // HexString.toHexString(sw.getDpid().value())); |
| 587 | HashMap<Switch, ArrayList<ArrayList<Dpid>>> swViaMap = |
| 588 | switchVia.get(itrIdx); |
| 589 | for (Switch targetSw : swViaMap.keySet()) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 590 | //log.debug("ECMPShortestPathGraph:****switch {} via:", |
| 591 | // HexString.toHexString(targetSw.getDpid().value())); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 592 | String destSw = sw.getDpid().toString(); |
| 593 | List<String> fwdToSw = new ArrayList<String>(); |
| 594 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 595 | for (ArrayList<Dpid> via : swViaMap.get(targetSw)) { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 596 | //log.debug("ECMPShortestPathGraph:******{}) {}", ++i, via); |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 597 | if (via.isEmpty()) { |
| 598 | fwdToSw.add(destSw); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 599 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 600 | else { |
| 601 | fwdToSw.add(via.get(0).toString()); |
| 602 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 603 | } |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 604 | setRoutingRule(targetSw, destSw, fwdToSw, modified); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 605 | } |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 606 | |
| 607 | // Send Barrier Message and make sure all rules are set |
| 608 | // before we set the rules to next routers |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 609 | // TODO: barriers to all switches in this update stage |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 610 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 611 | getSwId(sw.getDpid().toString())); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 612 | if (sw13 != null) { |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 613 | OFBarrierReplyFuture replyFuture = null; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 614 | try { |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 615 | replyFuture = sw13.sendBarrier(); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 616 | } catch (IOException e) { |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 617 | log.error("Error sending barrier request to switch {}", |
| 618 | sw13.getId(), e.getCause()); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 619 | } |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 620 | OFBarrierReply br = null; |
| 621 | try { |
| 622 | br = replyFuture.get(2, TimeUnit.SECONDS); |
| 623 | } catch (TimeoutException | InterruptedException | ExecutionException e) { |
| 624 | // XXX for some reason these exceptions are not being thrown |
| 625 | } |
| 626 | if (br == null) { |
| 627 | log.warn("Did not receive barrier-reply from {}", sw13.getId()); |
| 628 | // XXX take corrective action |
| 629 | } |
| 630 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | |
| 634 | } |
| 635 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 636 | /** |
| 637 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 638 | * Set routing rules in targetSw {forward packets to fwdToSw switches in |
| 639 | * order to send packets to destSw} - If the target switch is an edge router |
| 640 | * and final destnation switch is also an edge router, then set IP |
| 641 | * forwarding rules to subnets - If only the target switch is an edge |
| 642 | * router, then set IP forwarding rule to the transit router loopback IP |
| 643 | * address - If the target is a transit router, then just set the MPLS |
| 644 | * forwarding rule |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 645 | * |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 646 | * @param targetSw Switch to set the rules |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 647 | * @param destSw Final destination switches |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 648 | * @param fwdToSw next hop switches |
| 649 | */ |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 650 | private void setRoutingRule(Switch targetSw, String destSw, |
| 651 | List<String> fwdToSw, boolean modified) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 652 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 653 | if (fwdToSw.isEmpty()) { |
| 654 | fwdToSw.add(destSw); |
| 655 | } |
| 656 | |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 657 | // 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] | 658 | if (IsEdgeRouter(targetSw.getDpid().toString()) && |
| 659 | IsEdgeRouter(destSw)) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 660 | // We assume that there is at least one transit router b/w edge |
| 661 | // routers |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 662 | Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw)); |
| 663 | String subnets = destSwitch.getStringAttribute("subnets"); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 664 | setIpTableRouterSubnet(targetSw, subnets, getMplsLabel(destSw) |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 665 | , fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 666 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 667 | String routerIp = destSwitch.getStringAttribute("routerIp"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 668 | setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw, |
| 669 | null, modified); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 670 | // Edge router can be a transit router |
| 671 | setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 672 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 673 | // 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] | 674 | else if (IsEdgeRouter(targetSw.getDpid().toString())) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 675 | // We assume that there is at least one transit router b/w edge |
| 676 | // routers |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 677 | Switch destSwitch = mutableTopology.getSwitch(new Dpid(destSw)); |
| 678 | String routerIp = destSwitch.getStringAttribute("routerIp"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 679 | setIpTableRouter(targetSw, routerIp, getMplsLabel(destSw), fwdToSw, |
| 680 | null, modified); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 681 | // Edge router can be a transit router |
| 682 | setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 683 | } |
| 684 | // if it is a transit router, then set rules in the MPLS table |
| 685 | else { |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 686 | setMplsTable(targetSw, getMplsLabel(destSw), fwdToSw, modified); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | } |
| 690 | |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 691 | /** |
| 692 | * Set IP forwarding rule to the gateway of each subnet of switches |
| 693 | * |
| 694 | * @param targetSw Switch to set rules |
| 695 | * @param subnets subnet information |
| 696 | * @param mplsLabel destination MPLS label |
| 697 | * @param fwdToSw router to forward packets to |
| 698 | */ |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 699 | private void setIpTableRouterSubnet(Switch targetSw, String subnets, |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 700 | String mplsLabel, List<String> fwdToSw, boolean modified) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 701 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 702 | Collection<MatchActionOperationEntry> entries = |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 703 | new ArrayList<MatchActionOperationEntry>(); |
| 704 | |
| 705 | try { |
| 706 | JSONArray arry = new JSONArray(subnets); |
| 707 | for (int i = 0; i < arry.length(); i++) { |
| 708 | String subnetIp = (String) arry.getJSONObject(i).get("subnetIp"); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 709 | setIpTableRouter(targetSw, subnetIp, mplsLabel, fwdToSw, entries, |
| 710 | modified); |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 711 | } |
| 712 | } catch (JSONException e) { |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 713 | e.printStackTrace(); |
| 714 | } |
| 715 | |
| 716 | if (!entries.isEmpty()) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 717 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 718 | getSwId(targetSw.getDpid().toString())); |
| 719 | |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 720 | if (sw13 != null) { |
| 721 | try { |
| 722 | sw13.pushFlows(entries); |
| 723 | } catch (IOException e) { |
| 724 | e.printStackTrace(); |
| 725 | } |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
| 729 | } |
| 730 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 731 | /** |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 732 | * Set IP forwarding rule - If the destination is the next hop, then do not |
| 733 | * push MPLS, just decrease the NW TTL - Otherwise, push MPLS label and set |
| 734 | * the MPLS ID |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 735 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 736 | * @param sw target switch to set rules |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 737 | * @param subnetIp Match IP address |
| 738 | * @param mplsLabel MPLS label of final destination router |
| 739 | * @param fwdToSws next hop routers |
Sangho Shin | 11d4e0f | 2014-09-30 12:00:33 -0700 | [diff] [blame] | 740 | * @param entries |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 741 | */ |
| 742 | private void setIpTableRouter(Switch sw, String subnetIp, String mplsLabel, |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 743 | List<String> fwdToSws, Collection<MatchActionOperationEntry> entries, |
| 744 | boolean modified) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 745 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 746 | Ipv4Match ipMatch = new Ipv4Match(subnetIp); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 747 | List<Action> actions = new ArrayList<>(); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 748 | GroupAction groupAction = new GroupAction(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 749 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 750 | // If destination SW is the same as the fwd SW, then do not push MPLS |
| 751 | // label |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 752 | if (fwdToSws.size() > 1) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 753 | PushMplsAction pushMplsAction = new PushMplsAction(); |
| 754 | SetMplsIdAction setIdAction = new SetMplsIdAction(Integer.parseInt(mplsLabel)); |
| 755 | CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 756 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 757 | |
Sangho Shin | 62ce5c1 | 2014-10-08 16:24:40 -0700 | [diff] [blame] | 758 | //actions.add(pushMplsAction); |
| 759 | //actions.add(copyTtlOutAction); |
| 760 | //actions.add(decMplsTtlAction); |
Saurav Das | 82e6297 | 2014-10-16 14:53:57 -0700 | [diff] [blame] | 761 | // actions.add(setIdAction); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 762 | groupAction.setEdgeLabel(Integer.parseInt(mplsLabel)); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 763 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 764 | else { |
| 765 | String fwdToSw = fwdToSws.get(0); |
| 766 | if (getMplsLabel(fwdToSw).equals(mplsLabel)) { |
| 767 | DecNwTtlAction decTtlAction = new DecNwTtlAction(1); |
| 768 | actions.add(decTtlAction); |
| 769 | } |
| 770 | else { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 771 | SetMplsIdAction setIdAction = new SetMplsIdAction( |
| 772 | Integer.parseInt(mplsLabel)); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 773 | CopyTtlOutAction copyTtlOutAction = new CopyTtlOutAction(); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 774 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 775 | |
Sangho Shin | 62ce5c1 | 2014-10-08 16:24:40 -0700 | [diff] [blame] | 776 | //actions.add(pushMplsAction); |
| 777 | //actions.add(copyTtlOutAction); |
| 778 | //actions.add(decMplsTtlAction); |
Saurav Das | 82e6297 | 2014-10-16 14:53:57 -0700 | [diff] [blame] | 779 | // actions.add(setIdAction); |
Sangho Shin | 721ca04 | 2014-10-09 13:03:40 -0700 | [diff] [blame] | 780 | groupAction.setEdgeLabel(Integer.parseInt(mplsLabel)); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 781 | } |
| 782 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 783 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 784 | for (String fwdSw : fwdToSws) { |
| 785 | groupAction.addSwitch(new Dpid(fwdSw)); |
| 786 | } |
| 787 | actions.add(groupAction); |
| 788 | |
Sangho Shin | 99918bd | 2014-10-08 15:52:35 -0700 | [diff] [blame] | 789 | MatchAction matchAction = new MatchAction(new MatchActionId(matchActionId++), |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 790 | new SwitchPort((long) 0, (short) 0), ipMatch, actions); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 791 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 792 | Operator operator = null; |
| 793 | if (modified) |
| 794 | operator = Operator.MODIFY; |
| 795 | else |
| 796 | operator = Operator.ADD; |
| 797 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 798 | MatchActionOperationEntry maEntry = |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 799 | new MatchActionOperationEntry(operator, matchAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 800 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 801 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 802 | getSwId(sw.getDpid().toString())); |
| 803 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 804 | if (sw13 != null) { |
| 805 | try { |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 806 | //printMatchActionOperationEntry(sw, maEntry); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 807 | if (entries != null) |
| 808 | entries.add(maEntry); |
| 809 | else |
| 810 | sw13.pushFlow(maEntry); |
| 811 | } catch (IOException e) { |
| 812 | e.printStackTrace(); |
| 813 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 818 | /** |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 819 | * Set MPLS forwarding rules to MPLS table |
| 820 | * </p> |
| 821 | * If the destination is the same as the next hop to forward packets then, |
| 822 | * pop the MPLS label according to PHP rule. Here, if BoS is set, then |
| 823 | * copy TTL In and decrement NW TTL. Otherwise, it just decrement the MPLS |
| 824 | * TTL of the another MPLS header. |
| 825 | * If the next hop is not the destination, just forward packets to next |
| 826 | * hops using Group action. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 827 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 828 | * @param sw Switch to set the rules |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 829 | * @param mplsLabel destination MPLS label |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 830 | * @param fwdSws next hop switches |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 831 | * */ |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 832 | private void setMplsTable(Switch sw, String mplsLabel, List<String> fwdSws, |
| 833 | boolean modified) { |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 834 | |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 835 | if (fwdSws.isEmpty()) |
| 836 | return; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 837 | |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 838 | Collection<MatchActionOperationEntry> maEntries = |
| 839 | new ArrayList<MatchActionOperationEntry>(); |
| 840 | String fwdSw1 = fwdSws.get(0); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 841 | |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 842 | if (fwdSws.size() == 1 && mplsLabel.equals(getMplsLabel(fwdSw1))) { |
| 843 | // One rule for Bos = 1 |
| 844 | MplsMatch mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel), true); |
| 845 | List<Action> actions = new ArrayList<Action>(); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 846 | |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 847 | PopMplsAction popAction = new PopMplsAction(EthType.IPv4); |
| 848 | CopyTtlInAction copyTtlInAction = new CopyTtlInAction(); |
| 849 | DecNwTtlAction decNwTtlAction = new DecNwTtlAction(1); |
| 850 | |
| 851 | actions.add(copyTtlInAction); |
| 852 | actions.add(popAction); |
| 853 | actions.add(decNwTtlAction); |
| 854 | |
| 855 | GroupAction groupAction = new GroupAction(); |
| 856 | groupAction.addSwitch(new Dpid(fwdSw1)); |
| 857 | actions.add(groupAction); |
| 858 | |
| 859 | MatchAction matchAction = new MatchAction(new MatchActionId(matchActionId++), |
| 860 | new SwitchPort((long) 0, (short) 0), mplsMatch, actions); |
| 861 | Operator operator = Operator.ADD; |
| 862 | MatchActionOperationEntry maEntry = |
| 863 | new MatchActionOperationEntry(operator, matchAction); |
| 864 | maEntries.add(maEntry); |
| 865 | |
| 866 | // One rule for Bos = 0 |
Sangho Shin | 23f898d | 2014-10-13 16:54:00 -0700 | [diff] [blame] | 867 | MplsMatch mplsMatchBos = new MplsMatch(Integer.parseInt(mplsLabel), false); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 868 | List<Action> actionsBos = new ArrayList<Action>(); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 869 | PopMplsAction popActionBos = new PopMplsAction(EthType.MPLS_UNICAST); |
| 870 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
| 871 | |
| 872 | actionsBos.add(copyTtlInAction); |
| 873 | actionsBos.add(popActionBos); |
| 874 | actionsBos.add(decMplsTtlAction); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 875 | actionsBos.add(groupAction); |
| 876 | |
| 877 | MatchAction matchActionBos = new MatchAction(new MatchActionId(matchActionId++), |
| 878 | new SwitchPort((long) 0, (short) 0), mplsMatchBos, actionsBos); |
| 879 | MatchActionOperationEntry maEntryBos = |
| 880 | new MatchActionOperationEntry(operator, matchActionBos); |
| 881 | maEntries.add(maEntryBos); |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 882 | } |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 883 | else { |
| 884 | MplsMatch mplsMatch = new MplsMatch(Integer.parseInt(mplsLabel), false); |
| 885 | List<Action> actions = new ArrayList<Action>(); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 886 | |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 887 | DecMplsTtlAction decMplsTtlAction = new DecMplsTtlAction(1); |
| 888 | actions.add(decMplsTtlAction); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 889 | |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 890 | GroupAction groupAction = new GroupAction(); |
| 891 | for (String fwdSw : fwdSws) |
| 892 | groupAction.addSwitch(new Dpid(fwdSw)); |
| 893 | actions.add(groupAction); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 894 | |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 895 | MatchAction matchAction = new MatchAction(new MatchActionId( |
| 896 | matchActionId++), |
| 897 | new SwitchPort((long) 0, (short) 0), mplsMatch, actions); |
| 898 | Operator operator = Operator.ADD; |
| 899 | MatchActionOperationEntry maEntry = |
| 900 | new MatchActionOperationEntry(operator, matchAction); |
| 901 | maEntries.add(maEntry); |
Sangho Shin | e2e9bcd | 2014-10-13 15:14:18 -0700 | [diff] [blame] | 902 | |
| 903 | // BoS = 1 |
| 904 | MplsMatch mplsMatchBoS = new MplsMatch(Integer.parseInt(mplsLabel), true); |
| 905 | List<Action> actionsBoS = new ArrayList<Action>(); |
| 906 | |
| 907 | DecMplsTtlAction decMplsTtlActionBoS = new DecMplsTtlAction(1); |
| 908 | actionsBoS.add(decMplsTtlActionBoS); |
| 909 | |
| 910 | GroupAction groupActionBoS = new GroupAction(); |
| 911 | for (String fwdSw : fwdSws) |
| 912 | groupActionBoS.addSwitch(new Dpid(fwdSw)); |
| 913 | actionsBoS.add(groupActionBoS); |
| 914 | |
| 915 | MatchAction matchActionBos = new MatchAction(new MatchActionId( |
| 916 | matchActionId++), |
| 917 | new SwitchPort((long) 0, (short) 0), mplsMatchBoS, actionsBoS); |
| 918 | MatchActionOperationEntry maEntryBoS = |
| 919 | new MatchActionOperationEntry(operator, matchActionBos); |
| 920 | maEntries.add(maEntryBoS); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 921 | } |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 922 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 923 | getSwId(sw.getDpid().toString())); |
| 924 | |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 925 | if (sw13 != null) { |
| 926 | try { |
Sangho Shin | bce900e | 2014-10-07 17:13:23 -0700 | [diff] [blame] | 927 | //printMatchActionOperationEntry(sw, maEntry); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 928 | sw13.pushFlows(maEntries); |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 929 | } catch (IOException e) { |
| 930 | e.printStackTrace(); |
| 931 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 932 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 935 | |
| 936 | // ************************************ |
| 937 | // Policy routing classes and functions |
| 938 | // ************************************ |
| 939 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 940 | public class PolicyInfo { |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 941 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 942 | private String policyId; |
| 943 | private PacketMatch match; |
| 944 | private int priority; |
| 945 | private String tunnelId; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 946 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 947 | public PolicyInfo(String pid, PacketMatch match, int priority, String tid) { |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 948 | this.policyId = pid; |
| 949 | this.match = match; |
| 950 | this.priority = priority; |
| 951 | this.tunnelId = tid; |
| 952 | } |
| 953 | } |
| 954 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 955 | public class TunnelInfo { |
| 956 | private String tunnelId; |
| 957 | private List<Dpid> dpids; |
| 958 | private List<TunnelRouteInfo> routes; |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 959 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 960 | public TunnelInfo(String tid, List<Dpid> dpids, List<TunnelRouteInfo> routes) { |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 961 | this.tunnelId = tid; |
| 962 | this.dpids = dpids; |
| 963 | this.routes = routes; |
| 964 | } |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 965 | public String getTunnelId(){ |
| 966 | return this.tunnelId; |
| 967 | } |
| 968 | public List<Dpid> getDpids(){ |
| 969 | return this.dpids; |
| 970 | } |
| 971 | public List<TunnelRouteInfo> getRoutes(){ |
| 972 | return this.routes; |
| 973 | } |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 976 | public class TunnelRouteInfo { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 977 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 978 | private String srcSwDpid; |
| 979 | private List<Dpid> fwdSwDpids; |
| 980 | private List<String> route; |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 981 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 982 | public TunnelRouteInfo() { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 983 | fwdSwDpids = new ArrayList<Dpid>(); |
| 984 | route = new ArrayList<String>(); |
| 985 | } |
| 986 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 987 | private void setSrcDpid(String dpid) { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 988 | this.srcSwDpid = dpid; |
| 989 | } |
| 990 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 991 | private void setFwdSwDpid(List<Dpid> dpid) { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 992 | this.fwdSwDpids = dpid; |
| 993 | } |
| 994 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 995 | private void addRoute(String id) { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 996 | route.add(id); |
| 997 | } |
| 998 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 999 | private void setRoute(List<String> r) { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1000 | this.route = r; |
| 1001 | } |
| 1002 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1003 | public String getSrcSwDpid() { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1004 | return this.srcSwDpid; |
| 1005 | } |
| 1006 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1007 | public List<Dpid> getFwdSwDpid() { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1008 | return this.fwdSwDpids; |
| 1009 | } |
| 1010 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1011 | public List<String> getRoute() { |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1012 | return this.route; |
| 1013 | } |
| 1014 | } |
| 1015 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1016 | /** |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1017 | * Return the Tunnel table |
| 1018 | * |
| 1019 | * @return collection of TunnelInfo |
| 1020 | */ |
| 1021 | public Collection<TunnelInfo> getTunnelTable() { |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 1022 | return this.tunnelTable.values(); |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /** |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1026 | * Create a tunnel for policy routing |
| 1027 | * It delivers the node IDs of tunnels to driver. |
| 1028 | * Split the node IDs if number of IDs exceeds the limit for stitching. |
| 1029 | * |
| 1030 | * @param tunnelId Node IDs for the tunnel |
| 1031 | * @param Ids tunnel ID |
| 1032 | */ |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1033 | public boolean createTunnel(String tunnelId, List<Dpid> dpids) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1034 | |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1035 | if (dpids.isEmpty() || dpids.size() < 2) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1036 | log.debug("Wrong tunnel information"); |
| 1037 | return false; |
| 1038 | } |
| 1039 | |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1040 | List<String> Ids = new ArrayList<String>(); |
| 1041 | for (Dpid dpid: dpids) { |
| 1042 | Ids.add(getMplsLabel(dpid.toString())); |
| 1043 | } |
| 1044 | |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1045 | List<TunnelRouteInfo> stitchingRule = getStitchingRule(Ids); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1046 | if (stitchingRule == null) { |
| 1047 | log.debug("Failed to get the policy rule."); |
| 1048 | return false; |
| 1049 | } |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1050 | //HashMap<String, Integer> switchGroupPair = new HashMap<String, Integer>(); |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1051 | for (TunnelRouteInfo route: stitchingRule) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1052 | |
| 1053 | IOF13Switch targetSw = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1054 | getSwId(route.srcSwDpid)); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1055 | |
| 1056 | if (targetSw == null) { |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1057 | log.debug("Switch {} is gone.", route.srcSwDpid); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1058 | return false; |
| 1059 | } |
| 1060 | |
| 1061 | NeighborSet ns = new NeighborSet(); |
| 1062 | for (Dpid dpid: route.getFwdSwDpid()) |
| 1063 | ns.addDpid(dpid); |
| 1064 | |
| 1065 | printTunnelInfo(targetSw, tunnelId, route.getRoute(), ns); |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1066 | targetSw.createTunnel(tunnelId, route.getRoute(), ns); |
| 1067 | //switchGroupPair.put(route.srcSwDpid.toString(), groupId); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1068 | |
| 1069 | } |
| 1070 | |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1071 | //tunnelGroupMap.put(tunnelId, switchGroupPair); |
| 1072 | TunnelInfo tunnelInfo = new TunnelInfo(tunnelId, dpids, stitchingRule); |
| 1073 | tunnelTable.put(tunnelId, tunnelInfo); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1074 | |
| 1075 | return true; |
| 1076 | } |
| 1077 | |
| 1078 | /** |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1079 | * Return router DPIDs for the tunnel |
| 1080 | * |
| 1081 | * @param tid tunnel ID |
| 1082 | * @return List of DPID |
| 1083 | */ |
| 1084 | public List<Dpid> getTunnelInfo(String tid) { |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1085 | TunnelInfo tunnelInfo = tunnelTable.get(tid); |
| 1086 | return tunnelInfo.dpids; |
| 1087 | |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | /** |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1091 | * Set policy table for policy routing |
| 1092 | * |
| 1093 | * @param sw |
| 1094 | * @param mplsLabel |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1095 | * @return |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1096 | */ |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1097 | public boolean createPolicy(String pid, MACAddress srcMac, MACAddress dstMac, |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1098 | Short etherType, IPv4Net srcIp, IPv4Net dstIp, Byte ipProto, |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1099 | Short srcTcpPort, Short dstTcpPort, int priority, String tid) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1100 | |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1101 | //HashMap<String, TunnelRouteInfo> routeInfo = stitchInfo.get(tid); |
| 1102 | //HashMap<String, Integer> switchGroupPair = tunnelGroupMap.get(tid); |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1103 | |
| 1104 | PacketMatchBuilder packetBuilder = new PacketMatchBuilder(); |
| 1105 | |
| 1106 | if (srcMac != null) |
| 1107 | packetBuilder.setSrcMac(srcMac); |
| 1108 | if (dstMac != null) |
| 1109 | packetBuilder.setDstMac(dstMac); |
| 1110 | if (etherType != null) |
| 1111 | packetBuilder.setEtherType(etherType); |
| 1112 | if (srcIp != null) |
| 1113 | packetBuilder.setSrcIp(srcIp.address(), srcIp.prefixLen()); |
| 1114 | if (dstIp != null) |
| 1115 | packetBuilder.setDstIp(dstIp.address(), dstIp.prefixLen()); |
| 1116 | if (ipProto != null) |
| 1117 | packetBuilder.setIpProto(ipProto); |
| 1118 | if (srcTcpPort > 0) |
| 1119 | packetBuilder.setSrcTcpPort(srcTcpPort); |
| 1120 | if (dstTcpPort > 0) |
| 1121 | packetBuilder.setDstTcpPort(dstTcpPort); |
| 1122 | PacketMatch policyMatch = packetBuilder.build(); |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1123 | TunnelInfo tunnelInfo = tunnelTable.get(tid); |
| 1124 | List<TunnelRouteInfo> routes = tunnelInfo.routes; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1125 | |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1126 | for (TunnelRouteInfo route : routes) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1127 | List<Action> actions = new ArrayList<>(); |
| 1128 | GroupAction groupAction = new GroupAction(); |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1129 | groupAction.setTunnelId(tid); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1130 | actions.add(groupAction); |
| 1131 | |
| 1132 | MatchAction matchAction = new MatchAction(new MatchActionId( |
| 1133 | matchActionId++), |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1134 | new SwitchPort((long) 0, (short) 0), policyMatch, priority, |
| 1135 | actions); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1136 | MatchActionOperationEntry maEntry = |
| 1137 | new MatchActionOperationEntry(Operator.ADD, matchAction); |
| 1138 | |
| 1139 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1140 | getSwId(route.srcSwDpid)); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1141 | |
| 1142 | if (sw13 != null) { |
| 1143 | printMatchActionOperationEntry(sw13, maEntry); |
| 1144 | try { |
| 1145 | sw13.pushFlow(maEntry); |
| 1146 | } catch (IOException e) { |
| 1147 | e.printStackTrace(); |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1148 | return false; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | } |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1152 | |
| 1153 | PolicyInfo policyInfo = new PolicyInfo(pid, policyMatch, priority, tid); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1154 | policyTable.put(pid, policyInfo); |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1155 | |
| 1156 | return true; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | /** |
Sangho Shin | 1ad7be0 | 2014-10-20 16:56:49 -0700 | [diff] [blame] | 1160 | * Split the nodes IDs into multiple tunnel if Segment Stitching is required. |
| 1161 | * We assume that the first node ID is the one of source router, and the last |
| 1162 | * node ID is that of the destination router. |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1163 | * |
Sangho Shin | 1ad7be0 | 2014-10-20 16:56:49 -0700 | [diff] [blame] | 1164 | * @param route list of node IDs |
| 1165 | * @return List of the TunnelRoutInfo |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1166 | */ |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1167 | private List<TunnelRouteInfo> getStitchingRule(List<String> route) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1168 | |
| 1169 | if (route.isEmpty() || route.size() < 2) |
| 1170 | return null; |
| 1171 | |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1172 | List<TunnelRouteInfo> rules = new ArrayList<TunnelRouteInfo>(); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1173 | |
| 1174 | Switch srcSw = this.getSwitchFromNodeId(route.get(0)); |
| 1175 | String srcDpid = srcSw.getDpid().toString(); |
| 1176 | |
| 1177 | if (route.size() <= MAX_NUM_LABELS+1) { |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1178 | TunnelRouteInfo info = new TunnelRouteInfo(); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1179 | info.setSrcDpid(srcSw.getDpid().toString()); |
| 1180 | List<Dpid> fwdSwDpids = getForwardingSwitchForNodeId(srcSw, route.get(1)); |
| 1181 | info.setFwdSwDpid(fwdSwDpids); |
| 1182 | route.remove(0); |
| 1183 | info.setRoute(route); |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1184 | rules.add(info); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1185 | return rules; |
| 1186 | } |
| 1187 | |
| 1188 | int i = 0; |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1189 | TunnelRouteInfo routeInfo = new TunnelRouteInfo(); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1190 | boolean checkNeighbor = true; |
| 1191 | |
| 1192 | for (String nodeId: route) { |
Sangho Shin | 1ad7be0 | 2014-10-20 16:56:49 -0700 | [diff] [blame] | 1193 | // First node ID is always the source router |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1194 | if (i == 0) { |
| 1195 | routeInfo.setSrcDpid(srcDpid); |
| 1196 | srcSw = getSwitchFromNodeId(nodeId); |
| 1197 | i++; |
| 1198 | } |
| 1199 | else if (i == 1) { |
| 1200 | if (checkNeighbor) { |
| 1201 | // Check if next node is the neighbor SW of the source SW |
| 1202 | List<Dpid> fwdSwDpids = getForwardingSwitchForNodeId(srcSw, nodeId); |
| 1203 | if (fwdSwDpids == null || fwdSwDpids.isEmpty()) { |
| 1204 | log.debug("There is no route from node {} to node {}", srcSw.getDpid(), nodeId); |
| 1205 | return null; |
| 1206 | } |
| 1207 | // If first Id is one of the neighbors, do not include it to route, but set it as a fwd SW. |
| 1208 | boolean match = false; |
| 1209 | for (Dpid dpid: fwdSwDpids) { |
| 1210 | if (getMplsLabel(dpid.toString()).toString().equals(nodeId)) { |
| 1211 | List<Dpid> fwdSws = new ArrayList<Dpid>(); |
| 1212 | fwdSws.add(dpid); |
| 1213 | routeInfo.setFwdSwDpid(fwdSws); |
| 1214 | match = true; |
| 1215 | break; |
| 1216 | } |
| 1217 | } |
| 1218 | if (!match) { |
| 1219 | routeInfo.addRoute(nodeId); |
| 1220 | routeInfo.setFwdSwDpid(fwdSwDpids); |
| 1221 | i++; |
| 1222 | } |
Sangho Shin | 1ad7be0 | 2014-10-20 16:56:49 -0700 | [diff] [blame] | 1223 | // we check only the next node ID of the source router |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1224 | checkNeighbor = false; |
| 1225 | } |
| 1226 | else { |
| 1227 | routeInfo.addRoute(nodeId); |
| 1228 | i++; |
| 1229 | } |
| 1230 | } |
| 1231 | else { |
| 1232 | routeInfo.addRoute(nodeId); |
| 1233 | i++; |
| 1234 | } |
| 1235 | |
Sangho Shin | 1ad7be0 | 2014-10-20 16:56:49 -0700 | [diff] [blame] | 1236 | // If the number of labels reaches the limit, start over the procedure |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1237 | if (i == MAX_NUM_LABELS+1) { |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1238 | rules.add(routeInfo); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1239 | routeInfo = new TunnelRouteInfo(); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1240 | srcSw = getSwitchFromNodeId(nodeId); |
| 1241 | srcDpid = getSwitchFromNodeId(nodeId).getDpid().toString(); |
| 1242 | routeInfo.setSrcDpid(srcDpid); |
| 1243 | i = 1; |
| 1244 | checkNeighbor = true; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | if (i < MAX_NUM_LABELS+1) { |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1249 | rules.add(routeInfo); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | return rules; |
| 1253 | } |
| 1254 | |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1255 | /** |
| 1256 | * Remove all policies applied to specific tunnel. |
| 1257 | * |
| 1258 | * @param srcMac |
| 1259 | * @param dstMac |
| 1260 | * @param etherType |
| 1261 | * @param srcIp |
| 1262 | * @param dstIp |
| 1263 | * @param ipProto |
| 1264 | * @param srcTcpPort |
| 1265 | * @param dstTcpPort |
| 1266 | * @param tid |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1267 | * @return |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1268 | */ |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1269 | public boolean removePolicy(String pid) { |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1270 | PolicyInfo policyInfo = policyTable.get(pid); |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1271 | PacketMatch policyMatch = policyInfo.match; |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1272 | String tid = policyInfo.tunnelId; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1273 | int priority = policyInfo.priority; |
| 1274 | |
| 1275 | List<Action> actions = new ArrayList<>(); |
| 1276 | int gropuId = 0; // dummy group ID |
| 1277 | GroupAction groupAction = new GroupAction(); |
| 1278 | groupAction.setGroupId(gropuId); |
| 1279 | actions.add(groupAction); |
| 1280 | |
| 1281 | MatchAction matchAction = new MatchAction(new MatchActionId( |
| 1282 | matchActionId++), |
| 1283 | new SwitchPort((long) 0, (short) 0), policyMatch, priority, |
| 1284 | actions); |
| 1285 | MatchActionOperationEntry maEntry = |
| 1286 | new MatchActionOperationEntry(Operator.REMOVE, matchAction); |
| 1287 | |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1288 | TunnelInfo tunnelInfo = tunnelTable.get(tid); |
| 1289 | List<TunnelRouteInfo> routes = tunnelInfo.routes; |
| 1290 | |
| 1291 | for (TunnelRouteInfo route : routes) { |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1292 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1293 | getSwId(route.srcSwDpid)); |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1294 | |
| 1295 | if (sw13 != null) { |
| 1296 | printMatchActionOperationEntry(sw13, maEntry); |
| 1297 | try { |
| 1298 | sw13.pushFlow(maEntry); |
| 1299 | } catch (IOException e) { |
| 1300 | e.printStackTrace(); |
| 1301 | log.debug("policy remove failed due to pushFlow() exception"); |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1302 | return false; |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1307 | policyTable.remove(pid); |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1308 | log.debug("Policy {} is removed.", pid); |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1309 | return true; |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1310 | } |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1311 | |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1312 | /** |
Sangho Shin | 1ad7be0 | 2014-10-20 16:56:49 -0700 | [diff] [blame] | 1313 | * Get the first group ID for the tunnel for specific source router |
| 1314 | * If Segment Stitching was required to create the tunnel, there are |
| 1315 | * mutiple source routers. |
| 1316 | * |
| 1317 | * @param tunnelId ID for the tunnel |
| 1318 | * @param dpid source router DPID |
| 1319 | * @return the first group ID of the tunnel |
| 1320 | */ |
| 1321 | public int getTunnelGroupId(String tunnelId, String dpid) { |
| 1322 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 1323 | getSwId(dpid)); |
| 1324 | |
| 1325 | if (sw13 == null) { |
| 1326 | return -1; |
| 1327 | } |
| 1328 | else { |
| 1329 | return sw13.getTunnelGroupId(tunnelId); |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | /** |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1334 | * Remove a tunnel |
| 1335 | * It removes all groups for the tunnel if the tunnel is not used for any |
| 1336 | * policy. |
| 1337 | * |
| 1338 | * @param tunnelId tunnel ID to remove |
| 1339 | */ |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1340 | public boolean removeTunnel(String tunnelId) { |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1341 | |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1342 | // Check if the tunnel is used for any policy |
| 1343 | for (PolicyInfo policyInfo: policyTable.values()) { |
| 1344 | if (policyInfo.tunnelId == tunnelId) { |
| 1345 | log.debug("Tunnel {} is still used for the policy {}.", |
| 1346 | policyInfo.policyId, tunnelId); |
| 1347 | return false; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | TunnelInfo tunnelInfo = tunnelTable.get(tunnelId); |
| 1352 | |
| 1353 | List<TunnelRouteInfo> routes = tunnelInfo.routes; |
| 1354 | for (TunnelRouteInfo route: routes) { |
| 1355 | IOF13Switch sw13 = (IOF13Switch) floodlightProvider.getMasterSwitch( |
| 1356 | getSwId(route.srcSwDpid)); |
| 1357 | |
| 1358 | if (sw13 != null) { |
| 1359 | sw13.removeTunnel(tunnelId); |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | tunnelTable.remove(tunnelId); |
| 1364 | log.debug("Tunnel {} was removed ", tunnelId); |
| 1365 | |
Sangho Shin | 306633a | 2014-10-20 14:26:55 -0700 | [diff] [blame] | 1366 | return true; |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1369 | // ************************************ |
| 1370 | // Utility functions |
| 1371 | // ************************************ |
| 1372 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1373 | /** |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1374 | * Get the forwarding Switch DPIDs to send packets to a node |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1375 | * |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1376 | * @param srcSw source switch |
| 1377 | * @param nodeId destination node Id |
| 1378 | * @return list of switch DPID to forward packets to |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1379 | */ |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1380 | private List<Dpid> getForwardingSwitchForNodeId(Switch srcSw, String nodeId) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1381 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1382 | List<Dpid> fwdSws = new ArrayList<Dpid>(); |
| 1383 | Switch destSw = null; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1384 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1385 | destSw = getSwitchFromNodeId(nodeId); |
| 1386 | |
| 1387 | if (destSw == null) { |
| 1388 | log.debug("Cannot find the switch with ID {}", nodeId); |
| 1389 | return null; |
| 1390 | } |
| 1391 | |
| 1392 | ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(srcSw); |
| 1393 | |
| 1394 | HashMap<Integer, HashMap<Switch, ArrayList<ArrayList<Dpid>>>> switchVia = |
| 1395 | ecmpSPG.getAllLearnedSwitchesAndVia(); |
| 1396 | for (Integer itrIdx : switchVia.keySet()) { |
| 1397 | HashMap<Switch, ArrayList<ArrayList<Dpid>>> swViaMap = |
| 1398 | switchVia.get(itrIdx); |
| 1399 | for (Switch targetSw : swViaMap.keySet()) { |
| 1400 | String destSwDpid = destSw.getDpid().toString(); |
| 1401 | if (targetSw.getDpid().toString().equals(destSwDpid)) { |
| 1402 | for (ArrayList<Dpid> via : swViaMap.get(targetSw)) { |
| 1403 | if (via.isEmpty()) { |
| 1404 | fwdSws.add(destSw.getDpid()); |
| 1405 | } |
| 1406 | else { |
| 1407 | fwdSws.add(via.get(0)); |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | return fwdSws; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1417 | /** |
| 1418 | * Get switch for the node Id specified |
| 1419 | * |
| 1420 | * @param nodeId node ID for switch |
| 1421 | * @return Switch |
| 1422 | */ |
| 1423 | private Switch getSwitchFromNodeId(String nodeId) { |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 1424 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1425 | for (Switch sw : mutableTopology.getSwitches()) { |
| 1426 | String id = sw.getStringAttribute("nodeSid"); |
| 1427 | if (id.equals(nodeId)) { |
| 1428 | return sw; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | return null; |
| 1433 | } |
Fahad Naeem Khan | 4444b95 | 2014-10-18 22:30:50 -0700 | [diff] [blame] | 1434 | |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1435 | /** |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1436 | * Convert a string DPID to its Switch Id (integer) |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1437 | * |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1438 | * @param dpid |
| 1439 | * @return |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1440 | */ |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1441 | private long getSwId(String dpid) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1442 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1443 | long swId = 0; |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1444 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1445 | String swIdHexStr = "0x"+dpid.substring(dpid.lastIndexOf(":") + 1); |
| 1446 | if (swIdHexStr != null) |
| 1447 | swId = Integer.decode(swIdHexStr); |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1448 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1449 | return swId; |
| 1450 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1451 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1452 | /** |
| 1453 | * Check if the switch is the edge router or not. |
| 1454 | * |
| 1455 | * @param dpid Dpid of the switch to check |
| 1456 | * @return true if it is an edge router, otherwise false |
| 1457 | */ |
| 1458 | private boolean IsEdgeRouter(String dpid) { |
Sangho Shin | 0df0198 | 2014-09-25 17:11:18 -0700 | [diff] [blame] | 1459 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1460 | for (Switch sw : mutableTopology.getSwitches()) { |
| 1461 | String dpidStr = sw.getDpid().toString(); |
| 1462 | if (dpid.equals(dpidStr)) { |
| 1463 | /* |
| 1464 | String subnetInfo = sw.getStringAttribute("subnets"); |
| 1465 | if (subnetInfo == null || subnetInfo.equals("[]")) { |
| 1466 | return false; |
| 1467 | } |
| 1468 | else |
| 1469 | return true; |
| 1470 | */ |
| 1471 | String isEdge = sw.getStringAttribute("isEdgeRouter"); |
| 1472 | if (isEdge != null) { |
| 1473 | if (isEdge.equals("true")) |
| 1474 | return true; |
| 1475 | else |
| 1476 | return false; |
| 1477 | } |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1478 | } |
| 1479 | } |
| 1480 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1481 | return false; |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | /** |
| 1485 | * Get MPLS label reading the config file |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1486 | * |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1487 | * @param dipid DPID of the switch |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1488 | * @return MPLS label for the switch |
| 1489 | */ |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1490 | private String getMplsLabel(String dpid) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1491 | |
| 1492 | String mplsLabel = null; |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1493 | for (Switch sw : mutableTopology.getSwitches()) { |
Sangho Shin | 43cee11 | 2014-09-25 16:43:34 -0700 | [diff] [blame] | 1494 | String dpidStr = sw.getDpid().toString(); |
| 1495 | if (dpid.equals(dpidStr)) { |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1496 | mplsLabel = sw.getStringAttribute("nodeSid"); |
| 1497 | break; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1501 | return mplsLabel; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1504 | /** |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1505 | * The function checks if given IP matches to the given subnet mask |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1506 | * |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1507 | * @param addr - subnet address to match |
| 1508 | * @param addr1 - IP address to check |
| 1509 | * @return true if the IP address matches to the subnet, otherwise false |
| 1510 | */ |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1511 | public boolean netMatch(String addr, String addr1) { // addr is subnet |
| 1512 | // address and addr1 is |
| 1513 | // ip address. Function |
| 1514 | // will return true, if |
| 1515 | // addr1 is within |
| 1516 | // addr(subnet) |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1517 | |
| 1518 | String[] parts = addr.split("/"); |
| 1519 | String ip = parts[0]; |
| 1520 | int prefix; |
| 1521 | |
| 1522 | if (parts.length < 2) { |
| 1523 | prefix = 0; |
| 1524 | } else { |
| 1525 | prefix = Integer.parseInt(parts[1]); |
| 1526 | } |
| 1527 | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1528 | Inet4Address a = null; |
| 1529 | Inet4Address a1 = null; |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1530 | try { |
| 1531 | a = (Inet4Address) InetAddress.getByName(ip); |
| 1532 | a1 = (Inet4Address) InetAddress.getByName(addr1); |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1533 | } catch (UnknownHostException e) { |
| 1534 | } |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1535 | |
| 1536 | byte[] b = a.getAddress(); |
| 1537 | int ipInt = ((b[0] & 0xFF) << 24) | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1538 | ((b[1] & 0xFF) << 16) | |
| 1539 | ((b[2] & 0xFF) << 8) | |
| 1540 | ((b[3] & 0xFF) << 0); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1541 | |
| 1542 | byte[] b1 = a1.getAddress(); |
| 1543 | int ipInt1 = ((b1[0] & 0xFF) << 24) | |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1544 | ((b1[1] & 0xFF) << 16) | |
| 1545 | ((b1[2] & 0xFF) << 8) | |
| 1546 | ((b1[3] & 0xFF) << 0); |
Sangho Shin | 1aa9354 | 2014-09-22 09:49:44 -0700 | [diff] [blame] | 1547 | |
| 1548 | int mask = ~((1 << (32 - prefix)) - 1); |
| 1549 | |
| 1550 | if ((ipInt & mask) == (ipInt1 & mask)) { |
| 1551 | return true; |
| 1552 | } |
| 1553 | else { |
| 1554 | return false; |
| 1555 | } |
| 1556 | } |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1557 | |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 1558 | /** |
| 1559 | * Add a routing rule for the host |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1560 | * |
Sangho Shin | ac5ee2b | 2014-09-28 21:27:20 -0700 | [diff] [blame] | 1561 | * @param sw - Switch to add the rule |
| 1562 | * @param hostIpAddress Destination host IP address |
| 1563 | * @param hostMacAddress Destination host MAC address |
| 1564 | */ |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1565 | public void addRouteToHost(Switch sw, int hostIpAddress, byte[] hostMacAddress) { |
| 1566 | ipHandler.addRouteToHost(sw, hostIpAddress, hostMacAddress); |
Sangho Shin | eb08303 | 2014-09-22 16:11:34 -0700 | [diff] [blame] | 1567 | } |
Sangho Shin | 9c0f4c3 | 2014-09-26 16:02:38 -0700 | [diff] [blame] | 1568 | |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1569 | /** |
| 1570 | * Add IP packet to a buffer queue |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1571 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1572 | * @param ipv4 |
| 1573 | */ |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1574 | public void addPacketToPacketBuffer(IPv4 ipv4) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 1575 | ipPacketQueue.add(ipv4); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /** |
| 1579 | * Retrieve all packets whose destination is the given address. |
Sangho Shin | fbc572c | 2014-10-02 16:37:05 -0700 | [diff] [blame] | 1580 | * |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1581 | * @param destIp Destination address of packets to retrieve |
| 1582 | */ |
| 1583 | public List<IPv4> getIpPacketFromQueue(byte[] destIp) { |
| 1584 | |
| 1585 | List<IPv4> bufferedPackets = new ArrayList<IPv4>(); |
| 1586 | |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 1587 | if (!ipPacketQueue.isEmpty()) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1588 | for (IPv4 ip : ipPacketQueue) { |
Sangho Shin | 6153540 | 2014-10-01 11:37:14 -0700 | [diff] [blame] | 1589 | int dest = ip.getDestinationAddress(); |
| 1590 | IPv4Address ip1 = IPv4Address.of(dest); |
| 1591 | IPv4Address ip2 = IPv4Address.of(destIp); |
| 1592 | if (ip1.equals(ip2)) { |
Srikanth Vavilapalli | f25c7b0 | 2014-10-01 14:30:43 -0700 | [diff] [blame] | 1593 | bufferedPackets.add((IPv4) (ipPacketQueue.poll()).clone()); |
Sangho Shin | 463bee5 | 2014-09-29 15:14:43 -0700 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | return bufferedPackets; |
| 1599 | } |
| 1600 | |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1601 | /** |
| 1602 | * Get MAC address to known hosts |
| 1603 | * |
| 1604 | * @param destinationAddress IP address to get MAC address |
| 1605 | * @return MAC Address to given IP address |
| 1606 | */ |
| 1607 | public byte[] getMacAddressFromIpAddress(int destinationAddress) { |
| 1608 | |
| 1609 | // Can't we get the host IP address from the TopologyService ?? |
| 1610 | |
| 1611 | Iterator<ArpEntry> iterator = arpEntries.iterator(); |
| 1612 | |
| 1613 | IPv4Address ipAddress = IPv4Address.of(destinationAddress); |
| 1614 | byte[] ipAddressInByte = ipAddress.getBytes(); |
| 1615 | |
| 1616 | while (iterator.hasNext()) { |
| 1617 | ArpEntry arpEntry = iterator.next(); |
| 1618 | byte[] address = arpEntry.targetIpAddress; |
| 1619 | |
| 1620 | IPv4Address a = IPv4Address.of(address); |
| 1621 | IPv4Address b = IPv4Address.of(ipAddressInByte); |
| 1622 | |
| 1623 | if (a.equals(b)) { |
| 1624 | log.debug("Found an arp entry"); |
| 1625 | return arpEntry.targetMacAddress; |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | return null; |
| 1630 | } |
| 1631 | |
| 1632 | /** |
| 1633 | * Send an ARP request via ArpHandler |
| 1634 | * |
| 1635 | * @param destinationAddress |
| 1636 | * @param sw |
| 1637 | * @param inPort |
| 1638 | * |
| 1639 | */ |
| 1640 | public void sendArpRequest(Switch sw, int destinationAddress, Port inPort) { |
| 1641 | arpHandler.sendArpRequest(sw, destinationAddress, inPort); |
| 1642 | } |
| 1643 | |
| 1644 | |
| 1645 | // ************************************ |
| 1646 | // Test functions |
| 1647 | // ************************************ |
| 1648 | |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1649 | private void runTest() { |
| 1650 | |
| 1651 | if (testMode == POLICY_ADD1) { |
| 1652 | String[] routeArray = {"101", "105", "110"}; |
| 1653 | List<Dpid> routeList = new ArrayList<Dpid>(); |
| 1654 | for (int i = 0; i < routeArray.length; i++) { |
| 1655 | Dpid dpid = getSwitchFromNodeId(routeArray[i]).getDpid(); |
| 1656 | routeList.add(dpid); |
| 1657 | } |
| 1658 | |
| 1659 | if (createTunnel("1", routeList)) { |
| 1660 | IPv4Net srcIp = new IPv4Net("10.0.1.1/24"); |
| 1661 | IPv4Net dstIp = new IPv4Net("10.1.2.1/24"); |
| 1662 | |
| 1663 | log.debug("Set the policy 1"); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1664 | this.createPolicy("1", null, null, Ethernet.TYPE_IPV4, srcIp, |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1665 | dstIp, IPv4.PROTOCOL_ICMP, (short)-1, (short)-1, 10000, |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1666 | "1"); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1667 | testMode = POLICY_ADD2; |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1668 | testTask.reschedule(5, TimeUnit.SECONDS); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1669 | } |
| 1670 | else { |
| 1671 | // retry it |
| 1672 | testTask.reschedule(5, TimeUnit.SECONDS); |
| 1673 | } |
| 1674 | } |
| 1675 | else if (testMode == POLICY_ADD2) { |
| 1676 | String[] routeArray = {"101", "102", "103", "104", "105", "108", |
| 1677 | "110"}; |
| 1678 | List<Dpid> routeList = new ArrayList<Dpid>(); |
| 1679 | for (int i = 0; i < routeArray.length; i++) { |
| 1680 | Dpid dpid = getSwitchFromNodeId(routeArray[i]).getDpid(); |
| 1681 | routeList.add(dpid); |
| 1682 | } |
| 1683 | |
| 1684 | if (createTunnel("2", routeList)) { |
| 1685 | IPv4Net srcIp = new IPv4Net("10.0.1.1/24"); |
| 1686 | IPv4Net dstIp = new IPv4Net("10.1.2.1/24"); |
| 1687 | |
| 1688 | log.debug("Set the policy 2"); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1689 | this.createPolicy("2", null, null, Ethernet.TYPE_IPV4, srcIp, |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1690 | dstIp, IPv4.PROTOCOL_ICMP, (short)-1, (short)-1, 20000, |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1691 | "2"); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1692 | testMode = POLICY_REMOVE2; |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1693 | testTask.reschedule(5, TimeUnit.SECONDS); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1694 | } |
| 1695 | else { |
| 1696 | log.debug("Retry it"); |
| 1697 | testTask.reschedule(5, TimeUnit.SECONDS); |
| 1698 | } |
| 1699 | } |
| 1700 | else if (testMode == POLICY_REMOVE2){ |
| 1701 | log.debug("Remove the policy 2"); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1702 | this.removePolicy("2"); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1703 | testMode = POLICY_REMOVE1; |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1704 | testTask.reschedule(5, TimeUnit.SECONDS); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1705 | } |
| 1706 | else if (testMode == POLICY_REMOVE1){ |
| 1707 | log.debug("Remove the policy 1"); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1708 | this.removePolicy("1"); |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1709 | |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1710 | testMode = TUNNEL_REMOVE1; |
| 1711 | testTask.reschedule(5, TimeUnit.SECONDS); |
| 1712 | } |
| 1713 | else if (testMode == TUNNEL_REMOVE1) { |
| 1714 | log.debug("Remove the tunnel 1"); |
| 1715 | this.removeTunnel("1"); |
| 1716 | |
| 1717 | testMode = TUNNEL_REMOVE2; |
| 1718 | testTask.reschedule(5, TimeUnit.SECONDS); |
| 1719 | } |
| 1720 | else if (testMode == TUNNEL_REMOVE2) { |
| 1721 | log.debug("Remove the tunnel 2"); |
| 1722 | this.removeTunnel("2"); |
| 1723 | log.debug("The end of test"); |
| 1724 | } |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1725 | } |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1726 | |
| 1727 | private void runTest1() { |
| 1728 | |
| 1729 | String dpid1 = "00:00:00:00:00:00:00:01"; |
| 1730 | String dpid2 = "00:00:00:00:00:00:00:0a"; |
| 1731 | Switch srcSw = mutableTopology.getSwitch(new Dpid(dpid1)); |
| 1732 | Switch dstSw = mutableTopology.getSwitch(new Dpid(dpid2)); |
| 1733 | |
| 1734 | if (srcSw == null || dstSw == null) { |
| 1735 | testTask.reschedule(1, TimeUnit.SECONDS); |
| 1736 | log.debug("Switch is gone. Reschedule the test"); |
| 1737 | return; |
| 1738 | } |
| 1739 | |
| 1740 | String[] routeArray = {"101", "102", "105", "108", "110"}; |
| 1741 | List<String> routeList = new ArrayList<String>(); |
| 1742 | for (int i = 0; i < routeArray.length; i++) |
| 1743 | routeList.add(routeArray[i]); |
| 1744 | |
| 1745 | List<String> optimizedRoute = this.getOptimizedPath(srcSw, dstSw, routeList); |
| 1746 | |
| 1747 | log.debug("Test set is {}", routeList.toString()); |
| 1748 | log.debug("Result set is {}", optimizedRoute.toString()); |
| 1749 | |
| 1750 | |
| 1751 | } |
| 1752 | |
| 1753 | /** |
| 1754 | * print tunnel info - used only for debugging. |
| 1755 | * @param targetSw |
| 1756 | * |
| 1757 | * @param fwdSwDpids |
| 1758 | * @param ids |
| 1759 | * @param tunnelId |
| 1760 | */ |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1761 | private void printTunnelInfo(IOF13Switch targetSw, String tunnelId, |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1762 | List<String> ids, NeighborSet ns) { |
| 1763 | StringBuilder logStr = new StringBuilder("In switch " + |
| 1764 | targetSw.getId() + ", create a tunnel " + tunnelId + " " + " of push "); |
| 1765 | for (String id: ids) |
| 1766 | logStr.append(id + "-"); |
| 1767 | logStr.append(" output to "); |
| 1768 | for (Dpid dpid: ns.getDpids()) |
| 1769 | logStr.append(dpid + " - "); |
| 1770 | |
| 1771 | log.debug(logStr.toString()); |
| 1772 | |
| 1773 | } |
| 1774 | |
| 1775 | /** |
| 1776 | * Debugging function to print out the Match Action Entry |
| 1777 | * @param sw13 |
| 1778 | * |
| 1779 | * @param maEntry |
| 1780 | */ |
| 1781 | private void printMatchActionOperationEntry( |
| 1782 | IOF13Switch sw13, MatchActionOperationEntry maEntry) { |
| 1783 | |
| 1784 | StringBuilder logStr = new StringBuilder("In switch " + sw13.getId() + ", "); |
| 1785 | |
| 1786 | MatchAction ma = maEntry.getTarget(); |
| 1787 | Match m = ma.getMatch(); |
| 1788 | List<Action> actions = ma.getActions(); |
| 1789 | |
| 1790 | if (m instanceof Ipv4Match) { |
| 1791 | logStr.append("If the IP matches with "); |
| 1792 | IPv4Net ip = ((Ipv4Match) m).getDestination(); |
| 1793 | logStr.append(ip.toString()); |
| 1794 | logStr.append(" then "); |
| 1795 | } |
| 1796 | else if (m instanceof MplsMatch) { |
| 1797 | logStr.append("If the MPLS label matches with "); |
| 1798 | int mplsLabel = ((MplsMatch) m).getMplsLabel(); |
| 1799 | logStr.append(mplsLabel); |
| 1800 | logStr.append(" then "); |
| 1801 | } |
| 1802 | else if (m instanceof PacketMatch) { |
| 1803 | GroupAction ga = (GroupAction)actions.get(0); |
| 1804 | logStr.append("if the policy match is XXX then go to group " + |
| 1805 | ga.getGroupId()); |
| 1806 | log.debug(logStr.toString()); |
| 1807 | return; |
| 1808 | } |
| 1809 | |
| 1810 | logStr.append(" do { "); |
| 1811 | for (Action action : actions) { |
| 1812 | if (action instanceof CopyTtlInAction) { |
| 1813 | logStr.append("copy ttl In, "); |
| 1814 | } |
| 1815 | else if (action instanceof CopyTtlOutAction) { |
| 1816 | logStr.append("copy ttl Out, "); |
| 1817 | } |
| 1818 | else if (action instanceof DecMplsTtlAction) { |
| 1819 | logStr.append("Dec MPLS TTL , "); |
| 1820 | } |
| 1821 | else if (action instanceof GroupAction) { |
| 1822 | logStr.append("Forward packet to < "); |
| 1823 | NeighborSet dpids = ((GroupAction) action).getDpids(); |
| 1824 | logStr.append(dpids.toString() + ","); |
| 1825 | |
| 1826 | } |
| 1827 | else if (action instanceof PopMplsAction) { |
| 1828 | logStr.append("Pop MPLS label, "); |
| 1829 | } |
| 1830 | else if (action instanceof PushMplsAction) { |
| 1831 | logStr.append("Push MPLS label, "); |
| 1832 | } |
| 1833 | else if (action instanceof SetMplsIdAction) { |
| 1834 | int id = ((SetMplsIdAction) action).getMplsId(); |
| 1835 | logStr.append("Set MPLS ID as " + id + ", "); |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | log.debug(logStr.toString()); |
| 1840 | |
| 1841 | } |
| 1842 | |
| 1843 | |
| 1844 | // ************************************ |
| 1845 | // Unused classes and functions |
| 1846 | // ************************************ |
| 1847 | |
| 1848 | /** |
| 1849 | * Temporary class to to keep ARP entry |
| 1850 | * |
| 1851 | */ |
| 1852 | private class ArpEntry { |
| 1853 | |
| 1854 | byte[] targetMacAddress; |
| 1855 | byte[] targetIpAddress; |
| 1856 | |
| 1857 | private ArpEntry(byte[] macAddress, byte[] ipAddress) { |
| 1858 | this.targetMacAddress = macAddress; |
| 1859 | this.targetIpAddress = ipAddress; |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | /** |
| 1864 | * This class is used only for link recovery optimization in |
| 1865 | * modifyEcmpRoutingRules() function. |
| 1866 | * TODO: please remove if the optimization is not used at all |
| 1867 | */ |
| 1868 | private class SwitchPair { |
| 1869 | private Switch src; |
| 1870 | private Switch dst; |
| 1871 | |
| 1872 | public SwitchPair(Switch src, Switch dst) { |
| 1873 | this.src = src; |
| 1874 | this.dst = dst; |
| 1875 | } |
| 1876 | |
| 1877 | public Switch getSource() { |
| 1878 | return src; |
| 1879 | } |
| 1880 | |
| 1881 | public Switch getDestination() { |
| 1882 | return dst; |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | /** |
| 1887 | * Update ARP Cache using ARP packets It is used to set destination MAC |
| 1888 | * address to forward packets to known hosts. But, it will be replace with |
| 1889 | * Host information of Topology service later. |
| 1890 | * |
| 1891 | * @param arp APR packets to use for updating ARP entries |
| 1892 | */ |
| 1893 | public void updateArpCache(ARP arp) { |
| 1894 | |
| 1895 | ArpEntry arpEntry = new ArpEntry(arp.getSenderHardwareAddress(), |
| 1896 | arp.getSenderProtocolAddress()); |
| 1897 | // TODO: Need to check the duplication |
| 1898 | arpEntries.add(arpEntry); |
| 1899 | } |
| 1900 | |
| 1901 | /** |
| 1902 | * Modify the routing rules for the lost links |
| 1903 | * - Recompute the path if the link failed is included in the path |
| 1904 | * (including src and dest). |
| 1905 | * |
| 1906 | * @param newLink |
| 1907 | */ |
| 1908 | private void modifyEcmpRoutingRules(LinkData linkRemoved) { |
| 1909 | |
| 1910 | //HashMap<Switch, SwitchPair> linksToRecompute = new HashMap<Switch, SwitchPair>(); |
| 1911 | Set<SwitchPair> linksToRecompute = new HashSet<SwitchPair>(); |
| 1912 | |
| 1913 | for (ECMPShortestPathGraph ecmpSPG : graphs.values()) { |
| 1914 | Switch rootSw = ecmpSPG.getRootSwitch(); |
| 1915 | HashMap<Integer, HashMap<Switch, ArrayList<Path>>> paths = |
| 1916 | ecmpSPG.getCompleteLearnedSwitchesAndPaths(); |
| 1917 | for (HashMap<Switch, ArrayList<Path>> p: paths.values()) { |
| 1918 | for (Switch destSw: p.keySet()) { |
| 1919 | ArrayList<Path> path = p.get(destSw); |
| 1920 | if (checkPath(path, linkRemoved)) { |
| 1921 | boolean found = false; |
| 1922 | for (SwitchPair pair: linksToRecompute) { |
| 1923 | if (pair.getSource().getDpid() == rootSw.getDpid() && |
| 1924 | pair.getSource().getDpid() == destSw.getDpid()) { |
| 1925 | found = true; |
| 1926 | } |
| 1927 | } |
| 1928 | if (!found) { |
| 1929 | linksToRecompute.add(new SwitchPair(rootSw, destSw)); |
| 1930 | } |
| 1931 | } |
| 1932 | } |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | // Recompute the path for the specific route |
| 1937 | for (SwitchPair pair: linksToRecompute) { |
| 1938 | |
| 1939 | log.debug("Recompute path from {} to {}", pair.getSource(), pair.getDestination()); |
| 1940 | // We need the following function for optimization |
| 1941 | //ECMPShortestPathGraph ecmpSPG = |
| 1942 | // new ECMPShortestPathGraph(pair.getSource(), pair.getDestination()); |
| 1943 | ECMPShortestPathGraph ecmpSPG = |
| 1944 | new ECMPShortestPathGraph(pair.getSource()); |
| 1945 | populateEcmpRoutingRulesForPath(pair.getSource(), ecmpSPG, true); |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | /** |
| 1950 | * Optimize the mpls label |
| 1951 | * The feature will be used only for policy of "avoid a specific switch". |
| 1952 | * Check route to each router in route backward. |
| 1953 | * If there is only one route to the router and the routers are included in |
| 1954 | * the route, remove the id from the path. |
| 1955 | * A-B-C-D-E => A-B-C-D-E -> A-E |
| 1956 | * | | => A-B-H-I -> A-I |
Sangho Shin | 5b8f545 | 2014-10-20 11:46:01 -0700 | [diff] [blame] | 1957 | * F-G-H-I => A-D-I > A-D-I |
Sangho Shin | 7330c03 | 2014-10-20 10:34:51 -0700 | [diff] [blame] | 1958 | */ |
| 1959 | private List<String> getOptimizedPath(Switch srcSw, Switch dstSw, List<String> route) { |
| 1960 | |
| 1961 | List<String> optimizedPath = new ArrayList<String>(); |
| 1962 | optimizedPath.addAll(route); |
| 1963 | ECMPShortestPathGraph ecmpSPG = new ECMPShortestPathGraph(srcSw); |
| 1964 | |
| 1965 | HashMap<Integer, HashMap<Switch, ArrayList<Path>>> paths = |
| 1966 | ecmpSPG.getCompleteLearnedSwitchesAndPaths(); |
| 1967 | for (HashMap<Switch, ArrayList<Path>> p: paths.values()) { |
| 1968 | for (Switch s: p.keySet()) { |
| 1969 | if (s.getDpid().toString().equals(dstSw.getDpid().toString())) { |
| 1970 | ArrayList<Path> ecmpPaths = p.get(s); |
| 1971 | if (ecmpPaths!= null && ecmpPaths.size() == 1) { |
| 1972 | for (Path path: ecmpPaths) { |
| 1973 | for (LinkData link: path) { |
| 1974 | String srcId = getMplsLabel(link.getSrc().getDpid().toString()); |
| 1975 | String dstId = getMplsLabel(link.getSrc().getDpid().toString()); |
| 1976 | if (optimizedPath.contains(srcId)) { |
| 1977 | optimizedPath.remove(srcId); |
| 1978 | } |
| 1979 | if (optimizedPath.contains(dstId)) { |
| 1980 | optimizedPath.remove(dstId); |
| 1981 | } |
| 1982 | } |
| 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | return optimizedPath; |
| 1990 | |
| 1991 | } |
| 1992 | |
| 1993 | /** |
| 1994 | * Check if the path is affected from the link removed |
| 1995 | * |
| 1996 | * @param path Path to check |
| 1997 | * @param linkRemoved link removed |
| 1998 | * @return true if the path contains the link removed |
| 1999 | */ |
| 2000 | private boolean checkPath(ArrayList<Path> path, LinkData linkRemoved) { |
| 2001 | |
| 2002 | for (Path ppp: path) { |
| 2003 | // TODO: need to check if this is a bidirectional or |
| 2004 | // unidirectional |
| 2005 | for (LinkData link: ppp) { |
| 2006 | if (link.getDst().getDpid().equals(linkRemoved.getDst().getDpid()) && |
| 2007 | link.getSrc().getDpid().equals(linkRemoved.getSrc().getDpid())) |
| 2008 | return true; |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | return false; |
| 2013 | } |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 2014 | |
| 2015 | |
Sangho Shin | 2f26369 | 2014-09-15 14:09:41 -0700 | [diff] [blame] | 2016 | } |