blob: a93a730db99f906b85d6d37e4afdaebd5f04d39d [file] [log] [blame]
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001package net.onrc.onos.core.drivermanager;
2
3import java.io.IOException;
4import java.util.ArrayList;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07005import java.util.Collection;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07006import java.util.Collections;
Saurav Das2d41a432014-10-21 20:40:10 -07007import java.util.HashMap;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07008import java.util.HashSet;
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -07009import java.util.Iterator;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070010import java.util.List;
Saurav Das2d41a432014-10-21 20:40:10 -070011import java.util.Map;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -070012import java.util.Set;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070013import java.util.concurrent.ConcurrentHashMap;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -070014import java.util.concurrent.ConcurrentMap;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070015import java.util.concurrent.atomic.AtomicBoolean;
Srikanth Vavilapalli25fbe392014-10-10 15:04:51 -070016import java.util.concurrent.atomic.AtomicInteger;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070017
18import net.floodlightcontroller.core.IFloodlightProviderService.Role;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -070019import net.floodlightcontroller.core.IOF13Switch;
20import net.floodlightcontroller.core.IOFSwitch;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070021import net.floodlightcontroller.core.SwitchDriverSubHandshakeAlreadyStarted;
22import net.floodlightcontroller.core.SwitchDriverSubHandshakeCompleted;
23import net.floodlightcontroller.core.SwitchDriverSubHandshakeNotStarted;
24import net.floodlightcontroller.core.internal.OFSwitchImplBase;
Saurav Dase972b3a2014-09-26 15:41:06 -070025import net.floodlightcontroller.util.MACAddress;
Srikanth Vavilapalli25fbe392014-10-10 15:04:51 -070026import net.floodlightcontroller.util.OrderedCollection;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -070027import net.onrc.onos.core.configmanager.INetworkConfigService;
28import net.onrc.onos.core.configmanager.INetworkConfigService.NetworkConfigState;
29import net.onrc.onos.core.configmanager.INetworkConfigService.SwitchConfigStatus;
30import net.onrc.onos.core.configmanager.NetworkConfig.LinkConfig;
Srikanth Vavilapalli68144302014-10-08 15:55:24 -070031import net.onrc.onos.core.configmanager.NetworkConfig.SwitchConfig;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -070032import net.onrc.onos.core.configmanager.NetworkConfigManager;
33import net.onrc.onos.core.configmanager.PktLinkConfig;
34import net.onrc.onos.core.configmanager.SegmentRouterConfig;
Saurav Das2d41a432014-10-21 20:40:10 -070035import net.onrc.onos.core.configmanager.SegmentRouterConfig.AdjacencySid;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -070036import net.onrc.onos.core.matchaction.MatchAction;
37import net.onrc.onos.core.matchaction.MatchActionOperationEntry;
Saurav Dasbc594a42014-09-25 20:13:50 -070038import net.onrc.onos.core.matchaction.MatchActionOperations;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -070039import net.onrc.onos.core.matchaction.MatchActionOperations.Operator;
40import net.onrc.onos.core.matchaction.action.Action;
41import net.onrc.onos.core.matchaction.action.CopyTtlInAction;
42import net.onrc.onos.core.matchaction.action.CopyTtlOutAction;
43import net.onrc.onos.core.matchaction.action.DecMplsTtlAction;
44import net.onrc.onos.core.matchaction.action.DecNwTtlAction;
45import net.onrc.onos.core.matchaction.action.GroupAction;
46import net.onrc.onos.core.matchaction.action.ModifyDstMacAction;
47import net.onrc.onos.core.matchaction.action.ModifySrcMacAction;
48import net.onrc.onos.core.matchaction.action.OutputAction;
49import net.onrc.onos.core.matchaction.action.PopMplsAction;
Saurav Dasa962a692014-10-17 14:52:38 -070050import net.onrc.onos.core.matchaction.action.PushMplsAction;
Sangho Shine842cad2014-10-24 16:07:35 -070051import net.onrc.onos.core.matchaction.action.SetDAAction;
Saurav Dasa962a692014-10-17 14:52:38 -070052import net.onrc.onos.core.matchaction.action.SetMplsBosAction;
53import net.onrc.onos.core.matchaction.action.SetMplsIdAction;
Sangho Shine842cad2014-10-24 16:07:35 -070054import net.onrc.onos.core.matchaction.action.SetSAAction;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -070055import net.onrc.onos.core.matchaction.match.Ipv4Match;
56import net.onrc.onos.core.matchaction.match.Match;
57import net.onrc.onos.core.matchaction.match.MplsMatch;
58import net.onrc.onos.core.matchaction.match.PacketMatch;
59import net.onrc.onos.core.util.Dpid;
60import net.onrc.onos.core.util.IPv4Net;
61import net.onrc.onos.core.util.PortNumber;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070062
Saurav Das2d41a432014-10-21 20:40:10 -070063import org.codehaus.jackson.map.ObjectMapper;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070064import org.projectfloodlight.openflow.protocol.OFAsyncGetReply;
65import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
66import org.projectfloodlight.openflow.protocol.OFBucket;
67import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
68import org.projectfloodlight.openflow.protocol.OFErrorMsg;
69import org.projectfloodlight.openflow.protocol.OFFactory;
Saurav Dase972b3a2014-09-26 15:41:06 -070070import org.projectfloodlight.openflow.protocol.OFFlowMod;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070071import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply;
72import org.projectfloodlight.openflow.protocol.OFGroupFeaturesStatsReply;
73import org.projectfloodlight.openflow.protocol.OFGroupType;
74import org.projectfloodlight.openflow.protocol.OFMatchV3;
75import org.projectfloodlight.openflow.protocol.OFMessage;
76import org.projectfloodlight.openflow.protocol.OFOxmList;
77import org.projectfloodlight.openflow.protocol.OFPortDesc;
Srikanth Vavilapalli25fbe392014-10-10 15:04:51 -070078import org.projectfloodlight.openflow.protocol.OFPortStatus;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070079import org.projectfloodlight.openflow.protocol.OFStatsReply;
80import org.projectfloodlight.openflow.protocol.action.OFAction;
81import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Saurav Dase972b3a2014-09-26 15:41:06 -070082import org.projectfloodlight.openflow.protocol.match.Match.Builder;
83import org.projectfloodlight.openflow.protocol.match.MatchField;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070084import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthDst;
85import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthSrc;
86import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthType;
87import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort;
88import org.projectfloodlight.openflow.protocol.oxm.OFOxmIpv4DstMasked;
Saurav Dascc3e35f2014-10-10 15:33:32 -070089import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsBos;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070090import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel;
91import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
92import org.projectfloodlight.openflow.types.EthType;
93import org.projectfloodlight.openflow.types.IPv4Address;
Saurav Dase972b3a2014-09-26 15:41:06 -070094import org.projectfloodlight.openflow.types.IpProtocol;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070095import org.projectfloodlight.openflow.types.MacAddress;
Saurav Dascc3e35f2014-10-10 15:33:32 -070096import org.projectfloodlight.openflow.types.OFBooleanValue;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070097import org.projectfloodlight.openflow.types.OFBufferId;
98import org.projectfloodlight.openflow.types.OFGroup;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070099import org.projectfloodlight.openflow.types.OFPort;
100import org.projectfloodlight.openflow.types.OFVlanVidMatch;
101import org.projectfloodlight.openflow.types.TableId;
Saurav Dase972b3a2014-09-26 15:41:06 -0700102import org.projectfloodlight.openflow.types.TransportPort;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700103import org.projectfloodlight.openflow.types.U32;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700104import org.projectfloodlight.openflow.util.HexString;
105
106/**
107 * OFDescriptionStatistics Vendor (Manufacturer Desc.): Stanford University,
108 * Ericsson Research and CPqD Research. Make (Hardware Desc.) : OpenFlow 1.3
109 * Reference Userspace Switch Model (Datapath Desc.) : None Software : Serial :
110 * None
111 */
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700112public class OFSwitchImplCPqD13 extends OFSwitchImplBase implements IOF13Switch {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700113 private AtomicBoolean driverHandshakeComplete;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700114 private AtomicBoolean haltStateMachine;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700115 private OFFactory factory;
116 private static final int OFPCML_NO_BUFFER = 0xffff;
117 // Configuration of asynch messages to controller. We need different
118 // asynch messages depending on role-equal or role-master.
119 // We don't want to get anything if we are slave.
120 private static final long SET_FLOW_REMOVED_MASK_MASTER = 0xf;
121 private static final long SET_PACKET_IN_MASK_MASTER = 0x7;
122 private static final long SET_PORT_STATUS_MASK_MASTER = 0x7;
123 private static final long SET_FLOW_REMOVED_MASK_EQUAL = 0x0;
124 private static final long SET_PACKET_IN_MASK_EQUAL = 0x0;
125 private static final long SET_PORT_STATUS_MASK_EQUAL = 0x7;
126 private static final long SET_ALL_SLAVE = 0x0;
127
128 private static final long TEST_FLOW_REMOVED_MASK = 0xf;
129 private static final long TEST_PACKET_IN_MASK = 0x7;
130 private static final long TEST_PORT_STATUS_MASK = 0x7;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700131
132 private static final int TABLE_VLAN = 0;
133 private static final int TABLE_TMAC = 1;
Sangho Shin01bca862014-09-12 11:18:59 -0700134 private static final int TABLE_IPv4_UNICAST = 2;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700135 private static final int TABLE_MPLS = 3;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700136 private static final int TABLE_ACL = 5;
137
138 private static final short MAX_PRIORITY = (short) 0xffff;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700139 private static final short PRIORITY_MULTIPLIER = (short) 2046;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700140 private static final short MIN_PRIORITY = 0x0;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700141
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700142 private long barrierXidToWaitFor = -1;
143 private DriverState driverState;
Jonathan Hartcb34f382014-08-12 21:11:03 -0700144 private final boolean usePipeline13;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700145 private SegmentRouterConfig srConfig;
146 private ConcurrentMap<Dpid, Set<PortNumber>> neighbors;
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700147 private ConcurrentMap<PortNumber, Dpid> portToNeighbors;
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700148 private List<Integer> segmentIds;
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700149 private boolean isEdgeRouter;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700150 private ConcurrentMap<NeighborSet, EcmpInfo> ecmpGroups;
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -0700151 private ConcurrentMap<Integer, EcmpInfo> userDefinedGroups;
Srikanth Vavilapallib95e6e02014-10-02 13:56:39 -0700152 private ConcurrentMap<PortNumber, ArrayList<NeighborSet>> portNeighborSetMap;
Srikanth Vavilapalli25fbe392014-10-10 15:04:51 -0700153 private AtomicInteger groupid;
Saurav Das2d41a432014-10-21 20:40:10 -0700154 private Map<String, String> publishAttributes;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700155
Jonathan Hartcb34f382014-08-12 21:11:03 -0700156 public OFSwitchImplCPqD13(OFDescStatsReply desc, boolean usePipeline13) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700157 super();
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700158 haltStateMachine = new AtomicBoolean(false);
159 driverState = DriverState.INIT;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700160 driverHandshakeComplete = new AtomicBoolean(false);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700161 setSwitchDescription(desc);
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700162 neighbors = new ConcurrentHashMap<Dpid, Set<PortNumber>>();
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700163 portToNeighbors = new ConcurrentHashMap<PortNumber, Dpid>();
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700164 ecmpGroups = new ConcurrentHashMap<NeighborSet, EcmpInfo>();
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -0700165 userDefinedGroups = new ConcurrentHashMap<Integer, EcmpInfo>();
Srikanth Vavilapallib95e6e02014-10-02 13:56:39 -0700166 portNeighborSetMap =
167 new ConcurrentHashMap<PortNumber, ArrayList<NeighborSet>>();
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700168 segmentIds = new ArrayList<Integer>();
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700169 isEdgeRouter = false;
Srikanth Vavilapalli25fbe392014-10-10 15:04:51 -0700170 groupid = new AtomicInteger(0);
Jonathan Hartcb34f382014-08-12 21:11:03 -0700171 this.usePipeline13 = usePipeline13;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700172 }
173
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700174 // *****************************
175 // OFSwitchImplBase
176 // *****************************
177
178
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700179 /* (non-Javadoc)
180 * @see java.lang.Object#toString()
181 */
182 @Override
183 public String toString() {
184 return "OFSwitchImplCPqD13 [" + ((channel != null)
185 ? channel.getRemoteAddress() : "?")
186 + " DPID[" + ((stringId != null) ? stringId : "?") + "]]";
187 }
188
189 @Override
190 public void startDriverHandshake() throws IOException {
191 log.debug("Starting driver handshake for sw {}", getStringId());
192 if (startDriverHandshakeCalled) {
193 throw new SwitchDriverSubHandshakeAlreadyStarted();
194 }
195 startDriverHandshakeCalled = true;
Jonathan Harta213bce2014-08-11 15:44:07 -0700196 factory = getFactory();
Jonathan Hartcb34f382014-08-12 21:11:03 -0700197 if (!usePipeline13) {
198 // Send packet-in to controller if a packet misses the first table
199 populateTableMissEntry(0, true, false, false, 0);
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700200 driverHandshakeComplete.set(true);
Sangho Shin01bca862014-09-12 11:18:59 -0700201 } else {
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700202 nextDriverState();
Sangho Shin01bca862014-09-12 11:18:59 -0700203 }
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700204 }
205
206 @Override
207 public boolean isDriverHandshakeComplete() {
Sangho Shin01bca862014-09-12 11:18:59 -0700208 if (!startDriverHandshakeCalled)
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700209 throw new SwitchDriverSubHandshakeNotStarted();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700210 return driverHandshakeComplete.get();
211 }
212
213 @Override
214 public void processDriverHandshakeMessage(OFMessage m) {
Sangho Shin01bca862014-09-12 11:18:59 -0700215 if (!startDriverHandshakeCalled)
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700216 throw new SwitchDriverSubHandshakeNotStarted();
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700217 if (isDriverHandshakeComplete())
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700218 throw new SwitchDriverSubHandshakeCompleted(m);
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700219 try {
220 processOFMessage(this, m);
221 } catch (IOException e) {
222 log.error("Error generated when processing OFMessage", e.getCause());
223 }
224 }
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700225
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700226 @Override
227 public String getSwitchDriverState() {
228 return driverState.toString();
229 }
230
Srikanth Vavilapallibd9b3e22014-10-02 08:57:46 -0700231 public void removePortFromGroups(PortNumber port) {
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700232 log.debug("removePortFromGroups: Remove port {} from Switch {}",
233 port, getStringId());
234 ArrayList<NeighborSet> portNSSet = portNeighborSetMap.get(port);
235 if (portNSSet == null)
236 {
237 /* No Groups are created with this port yet */
238 log.warn("removePortFromGroups: No groups exist with Switch {} port {}",
Srikanth Vavilapallicf1840b2014-10-09 15:29:51 -0700239 getStringId(), port);
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700240 return;
241 }
242 log.debug("removePortFromGroups: Neighborsets that the port {} is part"
243 + "of on Switch {} are {}",
244 port, getStringId(), portNSSet);
245
246 for (NeighborSet ns : portNSSet) {
247 /* Delete the first matched bucket */
248 EcmpInfo portEcmpInfo = ecmpGroups.get(ns);
249 Iterator<BucketInfo> it = portEcmpInfo.buckets.iterator();
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700250 log.debug("removePortFromGroups: Group {} on Switch {} has {} buckets",
251 portEcmpInfo.groupId, getStringId(),
252 portEcmpInfo.buckets.size());
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700253 while (it.hasNext()) {
254 BucketInfo bucket = it.next();
255 if (bucket.outport.equals(port)) {
256 it.remove();
Srikanth Vavilapallibd9b3e22014-10-02 08:57:46 -0700257 }
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700258 }
259 log.debug("removePortFromGroups: Modifying Group on Switch {} "
260 + "and Neighborset {} with {}",
261 getStringId(), ns, portEcmpInfo);
262 modifyEcmpGroup(portEcmpInfo);
263 }
264 /* Don't delete the entry from portNeighborSetMap because
Srikanth Vavilapallicf1840b2014-10-09 15:29:51 -0700265 * when the port is up again this info is needed
266 */
Srikanth Vavilapalli7d873fd2014-10-04 07:50:57 -0700267 return;
Srikanth Vavilapallibd9b3e22014-10-02 08:57:46 -0700268 }
269
270 public void addPortToGroups(PortNumber port) {
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700271 log.debug("addPortToGroups: Add port {} to Switch {}",
272 port, getStringId());
273 ArrayList<NeighborSet> portNSSet = portNeighborSetMap.get(port);
274 if (portNSSet == null) {
275 /* Unknown Port */
276 log.warn("addPortToGroups: Switch {} port {} is unknown",
Srikanth Vavilapallicf1840b2014-10-09 15:29:51 -0700277 getStringId(), port);
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700278 return;
279 }
280 log.debug("addPortToGroups: Neighborsets that the port {} is part"
281 + "of on Switch {} are {}",
282 port, getStringId(), portNSSet);
283
284 Dpid neighborDpid = portToNeighbors.get(port);
285 for (NeighborSet ns : portNSSet) {
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700286 EcmpInfo portEcmpInfo = ecmpGroups.get(ns);
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700287 /* Find if this port is already part of any bucket
288 * in this group
289 * NOTE: This is needed because in some cases
290 * (such as for configured network nodes), both driver and
291 * application detect the network elements and creates the
292 * buckets in the same group. This check is to avoid
293 * duplicate bucket creation in such scenarios
294 */
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700295 List<BucketInfo> buckets = portEcmpInfo.buckets;
296 if (buckets == null) {
297 buckets = new ArrayList<BucketInfo>();
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700298 portEcmpInfo.buckets = buckets;
299 } else {
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700300 Iterator<BucketInfo> it = buckets.iterator();
301 boolean matchingBucketExist = false;
302 while (it.hasNext()) {
303 BucketInfo bucket = it.next();
304 if (bucket.outport.equals(port)) {
305 matchingBucketExist = true;
306 break;
307 }
308 }
309 if (matchingBucketExist) {
310 log.warn("addPortToGroups: On Switch {} duplicate "
311 + "portAdd is called for port {} with buckets {}",
312 getStringId(), port, buckets);
313 continue;
314 }
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700315 }
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700316 BucketInfo b = new BucketInfo(neighborDpid,
317 MacAddress.of(srConfig.getRouterMac()),
318 getNeighborRouterMacAddress(neighborDpid),
319 port,
Srikanth Vavilapalli16a04722014-10-23 11:09:24 -0700320 ns.getEdgeLabel(), true, -1);
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700321 buckets.add(b);
Srikanth Vavilapallibb47e782014-10-09 18:16:35 -0700322 log.debug("addPortToGroups: Modifying Group on Switch {} "
323 + "and Neighborset {} with {}",
324 getStringId(), ns, portEcmpInfo);
325 modifyEcmpGroup(portEcmpInfo);
326 }
327 return;
Srikanth Vavilapallibd9b3e22014-10-02 08:57:46 -0700328 }
329
Srikanth Vavilapalli25fbe392014-10-10 15:04:51 -0700330 @Override
331 public OrderedCollection<PortChangeEvent> processOFPortStatus(OFPortStatus ps) {
332 OrderedCollection<PortChangeEvent> events = super.processOFPortStatus(ps);
333 for (PortChangeEvent e : events) {
334 switch (e.type) {
335 case DELETE:
336 case DOWN:
337 log.debug("processOFPortStatus: sw {} Port {} DOWN",
338 getStringId(), e.port.getPortNo().getPortNumber());
339 removePortFromGroups(PortNumber.uint32(
340 e.port.getPortNo().getPortNumber()));
341 break;
342 case UP:
343 log.debug("processOFPortStatus: sw {} Port {} UP",
344 getStringId(), e.port.getPortNo().getPortNumber());
345 addPortToGroups(PortNumber.uint32(
346 e.port.getPortNo().getPortNumber()));
347 }
348 }
349 return events;
350 }
351
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700352 // *****************************
353 // Driver handshake state-machine
354 // *****************************
355
356 enum DriverState {
357 INIT,
358 SET_TABLE_MISS_ENTRIES,
359 SET_TABLE_VLAN_TMAC,
360 SET_GROUPS,
361 VERIFY_GROUPS,
362 SET_ADJACENCY_LABELS,
363 EXIT
364 }
365
366 protected void nextDriverState() throws IOException {
367 DriverState currentState = driverState;
Saurav Das80d17392014-10-01 10:24:56 -0700368 if (haltStateMachine.get()) {
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700369 return;
Saurav Das80d17392014-10-01 10:24:56 -0700370 }
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700371 switch (currentState) {
372 case INIT:
373 driverState = DriverState.SET_TABLE_MISS_ENTRIES;
374 setTableMissEntries();
Saurav Dasd84178f2014-09-29 17:48:54 -0700375 sendHandshakeBarrier();
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700376 break;
377 case SET_TABLE_MISS_ENTRIES:
378 driverState = DriverState.SET_TABLE_VLAN_TMAC;
379 getNetworkConfig();
380 populateTableVlan();
381 populateTableTMac();
Saurav Dasd84178f2014-09-29 17:48:54 -0700382 sendHandshakeBarrier();
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700383 break;
384 case SET_TABLE_VLAN_TMAC:
385 driverState = DriverState.SET_GROUPS;
386 createGroups();
Saurav Dasd84178f2014-09-29 17:48:54 -0700387 sendHandshakeBarrier();
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700388 break;
389 case SET_GROUPS:
390 driverState = DriverState.VERIFY_GROUPS;
391 verifyGroups();
392 break;
393 case VERIFY_GROUPS:
394 driverState = DriverState.SET_ADJACENCY_LABELS;
395 assignAdjacencyLabels();
396 break;
397 case SET_ADJACENCY_LABELS:
398 driverState = DriverState.EXIT;
399 driverHandshakeComplete.set(true);
400 break;
401 case EXIT:
402 default:
403 driverState = DriverState.EXIT;
404 log.error("Driver handshake has exited for sw: {}", getStringId());
405 }
406 }
407
408 void processOFMessage(IOFSwitch sw, OFMessage m) throws IOException {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700409 switch (m.getType()) {
410 case BARRIER_REPLY:
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700411 processBarrierReply(m);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700412 break;
413
414 case ERROR:
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700415 processErrorMessage(m);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700416 break;
417
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700418 case GET_ASYNC_REPLY:
419 OFAsyncGetReply asrep = (OFAsyncGetReply) m;
420 decodeAsyncGetReply(asrep);
421 break;
422
423 case PACKET_IN:
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700424 // not ready to handle packet-ins
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700425 break;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700426
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700427 case QUEUE_GET_CONFIG_REPLY:
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700428 // not doing queue config yet
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700429 break;
430
431 case STATS_REPLY:
432 processStatsReply((OFStatsReply) m);
433 break;
434
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700435 case ROLE_REPLY: // channelHandler should handle this
436 case PORT_STATUS: // channelHandler should handle this
437 case FEATURES_REPLY: // don't care
438 case FLOW_REMOVED: // don't care
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700439 default:
440 log.debug("Received message {} during switch-driver subhandshake "
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700441 + "from switch {} ... Ignoring message", m, sw.getStringId());
442 }
443 }
444
445 private void processStatsReply(OFStatsReply sr) {
446 switch (sr.getStatsType()) {
447 case AGGREGATE:
448 break;
449 case DESC:
450 break;
451 case EXPERIMENTER:
452 break;
453 case FLOW:
454 break;
455 case GROUP_DESC:
456 processGroupDesc((OFGroupDescStatsReply) sr);
457 break;
458 case GROUP_FEATURES:
459 processGroupFeatures((OFGroupFeaturesStatsReply) sr);
460 break;
461 case METER_CONFIG:
462 break;
463 case METER_FEATURES:
464 break;
465 case PORT_DESC:
466 break;
467 case TABLE_FEATURES:
468 break;
469 default:
470 break;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700471
472 }
473 }
474
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700475 private void processErrorMessage(OFMessage m) {
476 log.error("Switch {} Error {} in DriverState", getStringId(),
477 (OFErrorMsg) m, driverState);
478 }
479
480 private void processBarrierReply(OFMessage m) throws IOException {
481 if (m.getXid() == barrierXidToWaitFor) {
482 // Driver state-machine progresses to the next state.
483 // If Barrier messages is not received, then eventually
484 // the ChannelHandler state machine will timeout, and the switch
485 // will be disconnected.
486 nextDriverState();
487 } else {
488 log.error("Received incorrect barrier-message xid {} (expected: {}) in "
489 + "switch-driver state {} for switch {}", m, barrierXidToWaitFor,
490 driverState, getStringId());
491 }
492 }
493
494 private void processGroupDesc(OFGroupDescStatsReply gdsr) {
495 log.info("Sw: {} Group Desc {}", getStringId(), gdsr);
Saurav Das2d41a432014-10-21 20:40:10 -0700496 // TODO -- actually do verification
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700497 try {
498 nextDriverState();
499 } catch (IOException e) {
500 // TODO Auto-generated catch block
501 e.printStackTrace();
502 }
503 }
504
505 // *****************************
506 // Utility methods
507 // *****************************
508
509 void setTableMissEntries() throws IOException {
510 // set all table-miss-entries
511 populateTableMissEntry(TABLE_VLAN, true, false, false, -1);
512 populateTableMissEntry(TABLE_TMAC, true, false, false, -1);
513 populateTableMissEntry(TABLE_IPv4_UNICAST, false, true, true,
514 TABLE_ACL);
515 populateTableMissEntry(TABLE_MPLS, false, true, true,
516 TABLE_ACL);
517 populateTableMissEntry(TABLE_ACL, false, false, false, -1);
518 }
519
Saurav Dasd84178f2014-09-29 17:48:54 -0700520 private void sendHandshakeBarrier() throws IOException {
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700521 long xid = getNextTransactionId();
522 barrierXidToWaitFor = xid;
523 OFBarrierRequest br = getFactory()
524 .buildBarrierRequest()
525 .setXid(xid)
526 .build();
527 write(br, null);
528 }
529
530 /**
531 * Adds a table-miss-entry to a pipeline table.
532 * <p>
533 * The table-miss-entry can be added with 'write-actions' or
534 * 'apply-actions'. It can also add a 'goto-table' instruction. By default
535 * if none of the booleans in the call are set, then the table-miss entry is
536 * added with no instructions, which means that if a packet hits the
537 * table-miss-entry, pipeline execution will stop, and the action set
538 * associated with the packet will be executed.
539 *
540 * @param tableToAdd the table to where the table-miss-entry will be added
541 * @param toControllerNow as an APPLY_ACTION instruction
542 * @param toControllerWrite as a WRITE_ACTION instruction
543 * @param toTable as a GOTO_TABLE instruction
544 * @param tableToSend the table to send as per the GOTO_TABLE instruction it
545 * needs to be set if 'toTable' is true. Ignored of 'toTable' is
546 * false.
547 * @throws IOException
548 */
549 @SuppressWarnings("unchecked")
550 private void populateTableMissEntry(int tableToAdd, boolean toControllerNow,
551 boolean toControllerWrite,
552 boolean toTable, int tableToSend) throws IOException {
553 OFOxmList oxmList = OFOxmList.EMPTY;
554 OFMatchV3 match = factory.buildMatchV3()
555 .setOxmList(oxmList)
556 .build();
557 OFAction outc = factory.actions()
558 .buildOutput()
559 .setPort(OFPort.CONTROLLER)
560 .setMaxLen(OFPCML_NO_BUFFER)
561 .build();
562 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
563 if (toControllerNow) {
564 // table-miss instruction to send to controller immediately
565 OFInstruction instr = factory.instructions()
566 .buildApplyActions()
567 .setActions(Collections.singletonList(outc))
568 .build();
569 instructions.add(instr);
570 }
571
572 if (toControllerWrite) {
573 // table-miss instruction to write-action to send to controller
574 // this will be executed whenever the action-set gets executed
575 OFInstruction instr = factory.instructions()
576 .buildWriteActions()
577 .setActions(Collections.singletonList(outc))
578 .build();
579 instructions.add(instr);
580 }
581
582 if (toTable) {
583 // table-miss instruction to goto-table x
584 OFInstruction instr = factory.instructions()
585 .gotoTable(TableId.of(tableToSend));
586 instructions.add(instr);
587 }
588
589 if (!toControllerNow && !toControllerWrite && !toTable) {
590 // table-miss has no instruction - at which point action-set will be
591 // executed - if there is an action to output/group in the action
592 // set
593 // the packet will be sent there, otherwise it will be dropped.
594 instructions = (List<OFInstruction>) Collections.EMPTY_LIST;
595 }
596
597 OFMessage tableMissEntry = factory.buildFlowAdd()
598 .setTableId(TableId.of(tableToAdd))
599 .setMatch(match) // match everything
600 .setInstructions(instructions)
601 .setPriority(MIN_PRIORITY)
602 .setBufferId(OFBufferId.NO_BUFFER)
603 .setIdleTimeout(0)
604 .setHardTimeout(0)
605 .setXid(getNextTransactionId())
606 .build();
607 write(tableMissEntry, null);
608 }
609
610 private void getNetworkConfig() {
611 INetworkConfigService ncs = floodlightProvider.getNetworkConfigService();
612 SwitchConfigStatus scs = ncs.checkSwitchConfig(new Dpid(getId()));
613 if (scs.getConfigState() == NetworkConfigState.ACCEPT_ADD) {
614 srConfig = (SegmentRouterConfig) scs.getSwitchConfig();
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700615 isEdgeRouter = srConfig.isEdgeRouter();
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700616 } else {
617 log.error("Switch not configured as Segment-Router");
618 }
619
620 List<LinkConfig> linkConfigList = ncs.getConfiguredAllowedLinks();
621 setNeighbors(linkConfigList);
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700622
623 if (isEdgeRouter) {
624 List<SwitchConfig> switchList = ncs.getConfiguredAllowedSwitches();
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700625 getAllNodeSegmentIds(switchList);
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700626 }
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700627 }
628
629 private void populateTableVlan() throws IOException {
630 List<OFMessage> msglist = new ArrayList<OFMessage>();
631 for (OFPortDesc p : getPorts()) {
632 int pnum = p.getPortNo().getPortNumber();
633 if (U32.of(pnum).compareTo(U32.of(OFPort.MAX.getPortNumber())) < 1) {
634 OFOxmInPort oxp = factory.oxms().inPort(p.getPortNo());
635 OFOxmVlanVid oxv = factory.oxms()
636 .vlanVid(OFVlanVidMatch.UNTAGGED);
637 OFOxmList oxmList = OFOxmList.of(oxp, oxv);
638 OFMatchV3 match = factory.buildMatchV3()
639 .setOxmList(oxmList).build();
640
641 // TODO: match on vlan-tagged packets for vlans configured on
642 // subnet ports and strip-vlan
643
644 // Do not need to add vlans
645 /*int vlanid = getVlanConfig(pnum);
646 OFOxmVlanVid vidToSet = factory.oxms()
647 .vlanVid(OFVlanVidMatch.ofVlan(vlanid));
648 OFAction pushVlan = factory.actions().pushVlan(EthType.VLAN_FRAME);
649 OFAction setVlan = factory.actions().setField(vidToSet);
650 List<OFAction> actionlist = new ArrayList<OFAction>();
651 actionlist.add(pushVlan);
652 actionlist.add(setVlan);
653 OFInstruction appAction = factory.instructions().buildApplyActions()
654 .setActions(actionlist).build();*/
655
656 OFInstruction gotoTbl = factory.instructions().buildGotoTable()
657 .setTableId(TableId.of(TABLE_TMAC)).build();
658 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
659 // instructions.add(appAction);
660 instructions.add(gotoTbl);
661 OFMessage flowEntry = factory.buildFlowAdd()
662 .setTableId(TableId.of(TABLE_VLAN))
663 .setMatch(match)
664 .setInstructions(instructions)
665 .setPriority(1000) // does not matter - all rules
666 // exclusive
667 .setBufferId(OFBufferId.NO_BUFFER)
668 .setIdleTimeout(0)
669 .setHardTimeout(0)
670 .setXid(getNextTransactionId())
671 .build();
672 msglist.add(flowEntry);
673 }
674 }
675 write(msglist);
676 log.debug("Adding {} port/vlan-rules in sw {}", msglist.size(), getStringId());
677 }
678
679 private void populateTableTMac() throws IOException {
680 // match for router-mac and ip-packets
681 OFOxmEthType oxe = factory.oxms().ethType(EthType.IPv4);
682 OFOxmEthDst dmac = factory.oxms().ethDst(getRouterMacAddr());
683 OFOxmList oxmListIp = OFOxmList.of(dmac, oxe);
684 OFMatchV3 matchIp = factory.buildMatchV3()
685 .setOxmList(oxmListIp).build();
686 OFInstruction gotoTblIp = factory.instructions().buildGotoTable()
687 .setTableId(TableId.of(TABLE_IPv4_UNICAST)).build();
688 List<OFInstruction> instructionsIp = Collections.singletonList(gotoTblIp);
689 OFMessage ipEntry = factory.buildFlowAdd()
690 .setTableId(TableId.of(TABLE_TMAC))
691 .setMatch(matchIp)
692 .setInstructions(instructionsIp)
693 .setPriority(1000) // strict priority required lower than
694 // multicastMac
695 .setBufferId(OFBufferId.NO_BUFFER)
696 .setIdleTimeout(0)
697 .setHardTimeout(0)
698 .setXid(getNextTransactionId())
699 .build();
700
701 // match for router-mac and mpls packets
702 OFOxmEthType oxmpls = factory.oxms().ethType(EthType.MPLS_UNICAST);
703 OFOxmList oxmListMpls = OFOxmList.of(dmac, oxmpls);
704 OFMatchV3 matchMpls = factory.buildMatchV3()
705 .setOxmList(oxmListMpls).build();
706 OFInstruction gotoTblMpls = factory.instructions().buildGotoTable()
707 .setTableId(TableId.of(TABLE_MPLS)).build();
708 List<OFInstruction> instructionsMpls = Collections.singletonList(gotoTblMpls);
709 OFMessage mplsEntry = factory.buildFlowAdd()
710 .setTableId(TableId.of(TABLE_TMAC))
711 .setMatch(matchMpls)
712 .setInstructions(instructionsMpls)
713 .setPriority(1001) // strict priority required lower than
714 // multicastMac
715 .setBufferId(OFBufferId.NO_BUFFER)
716 .setIdleTimeout(0)
717 .setHardTimeout(0)
718 .setXid(getNextTransactionId())
719 .build();
720
721 log.debug("Adding termination-mac-rules in sw {}", getStringId());
722 List<OFMessage> msglist = new ArrayList<OFMessage>(2);
723 msglist.add(ipEntry);
724 msglist.add(mplsEntry);
725 write(msglist);
726 }
727
728 private MacAddress getRouterMacAddr() {
729 if (srConfig != null) {
730 return MacAddress.of(srConfig.getRouterMac());
731 } else {
732 // return a dummy mac address - it will not be used
733 return MacAddress.of("00:00:00:00:00:00");
734 }
735 }
736
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700737 private boolean isEdgeRouter(Dpid ndpid) {
738 INetworkConfigService ncs = floodlightProvider.getNetworkConfigService();
739 SwitchConfigStatus scs = ncs.checkSwitchConfig(ndpid);
740 if (scs.getConfigState() == NetworkConfigState.ACCEPT_ADD) {
741 return ((SegmentRouterConfig) scs.getSwitchConfig()).isEdgeRouter();
742 } else {
743 // TODO: return false if router not allowed
744 return false;
745 }
746 }
747
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700748 private MacAddress getNeighborRouterMacAddress(Dpid ndpid) {
749 INetworkConfigService ncs = floodlightProvider.getNetworkConfigService();
750 SwitchConfigStatus scs = ncs.checkSwitchConfig(ndpid);
751 if (scs.getConfigState() == NetworkConfigState.ACCEPT_ADD) {
752 return MacAddress.of(((SegmentRouterConfig) scs.getSwitchConfig())
753 .getRouterMac());
754 } else {
755 // return a dummy mac address - it will not be used
756 return MacAddress.of("00:00:00:00:00:00");
757 }
758 }
759
760 private void setNeighbors(List<LinkConfig> linkConfigList) {
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700761 for (LinkConfig lg : linkConfigList) {
762 if (!lg.getType().equals(NetworkConfigManager.PKT_LINK)) {
Saurav Das80d17392014-10-01 10:24:56 -0700763 continue;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700764 }
765 PktLinkConfig plg = (PktLinkConfig) lg;
766 if (plg.getDpid1() == getId()) {
767 addNeighborAtPort(new Dpid(plg.getDpid2()),
768 PortNumber.uint32(plg.getPort1()));
769 } else if (plg.getDpid2() == getId()) {
770 addNeighborAtPort(new Dpid(plg.getDpid1()),
771 PortNumber.uint32(plg.getPort2()));
772 }
773 }
774 }
775
776 private void addNeighborAtPort(Dpid neighborDpid, PortNumber portToNeighbor) {
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700777 /* Update NeighborToPort database */
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700778 if (neighbors.get(neighborDpid) != null) {
779 neighbors.get(neighborDpid).add(portToNeighbor);
780 } else {
781 Set<PortNumber> ports = new HashSet<PortNumber>();
782 ports.add(portToNeighbor);
783 neighbors.put(neighborDpid, ports);
784 }
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700785
786 /* Update portToNeighbors database */
787 if (portToNeighbors.get(portToNeighbor) == null)
788 portToNeighbors.put(portToNeighbor, neighborDpid);
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700789 }
790
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700791 private void getAllNodeSegmentIds(List<SwitchConfig> switchList) {
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700792 for (SwitchConfig sc : switchList) {
793 /* TODO: Do we need to check if the SwitchConfig is of
794 * type SegmentRouter?
795 */
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700796 if (sc.getDpid() == getId()) {
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700797 continue;
798 }
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700799 segmentIds.add(((SegmentRouterConfig) sc).getNodeSid());
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700800 }
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700801 log.debug("getAllNodeSegmentIds: at sw {} are {}",
802 getStringId(), segmentIds);
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700803 }
804
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700805 private boolean isSegmentIdSameAsNodeSegmentId(Dpid dpid, int sId) {
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700806 INetworkConfigService ncs = floodlightProvider.getNetworkConfigService();
807 SwitchConfigStatus scs = ncs.checkSwitchConfig(dpid);
808 if (scs.getConfigState() == NetworkConfigState.ACCEPT_ADD) {
809 return (((SegmentRouterConfig) scs.getSwitchConfig()).
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700810 getNodeSid() == sId);
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700811 } else {
812 // TODO: return false if router not allowed
813 return false;
814 }
815 }
816
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700817 private Set<Set<Dpid>> getAllNeighborSets(Set<Dpid> neighbors) {
818 List<Dpid> list = new ArrayList<Dpid>(neighbors);
819 Set<Set<Dpid>> sets = new HashSet<Set<Dpid>>();
820 /* get the number of elements in the neighbors */
821 int elements = list.size();
822 /* the number of members of a power set is 2^n
823 * including the empty set
824 */
825 int powerElements = (1 << elements);
826
827 /* run a binary counter for the number of power elements */
828 for (long i = 1; i < powerElements; i++) {
829 Set<Dpid> dpidSubSet = new HashSet<Dpid>();
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700830 for (int j = 0; j < elements; j++) {
831 if ((i >> j) % 2 == 1) {
832 dpidSubSet.add(list.get(j));
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700833 }
834 }
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700835 /* NOTE: Avoid any pairings of edge routers only
836 * at a backbone router */
837 boolean avoidEdgeRouterPairing = true;
838 if ((!isEdgeRouter) && (dpidSubSet.size() > 1)) {
Srikanth Vavilapalli696010f2014-10-08 17:00:17 -0700839 for (Dpid dpid : dpidSubSet) {
840 if (!isEdgeRouter(dpid)) {
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700841 avoidEdgeRouterPairing = false;
Srikanth Vavilapalli696010f2014-10-08 17:00:17 -0700842 break;
843 }
844 }
845 }
846 else
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700847 avoidEdgeRouterPairing = false;
Srikanth Vavilapalli696010f2014-10-08 17:00:17 -0700848
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700849 if (!avoidEdgeRouterPairing)
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700850 sets.add(dpidSubSet);
851 }
852 return sets;
853 }
854
Srikanth Vavilapalli25fbe392014-10-10 15:04:51 -0700855 private void createGroupForANeighborSet(NeighborSet ns, int groupId) {
856 List<BucketInfo> buckets = new ArrayList<BucketInfo>();
857 for (Dpid d : ns.getDpids()) {
858 for (PortNumber sp : neighbors.get(d)) {
859 BucketInfo b = new BucketInfo(d,
860 MacAddress.of(srConfig.getRouterMac()),
861 getNeighborRouterMacAddress(d), sp,
Srikanth Vavilapalli16a04722014-10-23 11:09:24 -0700862 ns.getEdgeLabel(), true, -1);
Srikanth Vavilapalli25fbe392014-10-10 15:04:51 -0700863 buckets.add(b);
864
865 /* Update Port Neighborset map */
866 ArrayList<NeighborSet> portNeighborSets =
867 portNeighborSetMap.get(sp);
868 if (portNeighborSets == null) {
869 portNeighborSets = new ArrayList<NeighborSet>();
870 portNeighborSets.add(ns);
871 portNeighborSetMap.put(sp, portNeighborSets);
872 }
873 else
874 portNeighborSets.add(ns);
875 }
876 }
877 EcmpInfo ecmpInfo = new EcmpInfo(groupId, buckets);
878 setEcmpGroup(ecmpInfo);
879 ecmpGroups.put(ns, ecmpInfo);
880 log.debug(
881 "createGroupForANeighborSet: Creating ecmp group {} in sw {} "
882 + "for neighbor set {} with: {}",
883 groupId, getStringId(), ns, ecmpInfo);
884 return;
885 }
886
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700887 /**
888 * createGroups creates ECMP groups for all ports on this router connected
889 * to other routers (in the OF network). The information for ports is
890 * gleaned from the configured links. If no links are configured no groups
891 * will be created, and it is up to the caller of the IOF13Switch API to
892 * create groups.
893 * <p>
894 * By default all ports connected to the same neighbor router will be part
895 * of the same ECMP group. In addition, groups will be created for all
896 * possible combinations of neighbor routers.
897 * <p>
898 * For example, consider this router (R0) connected to 3 neighbors (R1, R2,
899 * and R3). The following groups will be created in R0:
900 * <li>1) all ports to R1,
901 * <li>2) all ports to R2,
902 * <li>3) all ports to R3,
903 * <li>4) all ports to R1 and R2
904 * <li>5) all ports to R1 and R3
905 * <li>6) all ports to R2 and R3
906 * <li>7) all ports to R1, R2, and R3
907 */
908 private void createGroups() {
Sangho Shin15273b62014-10-16 22:22:05 -0700909
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700910 Set<Dpid> dpids = neighbors.keySet();
911 if (dpids == null || dpids.isEmpty()) {
912 return;
913 }
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700914 /* Create all possible Neighbor sets from this router
915 * NOTE: Avoid any pairings of edge routers only
916 */
917 Set<Set<Dpid>> powerSet = getAllNeighborSets(dpids);
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700918 log.debug("createGroups: The size of neighbor powerset for sw {} is {}",
Srikanth Vavilapallica2263c2014-10-09 15:19:11 -0700919 getStringId(), powerSet.size());
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700920 Set<NeighborSet> nsSet = new HashSet<NeighborSet>();
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700921 for (Set<Dpid> combo : powerSet) {
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700922 if (combo.isEmpty())
923 continue;
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700924 if (isEdgeRouter && !segmentIds.isEmpty()) {
925 for (Integer sId : segmentIds) {
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700926 NeighborSet ns = new NeighborSet();
927 ns.addDpids(combo);
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700928 /* Check if the edge label being set is of the
929 * same node in the Neighbor set
930 */
931 if ((combo.size() != 1) ||
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700932 (!isSegmentIdSameAsNodeSegmentId(
933 combo.iterator().next(), sId))) {
934 ns.setEdgeLabel(sId);
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -0700935 }
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700936 nsSet.add(ns);
937 }
938 } else {
939 NeighborSet ns = new NeighborSet();
940 ns.addDpids(combo);
941 nsSet.add(ns);
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700942 }
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700943 }
Srikanth Vavilapalli2cd0ff12014-10-10 13:07:46 -0700944 log.debug("createGroups: The neighborset with label for sw {} is {}",
Srikanth Vavilapallica2263c2014-10-09 15:19:11 -0700945 getStringId(), nsSet);
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700946
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700947 for (NeighborSet ns : nsSet) {
Srikanth Vavilapalli25fbe392014-10-10 15:04:51 -0700948 createGroupForANeighborSet(ns, groupid.incrementAndGet());
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700949 }
950 }
951
952 private class EcmpInfo {
953 int groupId;
954 List<BucketInfo> buckets;
955
956 EcmpInfo(int gid, List<BucketInfo> bucketInfos) {
957 groupId = gid;
958 buckets = bucketInfos;
959 }
960
961 @Override
962 public String toString() {
963 return "groupId: " + groupId + ", buckets: " + buckets;
964 }
965 }
966
967 private class BucketInfo {
968 Dpid neighborDpid;
969 MacAddress srcMac;
970 MacAddress dstMac;
971 PortNumber outport;
Sangho Shin15273b62014-10-16 22:22:05 -0700972 int groupNo;
Srikanth Vavilapalli68144302014-10-08 15:55:24 -0700973 int mplsLabel;
Sangho Shin15273b62014-10-16 22:22:05 -0700974 boolean bos;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700975
Srikanth Vavilapalli16a04722014-10-23 11:09:24 -0700976 /*
977 BucketInfo(Dpid nDpid, MacAddress smac, MacAddress dmac,
978 PortNumber p, int label) {
979 neighborDpid = nDpid;
980 srcMac = smac;
981 dstMac = dmac;
982 outport = p;
983 mplsLabel = label;
984 groupNo = -1;
985 }
Saurav Dasfc5e3eb2014-09-25 19:05:21 -0700986
Srikanth Vavilapalli16a04722014-10-23 11:09:24 -0700987 BucketInfo(int no, int label, boolean b) {
988 neighborDpid = null;
989 srcMac = null;
990 dstMac = null;
991 outport = null;
992 groupNo = no;
993 mplsLabel = label;
994 bos = b;
995 }
996 */
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -0700997 BucketInfo(Dpid nDpid, MacAddress smac, MacAddress dmac,
998 PortNumber p, int label, boolean bos, int gotoGroupNo) {
999 neighborDpid = nDpid;
1000 srcMac = smac;
1001 dstMac = dmac;
1002 outport = p;
1003 mplsLabel = label;
1004 this.bos = bos;
1005 groupNo = gotoGroupNo;
1006 }
1007
Sangho Shin15273b62014-10-16 22:22:05 -07001008
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001009 @Override
1010 public String toString() {
1011 return " {neighborDpid: " + neighborDpid + ", dstMac: " + dstMac +
Srikanth Vavilapalli68144302014-10-08 15:55:24 -07001012 ", srcMac: " + srcMac + ", outport: " + outport +
Sangho Shin15273b62014-10-16 22:22:05 -07001013 ", groupNo: " + groupNo +
1014 ", mplsLabel: " + mplsLabel + "}";
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001015 }
1016 }
1017
1018 private void setEcmpGroup(EcmpInfo ecmpInfo) {
1019 List<OFMessage> msglist = new ArrayList<OFMessage>();
1020 OFGroup group = OFGroup.of(ecmpInfo.groupId);
1021
1022 List<OFBucket> buckets = new ArrayList<OFBucket>();
1023 for (BucketInfo b : ecmpInfo.buckets) {
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001024 List<OFAction> actions = new ArrayList<OFAction>();
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001025 if (b.dstMac != null) {
Sangho Shin15273b62014-10-16 22:22:05 -07001026 OFOxmEthDst dmac = factory.oxms()
1027 .ethDst(b.dstMac);
1028 OFAction setDA = factory.actions().buildSetField()
1029 .setField(dmac).build();
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001030 actions.add(setDA);
1031 }
1032 if (b.srcMac != null) {
Sangho Shin15273b62014-10-16 22:22:05 -07001033 OFOxmEthSrc smac = factory.oxms()
1034 .ethSrc(b.srcMac);
1035 OFAction setSA = factory.actions().buildSetField()
1036 .setField(smac).build();
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001037 actions.add(setSA);
1038 }
1039 if (b.outport != null) {
Sangho Shin15273b62014-10-16 22:22:05 -07001040 OFAction outp = factory.actions().buildOutput()
1041 .setPort(OFPort.of(b.outport.shortValue()))
1042 .build();
Sangho Shin15273b62014-10-16 22:22:05 -07001043 actions.add(outp);
1044 }
Sangho Shin15273b62014-10-16 22:22:05 -07001045 if (b.mplsLabel != -1) {
1046 OFAction pushLabel = factory.actions().buildPushMpls()
1047 .setEthertype(EthType.MPLS_UNICAST).build();
Sangho Shin15273b62014-10-16 22:22:05 -07001048 OFBooleanValue bosValue = null;
1049 if (b.bos)
1050 bosValue = OFBooleanValue.TRUE;
1051 else
1052 bosValue = OFBooleanValue.FALSE;
1053 OFOxmMplsBos bosX = factory.oxms()
1054 .mplsBos(bosValue);
1055 OFAction setBX = factory.actions().buildSetField()
1056 .setField(bosX).build();
1057 OFOxmMplsLabel lid = factory.oxms()
1058 .mplsLabel(U32.of(b.mplsLabel));
1059 OFAction setLabel = factory.actions().buildSetField()
1060 .setField(lid).build();
1061 OFAction copyTtl = factory.actions().copyTtlOut();
1062 OFAction decrTtl = factory.actions().decMplsTtl();
1063 actions.add(pushLabel);
1064 actions.add(setLabel);
1065 actions.add(setBX);
1066 actions.add(copyTtl);
1067 // decrement TTL only when the first MPLS label is pushed
1068 if (b.bos)
1069 actions.add(decrTtl);
1070 }
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001071 if (b.groupNo > 0) {
1072 OFAction groupTo = factory.actions().buildGroup()
1073 .setGroup(OFGroup.of(b.groupNo))
1074 .build();
1075 actions.add(groupTo);
1076 }
Sangho Shin15273b62014-10-16 22:22:05 -07001077 OFBucket ofb = factory.buildBucket()
1078 .setWeight(1)
1079 .setActions(actions)
1080 .build();
1081 buckets.add(ofb);
1082 }
1083
1084 OFMessage gm = factory.buildGroupAdd()
1085 .setGroup(group)
1086 .setBuckets(buckets)
1087 .setGroupType(OFGroupType.SELECT)
1088 .setXid(getNextTransactionId())
1089 .build();
1090 msglist.add(gm);
1091 try {
1092 write(msglist);
1093 } catch (IOException e) {
1094 // TODO Auto-generated catch block
1095 e.printStackTrace();
1096 }
1097 }
1098
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001099 /*
1100 private void setPolicyEcmpGroup(EcmpInfo ecmpInfo) {
1101 List<OFMessage> msglist = new ArrayList<OFMessage>();
1102 OFGroup group = OFGroup.of(ecmpInfo.groupId);
1103
1104 List<OFBucket> buckets = new ArrayList<OFBucket>();
1105 List<OFAction> actions = new ArrayList<OFAction>();
1106 for (BucketInfo b : ecmpInfo.buckets) {
1107 if (b.dstMac != null && b.srcMac != null && b.outport != null) {
1108 OFOxmEthDst dmac = factory.oxms()
1109 .ethDst(b.dstMac);
1110 OFAction setDA = factory.actions().buildSetField()
1111 .setField(dmac).build();
1112 OFOxmEthSrc smac = factory.oxms()
1113 .ethSrc(b.srcMac);
1114 OFAction setSA = factory.actions().buildSetField()
1115 .setField(smac).build();
1116 OFAction outp = factory.actions().buildOutput()
1117 .setPort(OFPort.of(b.outport.shortValue()))
1118 .build();
1119 actions.add(setSA);
1120 actions.add(setDA);
1121 actions.add(outp);
1122 }
1123 if (b.groupNo > 0) {
1124 OFAction groupTo = factory.actions().buildGroup()
1125 .setGroup(OFGroup.of(b.groupNo))
1126 .build();
1127 actions.add(groupTo);
1128 }
1129 if (b.mplsLabel != -1) {
1130 OFAction pushLabel = factory.actions().buildPushMpls()
1131 .setEthertype(EthType.MPLS_UNICAST).build();
1132
1133 OFBooleanValue bosValue = null;
1134 if (b.bos)
1135 bosValue = OFBooleanValue.TRUE;
1136 else
1137 bosValue = OFBooleanValue.FALSE;
1138 OFOxmMplsBos bosX = factory.oxms()
1139 .mplsBos(bosValue);
1140 OFAction setBX = factory.actions().buildSetField()
1141 .setField(bosX).build();
1142 OFOxmMplsLabel lid = factory.oxms()
1143 .mplsLabel(U32.of(b.mplsLabel));
1144 OFAction setLabel = factory.actions().buildSetField()
1145 .setField(lid).build();
1146 OFAction copyTtl = factory.actions().copyTtlOut();
1147 OFAction decrTtl = factory.actions().decMplsTtl();
1148 actions.add(pushLabel);
1149 actions.add(setLabel);
1150 actions.add(setBX);
1151 actions.add(copyTtl);
1152 // decrement TTL only when the first MPLS label is pushed
1153 if (b.bos)
1154 actions.add(decrTtl);
1155 }
1156 OFBucket ofb = factory.buildBucket()
1157 .setWeight(1)
1158 .setActions(actions)
1159 .build();
1160 buckets.add(ofb);
1161 }
1162
1163 OFMessage gm = factory.buildGroupAdd()
1164 .setGroup(group)
1165 .setBuckets(buckets)
1166 .setGroupType(OFGroupType.SELECT)
1167 .setXid(getNextTransactionId())
1168 .build();
1169 msglist.add(gm);
1170 try {
1171 write(msglist);
1172 } catch (IOException e) {
1173 // TODO Auto-generated catch block
1174 e.printStackTrace();
1175 }
1176 }
1177 */
Sangho Shin55d00e12014-10-20 12:13:07 -07001178 private void deleteGroup(int groupId) {
1179
1180 List<OFMessage> msglist = new ArrayList<OFMessage>();
1181 OFGroup group = OFGroup.of(groupId);
1182
1183 OFMessage gm = factory.buildGroupDelete()
1184 .setGroup(group)
1185 .setGroupType(OFGroupType.SELECT)
1186 .setXid(getNextTransactionId())
1187 .build();
1188 msglist.add(gm);
1189 try {
1190 write(msglist);
1191 } catch (IOException e) {
1192 // TODO Auto-generated catch block
1193 e.printStackTrace();
1194 }
1195 }
1196
Srikanth Vavilapalli7d873fd2014-10-04 07:50:57 -07001197 private void modifyEcmpGroup(EcmpInfo ecmpInfo) {
1198 List<OFMessage> msglist = new ArrayList<OFMessage>();
1199 OFGroup group = OFGroup.of(ecmpInfo.groupId);
1200
1201 List<OFBucket> buckets = new ArrayList<OFBucket>();
1202 for (BucketInfo b : ecmpInfo.buckets) {
1203 OFOxmEthDst dmac = factory.oxms()
1204 .ethDst(b.dstMac);
1205 OFAction setDA = factory.actions().buildSetField()
1206 .setField(dmac).build();
1207 OFOxmEthSrc smac = factory.oxms()
1208 .ethSrc(b.srcMac);
1209 OFAction setSA = factory.actions().buildSetField()
1210 .setField(smac).build();
1211 OFAction outp = factory.actions().buildOutput()
1212 .setPort(OFPort.of(b.outport.shortValue()))
1213 .build();
1214 List<OFAction> actions = new ArrayList<OFAction>();
1215 actions.add(setSA);
1216 actions.add(setDA);
1217 actions.add(outp);
Srikanth Vavilapalli68144302014-10-08 15:55:24 -07001218 if (b.mplsLabel != -1) {
1219 OFAction pushLabel = factory.actions().buildPushMpls()
1220 .setEthertype(EthType.MPLS_UNICAST).build();
Srikanth Vavilapalli11dc6072014-10-13 13:32:11 -07001221 OFOxmMplsBos bosX = factory.oxms()
1222 .mplsBos(OFBooleanValue.TRUE);
1223 OFAction setBX = factory.actions().buildSetField()
1224 .setField(bosX).build();
Srikanth Vavilapalli4ae146e2014-10-09 14:35:26 -07001225 OFOxmMplsLabel lid = factory.oxms()
1226 .mplsLabel(U32.of(b.mplsLabel));
1227 OFAction setLabel = factory.actions().buildSetField()
1228 .setField(lid).build();
Srikanth Vavilapalli68144302014-10-08 15:55:24 -07001229 OFAction copyTtl = factory.actions().copyTtlOut();
1230 OFAction decrTtl = factory.actions().decMplsTtl();
1231 actions.add(pushLabel);
1232 actions.add(setLabel);
Srikanth Vavilapalli11dc6072014-10-13 13:32:11 -07001233 actions.add(setBX);
Srikanth Vavilapalli68144302014-10-08 15:55:24 -07001234 actions.add(copyTtl);
1235 actions.add(decrTtl);
1236 }
Srikanth Vavilapalli7d873fd2014-10-04 07:50:57 -07001237 OFBucket ofb = factory.buildBucket()
1238 .setWeight(1)
1239 .setActions(actions)
1240 .build();
1241 buckets.add(ofb);
1242 }
1243
1244 OFMessage gm = factory.buildGroupModify()
1245 .setGroup(group)
1246 .setBuckets(buckets)
1247 .setGroupType(OFGroupType.SELECT)
1248 .setXid(getNextTransactionId())
1249 .build();
1250 msglist.add(gm);
1251 try {
1252 write(msglist);
1253 } catch (IOException e) {
1254 // TODO Auto-generated catch block
1255 e.printStackTrace();
1256 }
1257 }
1258
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001259 private void verifyGroups() throws IOException {
1260 sendGroupDescRequest();
1261 }
1262
1263 private void sendGroupDescRequest() throws IOException {
1264 OFMessage gdr = factory.buildGroupDescStatsRequest()
1265 .setXid(getNextTransactionId())
1266 .build();
1267 write(gdr, null);
1268 }
1269
1270 private void assignAdjacencyLabels() {
Saurav Das2d41a432014-10-21 20:40:10 -07001271 List<AdjacencySid> autogenAdjSids = new ArrayList<AdjacencySid>();
1272 publishAttributes = new HashMap<String, String>();
Saurav Das05ed3222014-10-27 11:30:39 -07001273 if (srConfig == null) {
1274 log.error("Cannot obtain SegmentRouterConfig in sw {}", getStringId());
1275 return; // this will cause handshake to fail
1276 }
Saurav Das2d41a432014-10-21 20:40:10 -07001277 for (OFPortDesc p : getPorts()) {
1278 int pnum = p.getPortNo().getPortNumber();
1279
1280 if (U32.ofRaw(pnum).compareTo(U32.ofRaw(OFPort.MAX.getPortNumber())) >= 1) {
1281 continue;
1282 }
1283 // create unique adj-sid assuming that operator only
1284 // enters adjSids for multiple-ports and only in the range
1285 // 1-10k XXX make sure that happens
1286 int adjSid = srConfig.getNodeSid() * 1000 + pnum;
1287 AdjacencySid as = new AdjacencySid(adjSid,
1288 Collections.singletonList(pnum));
1289 autogenAdjSids.add(as);
1290 }
1291 ObjectMapper mapper = new ObjectMapper();
1292 try {
1293 publishAttributes.put("autogenAdjSids",
1294 mapper.writeValueAsString(autogenAdjSids));
1295 } catch (IOException e1) {
1296 log.error("Error while writing adjacency labels: {}", e1.getCause());
1297 }
1298
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001299 try {
1300 nextDriverState();
1301 } catch (IOException e) {
1302 // TODO Auto-generated catch block
1303 e.printStackTrace();
1304 }
1305 }
1306
Saurav Das1cd10152014-09-26 09:38:07 -07001307 private OFAction getOFAction(Action action) {
1308 OFAction ofAction = null;
1309 if (action instanceof OutputAction) {
1310 OutputAction outputAction = (OutputAction) action;
1311 OFPort port = OFPort.of((int) outputAction.getPortNumber().value());
1312 ofAction = factory.actions().output(port, Short.MAX_VALUE);
1313 } else if (action instanceof ModifyDstMacAction) {
1314 long dstMac = ((ModifyDstMacAction) action).getDstMac().toLong();
1315 OFOxmEthDst dmac = factory.oxms()
1316 .ethDst(MacAddress.of(dstMac));
1317 ofAction = factory.actions().buildSetField()
1318 .setField(dmac).build();
1319 } else if (action instanceof ModifySrcMacAction) {
1320 long srcMac = ((ModifySrcMacAction) action).getSrcMac().toLong();
1321 OFOxmEthSrc smac = factory.oxms()
1322 .ethSrc(MacAddress.of(srcMac));
1323 ofAction = factory.actions().buildSetField()
1324 .setField(smac).build();
Saurav Dasa962a692014-10-17 14:52:38 -07001325 } else if (action instanceof PushMplsAction) {
1326 ofAction = factory.actions().pushMpls(EthType.MPLS_UNICAST);
1327 } else if (action instanceof SetMplsIdAction) {
1328 int labelid = ((SetMplsIdAction) action).getMplsId();
1329 OFOxmMplsLabel lid = factory.oxms()
1330 .mplsLabel(U32.of(labelid));
1331 ofAction = factory.actions().buildSetField()
1332 .setField(lid).build();
1333 } else if (action instanceof SetMplsBosAction) {
1334 OFBooleanValue val = OFBooleanValue.of(
1335 ((SetMplsBosAction) action).isSet());
1336 OFOxmMplsBos bos = factory.oxms().mplsBos(val);
1337 OFAction setBos = factory.actions().buildSetField()
1338 .setField(bos).build();
1339 } else if (action instanceof PopMplsAction) {
Saurav Das1cd10152014-09-26 09:38:07 -07001340 EthType ethertype = ((PopMplsAction) action).getEthType();
1341 ofAction = factory.actions().popMpls(ethertype);
1342 } else if (action instanceof GroupAction) {
Sangho Shin15273b62014-10-16 22:22:05 -07001343 int gid = -1;
1344 GroupAction ga = (GroupAction)action;
Srikanth Vavilapalli16a04722014-10-23 11:09:24 -07001345 if (ga.getGroupId() > 0) {
1346 gid = ga.getGroupId();
Saurav Das1cd10152014-09-26 09:38:07 -07001347 }
Sangho Shin15273b62014-10-16 22:22:05 -07001348 else {
1349 NeighborSet ns = ((GroupAction) action).getDpids();
1350 EcmpInfo ei = ecmpGroups.get(ns);
1351 if (ei == null) {
1352 log.debug("Unable to find ecmp group for neighbors {} at "
1353 + "switch {} and hence creating it", ns, getStringId());
1354 createGroupForANeighborSet(ns, groupid.incrementAndGet());
1355 ei = ecmpGroups.get(ns);
1356 }
1357 gid = ei.groupId;
1358 }
Srikanth Vavilapalli846df292014-10-10 15:47:16 -07001359 ofAction = factory.actions().buildGroup()
1360 .setGroup(OFGroup.of(gid))
1361 .build();
Saurav Das1cd10152014-09-26 09:38:07 -07001362 } else if (action instanceof DecNwTtlAction) {
1363 ofAction = factory.actions().decNwTtl();
1364 } else if (action instanceof DecMplsTtlAction) {
1365 ofAction = factory.actions().decMplsTtl();
1366 } else if (action instanceof CopyTtlInAction) {
1367 ofAction = factory.actions().copyTtlIn();
1368 } else if (action instanceof CopyTtlOutAction) {
1369 ofAction = factory.actions().copyTtlOut();
Sangho Shine842cad2014-10-24 16:07:35 -07001370 } else if (action instanceof SetDAAction) {
1371 OFOxmEthDst dmac = factory.oxms()
1372 .ethDst(((SetDAAction)action).getAddress());
1373 ofAction = factory.actions().buildSetField()
1374 .setField(dmac).build();
1375 } else if (action instanceof SetSAAction) {
1376 OFOxmEthSrc smac = factory.oxms()
1377 .ethSrc(((SetSAAction)action).getAddress());
1378 ofAction = factory.actions().buildSetField()
1379 .setField(smac).build();
Saurav Das1cd10152014-09-26 09:38:07 -07001380 } else {
1381 log.warn("Unsupported Action type: {}", action.getClass().getName());
1382 return null;
1383 }
1384
Saurav Das1cd10152014-09-26 09:38:07 -07001385 return ofAction;
1386 }
1387
Saurav Das0a344b02014-09-26 14:18:52 -07001388 private OFMessage getIpEntry(MatchActionOperationEntry mao) {
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001389 MatchAction ma = mao.getTarget();
1390 Operator op = mao.getOperator();
1391 Ipv4Match ipm = (Ipv4Match) ma.getMatch();
1392
1393 // set match
1394 IPv4Net ipdst = ipm.getDestination();
1395 OFOxmEthType ethTypeIp = factory.oxms()
1396 .ethType(EthType.IPv4);
1397 OFOxmIpv4DstMasked ipPrefix = factory.oxms()
1398 .ipv4DstMasked(
1399 IPv4Address.of(ipdst.address().value()),
1400 IPv4Address.ofCidrMaskLength(ipdst.prefixLen())
1401 );
1402 OFOxmList oxmList = OFOxmList.of(ethTypeIp, ipPrefix);
1403 OFMatchV3 match = factory.buildMatchV3()
1404 .setOxmList(oxmList).build();
1405
1406 // set actions
1407 List<OFAction> writeActions = new ArrayList<OFAction>();
1408 for (Action action : ma.getActions()) {
Saurav Das1cd10152014-09-26 09:38:07 -07001409 OFAction ofAction = getOFAction(action);
1410 if (ofAction != null) {
1411 writeActions.add(ofAction);
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001412 }
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001413 }
1414
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001415 // set instructions
1416 OFInstruction writeInstr = factory.instructions().buildWriteActions()
1417 .setActions(writeActions).build();
1418 OFInstruction gotoInstr = factory.instructions().buildGotoTable()
1419 .setTableId(TableId.of(TABLE_ACL)).build();
1420 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
1421 instructions.add(writeInstr);
1422 instructions.add(gotoInstr);
1423
Saurav Das1cd10152014-09-26 09:38:07 -07001424 // set flow priority to emulate longest prefix match
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001425 int priority = ipdst.prefixLen() * PRIORITY_MULTIPLIER;
1426 if (ipdst.prefixLen() == (short) 32) {
1427 priority = MAX_PRIORITY;
1428 }
1429
Saurav Dasbc594a42014-09-25 20:13:50 -07001430 // set flow-mod
Saurav Dase972b3a2014-09-26 15:41:06 -07001431 OFFlowMod.Builder fmBuilder = null;
1432 switch (op) {
1433 case ADD:
1434 fmBuilder = factory.buildFlowAdd();
1435 break;
1436 case REMOVE:
1437 fmBuilder = factory.buildFlowDeleteStrict();
1438 break;
Sangho Shin5be3e532014-10-03 17:20:58 -07001439 case MODIFY: // TODO
1440 fmBuilder = factory.buildFlowModifyStrict();
1441 break;
Saurav Dase972b3a2014-09-26 15:41:06 -07001442 default:
1443 log.warn("Unsupported MatchAction Operator: {}", op);
1444 return null;
Saurav Dasbc594a42014-09-25 20:13:50 -07001445 }
Saurav Dase972b3a2014-09-26 15:41:06 -07001446 OFMessage ipFlow = fmBuilder
1447 .setTableId(TableId.of(TABLE_IPv4_UNICAST))
1448 .setMatch(match)
1449 .setInstructions(instructions)
1450 .setPriority(priority)
1451 .setBufferId(OFBufferId.NO_BUFFER)
1452 .setIdleTimeout(0)
1453 .setHardTimeout(0)
1454 .setXid(getNextTransactionId())
1455 .build();
Saurav Dasbc594a42014-09-25 20:13:50 -07001456 log.debug("{} ip-rule {}-{} in sw {}",
1457 (op == MatchActionOperations.Operator.ADD) ? "Adding" : "Deleting",
1458 match, writeActions,
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001459 getStringId());
Saurav Das0a344b02014-09-26 14:18:52 -07001460 return ipFlow;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001461 }
1462
Saurav Das0a344b02014-09-26 14:18:52 -07001463 private OFMessage getMplsEntry(MatchActionOperationEntry mao) {
Saurav Das1cd10152014-09-26 09:38:07 -07001464 MatchAction ma = mao.getTarget();
1465 Operator op = mao.getOperator();
1466 MplsMatch mplsm = (MplsMatch) ma.getMatch();
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001467
Saurav Das1cd10152014-09-26 09:38:07 -07001468 // set match
1469 OFOxmEthType ethTypeMpls = factory.oxms()
1470 .ethType(EthType.MPLS_UNICAST);
1471 OFOxmMplsLabel labelid = factory.oxms()
1472 .mplsLabel(U32.of(mplsm.getMplsLabel()));
Sangho Shinc1b8dea2014-10-13 12:03:28 -07001473 OFOxmMplsBos bos = factory.oxms()
1474 .mplsBos(OFBooleanValue.of(mplsm.isBos()));
1475 OFOxmList oxmList = OFOxmList.of(ethTypeMpls, labelid, bos);
Saurav Das1cd10152014-09-26 09:38:07 -07001476 OFMatchV3 matchlabel = factory.buildMatchV3()
1477 .setOxmList(oxmList).build();
1478
1479 // set actions
1480 List<OFAction> writeActions = new ArrayList<OFAction>();
1481 for (Action action : ma.getActions()) {
1482 OFAction ofAction = getOFAction(action);
1483 if (ofAction != null) {
1484 writeActions.add(ofAction);
1485 }
1486 }
1487
1488 // set instructions
1489 OFInstruction writeInstr = factory.instructions().buildWriteActions()
1490 .setActions(writeActions).build();
1491 OFInstruction gotoInstr = factory.instructions().buildGotoTable()
1492 .setTableId(TableId.of(TABLE_ACL)).build();
1493 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
1494 instructions.add(writeInstr);
1495 instructions.add(gotoInstr);
1496
Saurav Dase972b3a2014-09-26 15:41:06 -07001497 // set flow-mod
1498 OFFlowMod.Builder fmBuilder = null;
1499 switch (op) {
1500 case ADD:
1501 fmBuilder = factory.buildFlowAdd();
1502 break;
1503 case REMOVE:
1504 fmBuilder = factory.buildFlowDeleteStrict();
1505 break;
Sangho Shin5be3e532014-10-03 17:20:58 -07001506 case MODIFY: // TODO
1507 fmBuilder = factory.buildFlowModifyStrict();
1508 break;
Saurav Dase972b3a2014-09-26 15:41:06 -07001509 default:
1510 log.warn("Unsupported MatchAction Operator: {}", op);
1511 return null;
Saurav Das1cd10152014-09-26 09:38:07 -07001512 }
Saurav Dase972b3a2014-09-26 15:41:06 -07001513
1514 OFMessage mplsFlow = fmBuilder
1515 .setTableId(TableId.of(TABLE_MPLS))
1516 .setMatch(matchlabel)
1517 .setInstructions(instructions)
1518 .setPriority(MAX_PRIORITY) // exact match and exclusive
1519 .setBufferId(OFBufferId.NO_BUFFER)
1520 .setIdleTimeout(0)
1521 .setHardTimeout(0)
1522 .setXid(getNextTransactionId())
1523 .build();
Saurav Das1cd10152014-09-26 09:38:07 -07001524 log.debug("{} mpls-rule {}-{} in sw {}",
1525 (op == MatchActionOperations.Operator.ADD) ? "Adding" : "Deleting",
1526 matchlabel, writeActions,
1527 getStringId());
Saurav Das0a344b02014-09-26 14:18:52 -07001528 return mplsFlow;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001529 }
1530
Saurav Das0a344b02014-09-26 14:18:52 -07001531 private OFMessage getAclEntry(MatchActionOperationEntry mao) {
Saurav Dase972b3a2014-09-26 15:41:06 -07001532 MatchAction ma = mao.getTarget();
1533 Operator op = mao.getOperator();
1534 PacketMatch packetMatch = (PacketMatch) ma.getMatch();
1535 Builder matchBuilder = factory.buildMatch();
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001536
Saurav Dase972b3a2014-09-26 15:41:06 -07001537 // set match
1538 int inport = 0;
1539 if (ma.getSwitchPort() != null) {
1540 inport = (int) ma.getSwitchPort().getPortNumber().value();
1541 }
1542 final MACAddress srcMac = packetMatch.getSrcMacAddress();
1543 final MACAddress dstMac = packetMatch.getDstMacAddress();
1544 final Short etherType = packetMatch.getEtherType();
1545 final IPv4Net srcIp = packetMatch.getSrcIpAddress();
1546 final IPv4Net dstIp = packetMatch.getDstIpAddress();
1547 final Byte ipProto = packetMatch.getIpProtocolNumber();
1548 final Short srcTcpPort = packetMatch.getSrcTcpPortNumber();
1549 final Short dstTcpPort = packetMatch.getDstTcpPortNumber();
1550 if (inport > 0) {
1551 matchBuilder.setExact(MatchField.IN_PORT,
1552 OFPort.of(inport));
1553 }
1554 if (srcMac != null) {
1555 matchBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(srcMac.toLong()));
1556 }
1557 if (dstMac != null) {
1558 matchBuilder.setExact(MatchField.ETH_DST, MacAddress.of(dstMac.toLong()));
1559 }
1560 if (etherType != null) {
1561 matchBuilder.setExact(MatchField.ETH_TYPE, EthType.of(etherType));
1562 }
1563 if (srcIp != null) {
1564 matchBuilder.setMasked(MatchField.IPV4_SRC,
1565 IPv4Address.of(srcIp.address().value())
1566 .withMaskOfLength(srcIp.prefixLen()));
1567 }
1568 if (dstIp != null) {
1569 matchBuilder.setMasked(MatchField.IPV4_DST,
1570 IPv4Address.of(dstIp.address().value())
1571 .withMaskOfLength(dstIp.prefixLen()));
1572 }
1573 if (ipProto != null) {
1574 matchBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(ipProto));
1575 }
1576 if (srcTcpPort != null) {
1577 matchBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(srcTcpPort));
1578 }
1579 if (dstTcpPort != null) {
1580 matchBuilder.setExact(MatchField.TCP_DST, TransportPort.of(dstTcpPort));
1581 }
1582
1583 // set actions
Saurav Dascc3e35f2014-10-10 15:33:32 -07001584 List<OFAction> writeActions = new ArrayList<OFAction>();
Saurav Dase972b3a2014-09-26 15:41:06 -07001585 for (Action action : ma.getActions()) {
1586 OFAction ofAction = getOFAction(action);
1587 if (ofAction != null) {
Saurav Dascc3e35f2014-10-10 15:33:32 -07001588 writeActions.add(ofAction);
Saurav Dase972b3a2014-09-26 15:41:06 -07001589 }
1590 }
1591
1592 // set instructions
1593 OFInstruction clearInstr = factory.instructions().clearActions();
Saurav Dascc3e35f2014-10-10 15:33:32 -07001594 OFInstruction writeInstr = factory.instructions().buildWriteActions()
1595 .setActions(writeActions).build();
Saurav Dase972b3a2014-09-26 15:41:06 -07001596 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
1597 instructions.add(clearInstr);
Saurav Dascc3e35f2014-10-10 15:33:32 -07001598 instructions.add(writeInstr);
Saurav Dase972b3a2014-09-26 15:41:06 -07001599
1600 // set flow-mod
1601 OFFlowMod.Builder fmBuilder = null;
1602 switch (op) {
1603 case ADD:
1604 fmBuilder = factory.buildFlowAdd();
1605 break;
1606 case REMOVE:
1607 fmBuilder = factory.buildFlowDeleteStrict();
1608 break;
Sangho Shin5be3e532014-10-03 17:20:58 -07001609 case MODIFY: // TODO
1610 fmBuilder = factory.buildFlowModifyStrict();
1611 break;
Saurav Dase972b3a2014-09-26 15:41:06 -07001612 default:
1613 log.warn("Unsupported MatchAction Operator: {}", op);
1614 return null;
1615 }
1616
1617 OFMessage aclFlow = fmBuilder
1618 .setTableId(TableId.of(TABLE_ACL))
1619 .setMatch(matchBuilder.build())
1620 .setInstructions(instructions)
Sangho Shin6f47dd32014-10-17 23:10:39 -07001621 .setPriority(ma.getPriority()) // exact match and exclusive
Saurav Dase972b3a2014-09-26 15:41:06 -07001622 .setBufferId(OFBufferId.NO_BUFFER)
1623 .setIdleTimeout(0)
1624 .setHardTimeout(0)
1625 .setXid(getNextTransactionId())
1626 .build();
Sangho Shin15273b62014-10-16 22:22:05 -07001627
Saurav Das0a344b02014-09-26 14:18:52 -07001628 return aclFlow;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001629 }
1630
1631 // *****************************
1632 // IOF13Switch
1633 // *****************************
1634
1635 @Override
1636 public void pushFlow(MatchActionOperationEntry matchActionOp) throws IOException {
Saurav Das0a344b02014-09-26 14:18:52 -07001637 OFMessage ofm = getFlow(matchActionOp);
1638 if (ofm != null) {
1639 write(Collections.singletonList(ofm));
1640 }
1641 }
1642
1643 private OFMessage getFlow(MatchActionOperationEntry matchActionOp) {
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001644 final MatchAction matchAction = matchActionOp.getTarget();
1645 final Match match = matchAction.getMatch();
1646 if (match instanceof Ipv4Match) {
Saurav Das0a344b02014-09-26 14:18:52 -07001647 return getIpEntry(matchActionOp);
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001648 } else if (match instanceof MplsMatch) {
Saurav Das0a344b02014-09-26 14:18:52 -07001649 return getMplsEntry(matchActionOp);
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001650 } else if (match instanceof PacketMatch) {
Saurav Das0a344b02014-09-26 14:18:52 -07001651 return getAclEntry(matchActionOp);
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001652 } else {
1653 log.error("Unknown match type {} pushed to switch {}", match,
1654 getStringId());
1655 }
Saurav Das0a344b02014-09-26 14:18:52 -07001656 return null;
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001657 }
1658
1659 @Override
1660 public void pushFlows(Collection<MatchActionOperationEntry> matchActionOps)
1661 throws IOException {
Saurav Das0a344b02014-09-26 14:18:52 -07001662 List<OFMessage> flowMods = new ArrayList<OFMessage>();
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001663 for (MatchActionOperationEntry matchActionOp : matchActionOps) {
Saurav Das0a344b02014-09-26 14:18:52 -07001664 OFMessage ofm = getFlow(matchActionOp);
1665 if (ofm != null) {
1666 flowMods.add(ofm);
1667 }
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001668 }
Saurav Das0a344b02014-09-26 14:18:52 -07001669 write(flowMods);
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001670 }
1671
1672 @Override
1673 public int getEcmpGroupId(NeighborSet ns) {
1674 EcmpInfo ei = ecmpGroups.get(ns);
1675 if (ei == null) {
1676 return -1;
1677 } else {
1678 return ei.groupId;
1679 }
1680 }
1681
Saurav Dascc3e35f2014-10-10 15:33:32 -07001682 @Override
1683 public TableId getTableId(String tableType) {
1684 tableType = tableType.toLowerCase();
1685 if (tableType.contentEquals("ip")) {
1686 return TableId.of(OFSwitchImplCPqD13.TABLE_IPv4_UNICAST);
1687 }
1688 else if (tableType.contentEquals("mpls")) {
1689 return TableId.of(OFSwitchImplCPqD13.TABLE_MPLS);
1690 }
1691 else if (tableType.contentEquals("acl")) {
1692 return TableId.of(OFSwitchImplCPqD13.TABLE_ACL);
1693 }
1694 else {
1695 log.warn("Invalid tableType: {}", tableType);
1696 return null;
1697 }
1698 }
1699
Srikanth Vavilapallifd93b1f2014-10-23 14:15:21 -07001700 private EcmpInfo createIndirectGroup(int groupId, MacAddress srcMac,
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001701 MacAddress dstMac, PortNumber outPort, int gotoGroupNo,
1702 int mplsLabel, boolean bos) {
1703 List<BucketInfo> buckets = new ArrayList<BucketInfo>();
1704 BucketInfo b = new BucketInfo(null, srcMac, dstMac, outPort,
1705 mplsLabel, bos, gotoGroupNo);
1706 buckets.add(b);
1707
1708 EcmpInfo ecmpInfo = new EcmpInfo(groupId, buckets);
1709 setEcmpGroup(ecmpInfo);
1710 log.debug(
1711 "createIndirectGroup: Creating indirect group {} in sw {} "
1712 + "with: {}", groupId, getStringId(), ecmpInfo);
Srikanth Vavilapallifd93b1f2014-10-23 14:15:21 -07001713 return ecmpInfo;
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001714 }
1715
1716 private EcmpInfo createInnermostLabelGroup(int innermostGroupId,
1717 List<PortNumber> ports, int mplsLabel, boolean bos,
1718 HashMap<PortNumber, Integer> lastSetOfGroupIds) {
1719 List<BucketInfo> buckets = new ArrayList<BucketInfo>();
1720 for (PortNumber sp : ports) {
1721 Dpid neighborDpid = portToNeighbors.get(sp);
1722 BucketInfo b = new BucketInfo(neighborDpid,
1723 MacAddress.of(srConfig.getRouterMac()),
1724 getNeighborRouterMacAddress(neighborDpid), null,
1725 mplsLabel, bos,
1726 lastSetOfGroupIds.get(sp));
1727 buckets.add(b);
1728 }
1729 EcmpInfo ecmpInfo = new EcmpInfo(innermostGroupId, buckets);
1730 setEcmpGroup(ecmpInfo);
1731 log.debug(
Srikanth Vavilapallifd93b1f2014-10-23 14:15:21 -07001732 "createInnermostLabelGroup: Creating select group {} in sw {} "
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001733 + "with: {}", innermostGroupId, getStringId(), ecmpInfo);
1734 return ecmpInfo;
1735 }
1736 @Override
1737 /**
Srikanth Vavilapalli1167d0c2014-10-23 09:43:42 -07001738 * Create a group chain with the specified label stack for a given set of
1739 * ports. This API can be used by user to create groups for a tunnel based
1740 * policy routing scenario. NOTE: This API can not be used if a group to be
1741 * created with different label stacks for each port in the given set of
1742 * ports. Use XXX API for this purpose
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001743 *
Srikanth Vavilapalli1167d0c2014-10-23 09:43:42 -07001744 * @param labelStack list of router segment Ids to be pushed. Can be empty.
1745 * labelStack is processed from left to right with leftmost
1746 * representing the outermost label and rightmost representing
1747 * innermost label to be pushed
1748 * @param ports List of ports on this switch to get to the first router in
1749 * the labelStack
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001750 * @return group identifier
1751 */
1752 public int createGroup(List<Integer> labelStack, List<PortNumber> ports) {
1753
1754 if ((ports == null) ||
1755 ((labelStack != null) && (labelStack.size() > 3))) {
1756 log.warn("createGroup with wrong input parameters");
1757 }
1758 log.debug("createGroup with labelStack {} and ports {}",
1759 labelStack, ports);
1760
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001761 HashMap<PortNumber, Integer> lastSetOfGroupIds =
1762 new HashMap<PortNumber, Integer>();
1763 int innermostGroupId = -1;
Srikanth Vavilapalli1167d0c2014-10-23 09:43:42 -07001764 /* If it is empty label stack or label stack with only one label,
1765 * Create a single select group with buckets for each port in the list
1766 * of specified ports and specified label if any and return the
1767 * created group id
1768 */
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001769 if (labelStack.size() < 2) {
1770 int curLabel = -1;
1771 boolean bos = false;
1772 if (labelStack.size()==1) {
1773 curLabel = labelStack.get(0).intValue();
1774 bos = true;
1775 }
1776
1777 List<BucketInfo> buckets = new ArrayList<BucketInfo>();
1778 for (PortNumber sp : ports) {
1779 Dpid neighborDpid = portToNeighbors.get(sp);
1780 BucketInfo b = new BucketInfo(neighborDpid,
1781 MacAddress.of(srConfig.getRouterMac()),
1782 getNeighborRouterMacAddress(neighborDpid),
1783 sp, curLabel, bos, -1);
1784 buckets.add(b);
1785 }
1786 innermostGroupId = groupid.incrementAndGet();
1787 EcmpInfo ecmpInfo = new EcmpInfo(innermostGroupId, buckets);
1788 setEcmpGroup(ecmpInfo);
1789 userDefinedGroups.put(innermostGroupId, ecmpInfo);
1790 return innermostGroupId;
1791 }
1792
Srikanth Vavilapalli1167d0c2014-10-23 09:43:42 -07001793 /* If the label stack has two or more labels, then a chain of groups
1794 * to be created.
1795 * Step1: Create for each port in the list of specified ports,
1796 * an indirect group with the outermost label. These groups are the
1797 * end of the chain and hence don't reference to any other groups
1798 * Step2: Create for each port in the list of specified ports, an
1799 * indirect group with middle labels (if any). These groups will
1800 * have references to group ids that are created in the previous
1801 * iteration for the same ports
1802 * Step3: Create a select group with all ports and innermost label.
1803 * This group will have references to indirect group ids that are
1804 * created in the previous iteration for the same ports
1805 */
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001806 for (int i = 0; i < labelStack.size(); i++) {
1807 for (PortNumber sp : ports) {
1808 if (i == 0) {
1809 /* Outermost label processing */
1810 int currGroupId = groupid.incrementAndGet();
Srikanth Vavilapallifd93b1f2014-10-23 14:15:21 -07001811 EcmpInfo indirectGroup = createIndirectGroup(currGroupId,
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001812 null, null, sp, -1,
1813 labelStack.get(i).intValue(), false);
1814 lastSetOfGroupIds.put(sp, currGroupId);
Srikanth Vavilapallifd93b1f2014-10-23 14:15:21 -07001815 userDefinedGroups.put(currGroupId, indirectGroup);
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001816 }
1817 else if (i == (labelStack.size() - 1)) {
1818 /* Innermost label processing */
1819 innermostGroupId = groupid.incrementAndGet();
1820 EcmpInfo topLevelGroup = createInnermostLabelGroup(
1821 innermostGroupId,
1822 ports,
1823 labelStack.get(i).intValue(), true,
1824 lastSetOfGroupIds);
1825 userDefinedGroups.put(
1826 innermostGroupId, topLevelGroup);
1827 break;
1828 }
1829 else {
1830 /* Middle label processing */
1831 int currGroupId = groupid.incrementAndGet();
Srikanth Vavilapallifd93b1f2014-10-23 14:15:21 -07001832 EcmpInfo indirectGroup = createIndirectGroup(currGroupId,
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001833 null, null, null,
1834 lastSetOfGroupIds.get(sp),
1835 labelStack.get(i).intValue(), false);
1836 /* Overwrite with this iteration's group IDs */
1837 lastSetOfGroupIds.put(sp, currGroupId);
Srikanth Vavilapallifd93b1f2014-10-23 14:15:21 -07001838 userDefinedGroups.put(currGroupId, indirectGroup);
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001839 }
1840 }
1841 }
Srikanth Vavilapallifd93b1f2014-10-23 14:15:21 -07001842 log.debug("createGroup: group created with innermost group id {}",
1843 innermostGroupId);
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001844 return innermostGroupId;
1845 }
1846
Srikanth Vavilapalli16a04722014-10-23 11:09:24 -07001847 /*
1848 @Override
1849 public void removeTunnel(String tunnelId) {
1850 List<Integer> groups = tunnelGroupIdTable.get(tunnelId);
1851 if (groups == null)
1852 return;
1853
1854 // we need to delete groups in reverse order
1855 for (int i = groups.size() - 1; i >= 0; i--) {
1856 int groupId = groups.get(i);
1857 deleteGroup(groupId);
1858 }
1859 tunnelGroupIdTable.remove(tunnelId);
1860 }
1861 */
1862
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001863 /**
1864 * Remove the specified group
1865 *
1866 * @param groupId group identifier
1867 * @return success/fail
1868 */
1869 public boolean removeGroup(int groupId) {
Srikanth Vavilapallifd93b1f2014-10-23 14:15:21 -07001870 EcmpInfo group = userDefinedGroups.get(groupId);
1871 if (group == null) {
1872 log.warn("removeGroup: with invalid group id");
1873 return false;
1874 }
1875 for (BucketInfo bucket : group.buckets) {
1876 int currGroupIdToBeDeleted = bucket.groupNo;
1877 while (currGroupIdToBeDeleted != -1) {
1878 /* Assuming indirect groups with single buckets */
1879 int nextGroupIdToBeDeleted =
1880 userDefinedGroups.get(currGroupIdToBeDeleted).
1881 buckets.get(0).groupNo;
Sangho Shin93f623c2014-10-24 12:59:53 -07001882 deleteGroup(currGroupIdToBeDeleted);
Srikanth Vavilapallifd93b1f2014-10-23 14:15:21 -07001883 userDefinedGroups.remove(currGroupIdToBeDeleted);
1884 currGroupIdToBeDeleted = nextGroupIdToBeDeleted;
1885 }
1886 }
1887
Sangho Shin93f623c2014-10-24 12:59:53 -07001888 deleteGroup(groupId);
1889 userDefinedGroups.remove(groupId);
Srikanth Vavilapallifd93b1f2014-10-23 14:15:21 -07001890 log.debug("removeGroup: removed group with group id {}", groupId);
1891 return true;
Srikanth Vavilapalli2edeada2014-10-22 19:19:01 -07001892 }
Srikanth Vavilapalli16a04722014-10-23 11:09:24 -07001893
Saurav Das2d41a432014-10-21 20:40:10 -07001894 @Override
1895 public Map<String, String> getPublishAttributes() {
1896 return publishAttributes;
1897 }
Sangho Shin1ad7be02014-10-20 16:56:49 -07001898
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001899 // *****************************
Saurav Das80d17392014-10-01 10:24:56 -07001900 // Unused
Saurav Dasfc5e3eb2014-09-25 19:05:21 -07001901 // *****************************
1902
Saurav Das80d17392014-10-01 10:24:56 -07001903 @SuppressWarnings("unused")
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001904 private void setAsyncConfig() throws IOException {
1905 List<OFMessage> msglist = new ArrayList<OFMessage>(3);
1906 OFMessage setAC = null;
1907
1908 if (role == Role.MASTER) {
1909 setAC = factory.buildAsyncSet()
1910 .setFlowRemovedMaskEqualMaster(SET_FLOW_REMOVED_MASK_MASTER)
1911 .setPacketInMaskEqualMaster(SET_PACKET_IN_MASK_MASTER)
1912 .setPortStatusMaskEqualMaster(SET_PORT_STATUS_MASK_MASTER)
1913 .setFlowRemovedMaskSlave(SET_ALL_SLAVE)
1914 .setPacketInMaskSlave(SET_ALL_SLAVE)
1915 .setPortStatusMaskSlave(SET_ALL_SLAVE)
1916 .setXid(getNextTransactionId())
1917 .build();
1918 } else if (role == Role.EQUAL) {
1919 setAC = factory.buildAsyncSet()
1920 .setFlowRemovedMaskEqualMaster(SET_FLOW_REMOVED_MASK_EQUAL)
1921 .setPacketInMaskEqualMaster(SET_PACKET_IN_MASK_EQUAL)
1922 .setPortStatusMaskEqualMaster(SET_PORT_STATUS_MASK_EQUAL)
1923 .setFlowRemovedMaskSlave(SET_ALL_SLAVE)
1924 .setPacketInMaskSlave(SET_ALL_SLAVE)
1925 .setPortStatusMaskSlave(SET_ALL_SLAVE)
1926 .setXid(getNextTransactionId())
1927 .build();
1928 }
1929 msglist.add(setAC);
1930
1931 OFMessage br = factory.buildBarrierRequest()
1932 .setXid(getNextTransactionId())
1933 .build();
1934 msglist.add(br);
1935
1936 OFMessage getAC = factory.buildAsyncGetRequest()
1937 .setXid(getNextTransactionId())
1938 .build();
1939 msglist.add(getAC);
1940
1941 write(msglist);
1942 }
1943
Saurav Das80d17392014-10-01 10:24:56 -07001944 @SuppressWarnings("unused")
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001945 private void decodeAsyncGetReply(OFAsyncGetReply rep) {
1946 long frm = rep.getFlowRemovedMaskEqualMaster();
Sangho Shin01bca862014-09-12 11:18:59 -07001947 long frs = rep.getFlowRemovedMaskSlave();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001948 long pim = rep.getPacketInMaskEqualMaster();
Sangho Shin01bca862014-09-12 11:18:59 -07001949 long pis = rep.getPacketInMaskSlave();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001950 long psm = rep.getPortStatusMaskEqualMaster();
Sangho Shin01bca862014-09-12 11:18:59 -07001951 long pss = rep.getPortStatusMaskSlave();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001952
1953 if (role == Role.MASTER || role == Role.EQUAL) { // should separate
1954 log.info("FRM:{}", HexString.toHexString((frm & TEST_FLOW_REMOVED_MASK)));
1955 log.info("PIM:{}", HexString.toHexString((pim & TEST_PACKET_IN_MASK)));
1956 log.info("PSM:{}", HexString.toHexString((psm & TEST_PORT_STATUS_MASK)));
1957 }
1958
1959 }
1960
Saurav Das80d17392014-10-01 10:24:56 -07001961 @SuppressWarnings("unused")
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001962 private void getTableFeatures() throws IOException {
1963 OFMessage gtf = factory.buildTableFeaturesStatsRequest()
1964 .setXid(getNextTransactionId())
1965 .build();
1966 write(gtf, null);
1967 }
1968
Saurav Das80d17392014-10-01 10:24:56 -07001969 @SuppressWarnings("unused")
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001970 private void sendGroupFeaturesRequest() throws IOException {
1971 OFMessage gfr = factory.buildGroupFeaturesStatsRequest()
1972 .setXid(getNextTransactionId())
1973 .build();
1974 write(gfr, null);
1975 }
1976
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001977 private void processGroupFeatures(OFGroupFeaturesStatsReply gfsr) {
1978 log.info("Sw: {} Group Features {}", getStringId(), gfsr);
1979 }
1980
Saurav Dascc3e35f2014-10-10 15:33:32 -07001981 @SuppressWarnings("unused")
1982 private void testMultipleLabels() {
1983 if (getId() == 1) {
1984 List<OFMessage> msglist = new ArrayList<OFMessage>();
1985
1986 // first all the indirect groups
1987
1988 // the group to switch 2 with outer label
1989 OFGroup g1 = OFGroup.of(201);
1990 OFOxmEthDst dmac1 = factory.oxms().ethDst(MacAddress.of("00:00:02:02:02:80"));
1991 OFAction push1 = factory.actions().pushMpls(EthType.MPLS_UNICAST);
1992 OFOxmMplsLabel lid1 = factory.oxms()
1993 .mplsLabel(U32.of(105)); // outer label
1994 OFAction setMpls1 = factory.actions().buildSetField()
1995 .setField(lid1).build();
1996 OFOxmMplsBos bos1 = factory.oxms()
1997 .mplsBos(OFBooleanValue.FALSE);
1998 OFAction setB1 = factory.actions().buildSetField()
1999 .setField(bos1).build();
2000 OFAction setDA1 = factory.actions().buildSetField()
2001 .setField(dmac1).build();
2002 OFAction outp1 = factory.actions().buildOutput()
2003 .setPort(OFPort.of(2))
2004 .build();
2005 List<OFAction> a1 = new ArrayList<OFAction>();
2006 a1.add(push1);
2007 a1.add(setMpls1);
2008 a1.add(setB1);
2009 a1.add(setDA1);
2010 a1.add(outp1);
2011 OFBucket b1 = factory.buildBucket()
2012 .setActions(a1)
2013 .build();
2014 OFMessage gm1 = factory.buildGroupAdd()
2015 .setGroup(g1)
2016 .setBuckets(Collections.singletonList(b1))
2017 .setGroupType(OFGroupType.INDIRECT)
2018 .setXid(getNextTransactionId())
2019 .build();
2020 msglist.add(gm1);
2021
2022 // the group to switch 3 with outer label
2023 OFGroup g2 = OFGroup.of(301);
2024 OFOxmEthDst dmac2 = factory.oxms().ethDst(MacAddress.of("00:00:03:03:03:80"));
2025 OFAction push2 = factory.actions().pushMpls(EthType.MPLS_UNICAST);
2026 OFOxmMplsLabel lid2 = factory.oxms()
2027 .mplsLabel(U32.of(104)); // outer label
2028 OFAction setMpls2 = factory.actions().buildSetField()
2029 .setField(lid2).build();
2030 OFOxmMplsBos bos2 = factory.oxms()
2031 .mplsBos(OFBooleanValue.FALSE);
2032 OFAction setB2 = factory.actions().buildSetField()
2033 .setField(bos2).build();
2034 OFAction setDA2 = factory.actions().buildSetField()
2035 .setField(dmac2).build();
2036 OFAction outp2 = factory.actions().buildOutput()
2037 .setPort(OFPort.of(3))
2038 .build();
2039 List<OFAction> a2 = new ArrayList<OFAction>();
2040 a2.add(push2);
2041 a2.add(setMpls2);
2042 a2.add(setB2);
2043 a2.add(setDA2);
2044 a2.add(outp2);
2045 OFBucket b2 = factory.buildBucket()
2046 .setActions(a2)
2047 .build();
2048 OFMessage gm2 = factory.buildGroupAdd()
2049 .setGroup(g2)
2050 .setBuckets(Collections.singletonList(b2))
2051 .setGroupType(OFGroupType.INDIRECT)
2052 .setXid(getNextTransactionId())
2053 .build();
2054 msglist.add(gm2);
2055
2056 // now add main ECMP group with inner labels
2057 OFGroup group = OFGroup.of(786);
2058 List<OFBucket> buckets = new ArrayList<OFBucket>();
2059 for (int i = 0; i < 2; i++) { // 2 buckets
2060
2061 List<OFAction> actions = new ArrayList<OFAction>();
2062 OFOxmEthSrc smac = factory.oxms()
2063 .ethSrc(MacAddress.of("00:00:01:01:01:80"));
2064 OFAction setSA = factory.actions().buildSetField()
2065 .setField(smac).build();
2066 actions.add(setSA);
2067
2068 if (i == 0) {
2069 // send to switch 2
2070 OFAction pushX = factory.actions().pushMpls(EthType.MPLS_UNICAST);
2071 OFOxmMplsLabel lidX = factory.oxms()
2072 .mplsLabel(U32.of(106)); // inner label
2073 OFAction setX = factory.actions().buildSetField()
2074 .setField(lidX).build();
2075 OFOxmMplsBos bosX = factory.oxms()
2076 .mplsBos(OFBooleanValue.TRUE);
2077 OFAction setBX = factory.actions().buildSetField()
2078 .setField(bosX).build();
2079 OFAction ogX = factory.actions().buildGroup()
2080 .setGroup(g1).build();
2081 actions.add(pushX);
2082 actions.add(setX);
2083 actions.add(setBX);
2084 actions.add(ogX);
2085
2086 } else {
2087 // send to switch 3
2088 OFAction pushY = factory.actions().pushMpls(EthType.MPLS_UNICAST);
2089 OFOxmMplsLabel lidY = factory.oxms()
2090 .mplsLabel(U32.of(106)); // inner label
2091 OFAction setY = factory.actions().buildSetField()
2092 .setField(lidY).build();
2093 OFOxmMplsBos bosY = factory.oxms()
2094 .mplsBos(OFBooleanValue.TRUE);
2095 OFAction setBY = factory.actions().buildSetField()
2096 .setField(bosY).build();
2097 OFAction ogY = factory.actions().buildGroup()
2098 .setGroup(g2).build();
2099 actions.add(pushY);
2100 actions.add(setY);
2101 actions.add(setBY);
2102 actions.add(ogY);
2103 }
2104
2105 OFBucket ofb = factory.buildBucket()
2106 .setWeight(1)
2107 .setActions(actions)
2108 .build();
2109 buckets.add(ofb);
2110 }
2111
2112 OFMessage gm = factory.buildGroupAdd()
2113 .setGroup(group)
2114 .setBuckets(buckets)
2115 .setGroupType(OFGroupType.SELECT)
2116 .setXid(getNextTransactionId())
2117 .build();
2118 msglist.add(gm);
2119
2120 // create an ACL entry to use this ecmp group
2121 Builder matchBuilder = factory.buildMatch();
2122 matchBuilder.setExact(MatchField.ETH_TYPE, EthType.of(0x800));
2123 matchBuilder.setMasked(MatchField.IPV4_DST,
2124 IPv4Address.of("7.7.7.0")
2125 .withMaskOfLength(24));
2126
2127 OFAction grp = factory.actions().buildGroup()
2128 .setGroup(OFGroup.of(786))
2129 .build();
2130 List<OFAction> writeActions = Collections.singletonList(grp);
2131
2132 OFInstruction clearInstr = factory.instructions().clearActions();
2133 OFInstruction writeInstr = factory.instructions().buildWriteActions()
2134 .setActions(writeActions).build();
2135 List<OFInstruction> instructions = new ArrayList<OFInstruction>();
2136 instructions.add(clearInstr);
2137 instructions.add(writeInstr);
2138
2139 OFFlowMod.Builder fmBuilder = factory.buildFlowAdd();
2140
2141 OFMessage aclFlow = fmBuilder
2142 .setTableId(TableId.of(TABLE_ACL))
2143 .setMatch(matchBuilder.build())
2144 .setInstructions(instructions)
2145 .setPriority(10) // TODO: wrong - should be MA
2146 // priority
2147 .setBufferId(OFBufferId.NO_BUFFER)
2148 .setIdleTimeout(0)
2149 .setHardTimeout(0)
2150 .setXid(getNextTransactionId())
2151 .build();
2152 msglist.add(aclFlow);
2153
2154 try {
2155 write(msglist);
2156 } catch (IOException e) {
2157 // TODO Auto-generated catch block
2158 e.printStackTrace();
2159 }
Fahad Naeem Khand563af62014-10-08 17:37:25 -07002160 }
2161 }
2162
Saurav Das2d41a432014-10-21 20:40:10 -07002163
2164
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07002165}