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