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