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