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