Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.drivermanager; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.ArrayList; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 5 | import java.util.Collection; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 6 | import java.util.Collections; |
Saurav Das | 2d41a43 | 2014-10-21 20:40:10 -0700 | [diff] [blame] | 7 | import java.util.HashMap; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 8 | import java.util.HashSet; |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 9 | import java.util.Iterator; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 10 | import java.util.List; |
Saurav Das | 2d41a43 | 2014-10-21 20:40:10 -0700 | [diff] [blame] | 11 | import java.util.Map; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 12 | import java.util.Set; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 13 | import java.util.concurrent.ConcurrentHashMap; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 14 | import java.util.concurrent.ConcurrentMap; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 15 | import java.util.concurrent.atomic.AtomicBoolean; |
Srikanth Vavilapalli | 25fbe39 | 2014-10-10 15:04:51 -0700 | [diff] [blame] | 16 | import java.util.concurrent.atomic.AtomicInteger; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 17 | |
| 18 | import net.floodlightcontroller.core.IFloodlightProviderService.Role; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 19 | import net.floodlightcontroller.core.IOF13Switch; |
| 20 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 21 | import net.floodlightcontroller.core.SwitchDriverSubHandshakeAlreadyStarted; |
| 22 | import net.floodlightcontroller.core.SwitchDriverSubHandshakeCompleted; |
| 23 | import net.floodlightcontroller.core.SwitchDriverSubHandshakeNotStarted; |
| 24 | import net.floodlightcontroller.core.internal.OFSwitchImplBase; |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 25 | import net.floodlightcontroller.util.MACAddress; |
Srikanth Vavilapalli | 25fbe39 | 2014-10-10 15:04:51 -0700 | [diff] [blame] | 26 | import net.floodlightcontroller.util.OrderedCollection; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.configmanager.INetworkConfigService; |
| 28 | import net.onrc.onos.core.configmanager.INetworkConfigService.NetworkConfigState; |
| 29 | import net.onrc.onos.core.configmanager.INetworkConfigService.SwitchConfigStatus; |
| 30 | import net.onrc.onos.core.configmanager.NetworkConfig.LinkConfig; |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 31 | import net.onrc.onos.core.configmanager.NetworkConfig.SwitchConfig; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.configmanager.NetworkConfigManager; |
| 33 | import net.onrc.onos.core.configmanager.PktLinkConfig; |
| 34 | import net.onrc.onos.core.configmanager.SegmentRouterConfig; |
Saurav Das | 2d41a43 | 2014-10-21 20:40:10 -0700 | [diff] [blame] | 35 | import net.onrc.onos.core.configmanager.SegmentRouterConfig.AdjacencySid; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 36 | import net.onrc.onos.core.matchaction.MatchAction; |
| 37 | import net.onrc.onos.core.matchaction.MatchActionOperationEntry; |
Saurav Das | bc594a4 | 2014-09-25 20:13:50 -0700 | [diff] [blame] | 38 | import net.onrc.onos.core.matchaction.MatchActionOperations; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 39 | import net.onrc.onos.core.matchaction.MatchActionOperations.Operator; |
| 40 | import net.onrc.onos.core.matchaction.action.Action; |
| 41 | import net.onrc.onos.core.matchaction.action.CopyTtlInAction; |
| 42 | import net.onrc.onos.core.matchaction.action.CopyTtlOutAction; |
| 43 | import net.onrc.onos.core.matchaction.action.DecMplsTtlAction; |
| 44 | import net.onrc.onos.core.matchaction.action.DecNwTtlAction; |
| 45 | import net.onrc.onos.core.matchaction.action.GroupAction; |
| 46 | import net.onrc.onos.core.matchaction.action.ModifyDstMacAction; |
| 47 | import net.onrc.onos.core.matchaction.action.ModifySrcMacAction; |
| 48 | import net.onrc.onos.core.matchaction.action.OutputAction; |
| 49 | import net.onrc.onos.core.matchaction.action.PopMplsAction; |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 50 | import net.onrc.onos.core.matchaction.action.PushMplsAction; |
| 51 | import net.onrc.onos.core.matchaction.action.SetMplsBosAction; |
| 52 | import net.onrc.onos.core.matchaction.action.SetMplsIdAction; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 53 | import net.onrc.onos.core.matchaction.match.Ipv4Match; |
| 54 | import net.onrc.onos.core.matchaction.match.Match; |
| 55 | import net.onrc.onos.core.matchaction.match.MplsMatch; |
| 56 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
| 57 | import net.onrc.onos.core.util.Dpid; |
| 58 | import net.onrc.onos.core.util.IPv4Net; |
| 59 | import net.onrc.onos.core.util.PortNumber; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 60 | |
Saurav Das | 2d41a43 | 2014-10-21 20:40:10 -0700 | [diff] [blame] | 61 | import org.codehaus.jackson.map.ObjectMapper; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.protocol.OFAsyncGetReply; |
| 63 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 64 | import org.projectfloodlight.openflow.protocol.OFBucket; |
| 65 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 66 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
| 67 | import org.projectfloodlight.openflow.protocol.OFFactory; |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 69 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
| 70 | import org.projectfloodlight.openflow.protocol.OFGroupFeaturesStatsReply; |
| 71 | import org.projectfloodlight.openflow.protocol.OFGroupType; |
| 72 | import org.projectfloodlight.openflow.protocol.OFMatchV3; |
| 73 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 74 | import org.projectfloodlight.openflow.protocol.OFOxmList; |
| 75 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
Srikanth Vavilapalli | 25fbe39 | 2014-10-10 15:04:51 -0700 | [diff] [blame] | 76 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 77 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
| 78 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 79 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 80 | import org.projectfloodlight.openflow.protocol.match.Match.Builder; |
| 81 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 82 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthDst; |
| 83 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthSrc; |
| 84 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthType; |
| 85 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort; |
| 86 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmIpv4DstMasked; |
Saurav Das | cc3e35f | 2014-10-10 15:33:32 -0700 | [diff] [blame] | 87 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsBos; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 88 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel; |
| 89 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid; |
| 90 | import org.projectfloodlight.openflow.types.EthType; |
| 91 | import org.projectfloodlight.openflow.types.IPv4Address; |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 92 | import org.projectfloodlight.openflow.types.IpProtocol; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 93 | import org.projectfloodlight.openflow.types.MacAddress; |
Saurav Das | cc3e35f | 2014-10-10 15:33:32 -0700 | [diff] [blame] | 94 | import org.projectfloodlight.openflow.types.OFBooleanValue; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 95 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 96 | import org.projectfloodlight.openflow.types.OFGroup; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 97 | import org.projectfloodlight.openflow.types.OFPort; |
| 98 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
| 99 | import org.projectfloodlight.openflow.types.TableId; |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 100 | import org.projectfloodlight.openflow.types.TransportPort; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 101 | import org.projectfloodlight.openflow.types.U32; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 102 | import org.projectfloodlight.openflow.util.HexString; |
| 103 | |
| 104 | /** |
| 105 | * OFDescriptionStatistics Vendor (Manufacturer Desc.): Stanford University, |
| 106 | * Ericsson Research and CPqD Research. Make (Hardware Desc.) : OpenFlow 1.3 |
| 107 | * Reference Userspace Switch Model (Datapath Desc.) : None Software : Serial : |
| 108 | * None |
| 109 | */ |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 110 | public class OFSwitchImplCPqD13 extends OFSwitchImplBase implements IOF13Switch { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 111 | private AtomicBoolean driverHandshakeComplete; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 112 | private AtomicBoolean haltStateMachine; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 113 | private OFFactory factory; |
| 114 | private static final int OFPCML_NO_BUFFER = 0xffff; |
| 115 | // Configuration of asynch messages to controller. We need different |
| 116 | // asynch messages depending on role-equal or role-master. |
| 117 | // We don't want to get anything if we are slave. |
| 118 | private static final long SET_FLOW_REMOVED_MASK_MASTER = 0xf; |
| 119 | private static final long SET_PACKET_IN_MASK_MASTER = 0x7; |
| 120 | private static final long SET_PORT_STATUS_MASK_MASTER = 0x7; |
| 121 | private static final long SET_FLOW_REMOVED_MASK_EQUAL = 0x0; |
| 122 | private static final long SET_PACKET_IN_MASK_EQUAL = 0x0; |
| 123 | private static final long SET_PORT_STATUS_MASK_EQUAL = 0x7; |
| 124 | private static final long SET_ALL_SLAVE = 0x0; |
| 125 | |
| 126 | private static final long TEST_FLOW_REMOVED_MASK = 0xf; |
| 127 | private static final long TEST_PACKET_IN_MASK = 0x7; |
| 128 | private static final long TEST_PORT_STATUS_MASK = 0x7; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 129 | |
| 130 | private static final int TABLE_VLAN = 0; |
| 131 | private static final int TABLE_TMAC = 1; |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 132 | private static final int TABLE_IPv4_UNICAST = 2; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 133 | private static final int TABLE_MPLS = 3; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 134 | private static final int TABLE_ACL = 5; |
| 135 | |
| 136 | private static final short MAX_PRIORITY = (short) 0xffff; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 137 | private static final short PRIORITY_MULTIPLIER = (short) 2046; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 138 | private static final short MIN_PRIORITY = 0x0; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 139 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 140 | private long barrierXidToWaitFor = -1; |
| 141 | private DriverState driverState; |
Jonathan Hart | cb34f38 | 2014-08-12 21:11:03 -0700 | [diff] [blame] | 142 | private final boolean usePipeline13; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 143 | private SegmentRouterConfig srConfig; |
| 144 | private ConcurrentMap<Dpid, Set<PortNumber>> neighbors; |
Srikanth Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 145 | private ConcurrentMap<PortNumber, Dpid> portToNeighbors; |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 146 | private List<Integer> segmentIds; |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 147 | private boolean isEdgeRouter; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 148 | private ConcurrentMap<NeighborSet, EcmpInfo> ecmpGroups; |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 149 | private ConcurrentMap<String, List<Integer>> tunnelGroupIdTable; |
Srikanth Vavilapalli | b95e6e0 | 2014-10-02 13:56:39 -0700 | [diff] [blame] | 150 | private ConcurrentMap<PortNumber, ArrayList<NeighborSet>> portNeighborSetMap; |
Srikanth Vavilapalli | 25fbe39 | 2014-10-10 15:04:51 -0700 | [diff] [blame] | 151 | private AtomicInteger groupid; |
Saurav Das | 2d41a43 | 2014-10-21 20:40:10 -0700 | [diff] [blame] | 152 | private Map<String, String> publishAttributes; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 153 | |
Jonathan Hart | cb34f38 | 2014-08-12 21:11:03 -0700 | [diff] [blame] | 154 | public OFSwitchImplCPqD13(OFDescStatsReply desc, boolean usePipeline13) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 155 | super(); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 156 | haltStateMachine = new AtomicBoolean(false); |
| 157 | driverState = DriverState.INIT; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 158 | driverHandshakeComplete = new AtomicBoolean(false); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 159 | setSwitchDescription(desc); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 160 | neighbors = new ConcurrentHashMap<Dpid, Set<PortNumber>>(); |
Srikanth Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 161 | portToNeighbors = new ConcurrentHashMap<PortNumber, Dpid>(); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 162 | ecmpGroups = new ConcurrentHashMap<NeighborSet, EcmpInfo>(); |
Srikanth Vavilapalli | b95e6e0 | 2014-10-02 13:56:39 -0700 | [diff] [blame] | 163 | portNeighborSetMap = |
| 164 | new ConcurrentHashMap<PortNumber, ArrayList<NeighborSet>>(); |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 165 | tunnelGroupIdTable = new ConcurrentHashMap<String, List<Integer>>(); |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 166 | segmentIds = new ArrayList<Integer>(); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 167 | isEdgeRouter = false; |
Srikanth Vavilapalli | 25fbe39 | 2014-10-10 15:04:51 -0700 | [diff] [blame] | 168 | groupid = new AtomicInteger(0); |
Jonathan Hart | cb34f38 | 2014-08-12 21:11:03 -0700 | [diff] [blame] | 169 | this.usePipeline13 = usePipeline13; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 172 | // ***************************** |
| 173 | // OFSwitchImplBase |
| 174 | // ***************************** |
| 175 | |
| 176 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 177 | /* (non-Javadoc) |
| 178 | * @see java.lang.Object#toString() |
| 179 | */ |
| 180 | @Override |
| 181 | public String toString() { |
| 182 | return "OFSwitchImplCPqD13 [" + ((channel != null) |
| 183 | ? channel.getRemoteAddress() : "?") |
| 184 | + " DPID[" + ((stringId != null) ? stringId : "?") + "]]"; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void startDriverHandshake() throws IOException { |
| 189 | log.debug("Starting driver handshake for sw {}", getStringId()); |
| 190 | if (startDriverHandshakeCalled) { |
| 191 | throw new SwitchDriverSubHandshakeAlreadyStarted(); |
| 192 | } |
| 193 | startDriverHandshakeCalled = true; |
Jonathan Hart | a213bce | 2014-08-11 15:44:07 -0700 | [diff] [blame] | 194 | factory = getFactory(); |
Jonathan Hart | cb34f38 | 2014-08-12 21:11:03 -0700 | [diff] [blame] | 195 | if (!usePipeline13) { |
| 196 | // Send packet-in to controller if a packet misses the first table |
| 197 | populateTableMissEntry(0, true, false, false, 0); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 198 | driverHandshakeComplete.set(true); |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 199 | } else { |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 200 | nextDriverState(); |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 201 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public boolean isDriverHandshakeComplete() { |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 206 | if (!startDriverHandshakeCalled) |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 207 | throw new SwitchDriverSubHandshakeNotStarted(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 208 | return driverHandshakeComplete.get(); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void processDriverHandshakeMessage(OFMessage m) { |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 213 | if (!startDriverHandshakeCalled) |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 214 | throw new SwitchDriverSubHandshakeNotStarted(); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 215 | if (isDriverHandshakeComplete()) |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 216 | throw new SwitchDriverSubHandshakeCompleted(m); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 217 | try { |
| 218 | processOFMessage(this, m); |
| 219 | } catch (IOException e) { |
| 220 | log.error("Error generated when processing OFMessage", e.getCause()); |
| 221 | } |
| 222 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 223 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 224 | @Override |
| 225 | public String getSwitchDriverState() { |
| 226 | return driverState.toString(); |
| 227 | } |
| 228 | |
Srikanth Vavilapalli | bd9b3e2 | 2014-10-02 08:57:46 -0700 | [diff] [blame] | 229 | public void removePortFromGroups(PortNumber port) { |
Srikanth Vavilapalli | cf1840b | 2014-10-09 15:29:51 -0700 | [diff] [blame] | 230 | /* FIX: removePortFromGroups is not working */ |
| 231 | |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 232 | 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 Vavilapalli | cf1840b | 2014-10-09 15:29:51 -0700 | [diff] [blame] | 239 | getStringId(), port); |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 240 | 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 Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 250 | log.debug("removePortFromGroups: Group {} on Switch {} has {} buckets", |
| 251 | portEcmpInfo.groupId, getStringId(), |
| 252 | portEcmpInfo.buckets.size()); |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 253 | while (it.hasNext()) { |
| 254 | BucketInfo bucket = it.next(); |
| 255 | if (bucket.outport.equals(port)) { |
| 256 | it.remove(); |
Srikanth Vavilapalli | bd9b3e2 | 2014-10-02 08:57:46 -0700 | [diff] [blame] | 257 | } |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 258 | } |
| 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 Vavilapalli | cf1840b | 2014-10-09 15:29:51 -0700 | [diff] [blame] | 265 | * when the port is up again this info is needed |
| 266 | */ |
Srikanth Vavilapalli | 7d873fd | 2014-10-04 07:50:57 -0700 | [diff] [blame] | 267 | return; |
Srikanth Vavilapalli | bd9b3e2 | 2014-10-02 08:57:46 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | public void addPortToGroups(PortNumber port) { |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 271 | 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 Vavilapalli | cf1840b | 2014-10-09 15:29:51 -0700 | [diff] [blame] | 277 | getStringId(), port); |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 278 | 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 Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 286 | EcmpInfo portEcmpInfo = ecmpGroups.get(ns); |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 287 | /* 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 Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 295 | List<BucketInfo> buckets = portEcmpInfo.buckets; |
| 296 | if (buckets == null) { |
| 297 | buckets = new ArrayList<BucketInfo>(); |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 298 | portEcmpInfo.buckets = buckets; |
| 299 | } else { |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 300 | 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 Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 315 | } |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 316 | BucketInfo b = new BucketInfo(neighborDpid, |
| 317 | MacAddress.of(srConfig.getRouterMac()), |
| 318 | getNeighborRouterMacAddress(neighborDpid), |
| 319 | port, |
| 320 | ns.getEdgeLabel()); |
| 321 | buckets.add(b); |
Srikanth Vavilapalli | bb47e78 | 2014-10-09 18:16:35 -0700 | [diff] [blame] | 322 | log.debug("addPortToGroups: Modifying Group on Switch {} " |
| 323 | + "and Neighborset {} with {}", |
| 324 | getStringId(), ns, portEcmpInfo); |
| 325 | modifyEcmpGroup(portEcmpInfo); |
| 326 | } |
| 327 | return; |
Srikanth Vavilapalli | bd9b3e2 | 2014-10-02 08:57:46 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Srikanth Vavilapalli | 25fbe39 | 2014-10-10 15:04:51 -0700 | [diff] [blame] | 330 | @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 Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 352 | // ***************************** |
| 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 Das | 80d1739 | 2014-10-01 10:24:56 -0700 | [diff] [blame] | 368 | if (haltStateMachine.get()) { |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 369 | return; |
Saurav Das | 80d1739 | 2014-10-01 10:24:56 -0700 | [diff] [blame] | 370 | } |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 371 | switch (currentState) { |
| 372 | case INIT: |
| 373 | driverState = DriverState.SET_TABLE_MISS_ENTRIES; |
| 374 | setTableMissEntries(); |
Saurav Das | d84178f | 2014-09-29 17:48:54 -0700 | [diff] [blame] | 375 | sendHandshakeBarrier(); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 376 | break; |
| 377 | case SET_TABLE_MISS_ENTRIES: |
| 378 | driverState = DriverState.SET_TABLE_VLAN_TMAC; |
| 379 | getNetworkConfig(); |
| 380 | populateTableVlan(); |
| 381 | populateTableTMac(); |
Saurav Das | d84178f | 2014-09-29 17:48:54 -0700 | [diff] [blame] | 382 | sendHandshakeBarrier(); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 383 | break; |
| 384 | case SET_TABLE_VLAN_TMAC: |
| 385 | driverState = DriverState.SET_GROUPS; |
| 386 | createGroups(); |
Saurav Das | d84178f | 2014-09-29 17:48:54 -0700 | [diff] [blame] | 387 | sendHandshakeBarrier(); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 388 | 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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 409 | switch (m.getType()) { |
| 410 | case BARRIER_REPLY: |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 411 | processBarrierReply(m); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 412 | break; |
| 413 | |
| 414 | case ERROR: |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 415 | processErrorMessage(m); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 416 | break; |
| 417 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 418 | case GET_ASYNC_REPLY: |
| 419 | OFAsyncGetReply asrep = (OFAsyncGetReply) m; |
| 420 | decodeAsyncGetReply(asrep); |
| 421 | break; |
| 422 | |
| 423 | case PACKET_IN: |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 424 | // not ready to handle packet-ins |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 425 | break; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 426 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 427 | case QUEUE_GET_CONFIG_REPLY: |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 428 | // not doing queue config yet |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 429 | break; |
| 430 | |
| 431 | case STATS_REPLY: |
| 432 | processStatsReply((OFStatsReply) m); |
| 433 | break; |
| 434 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 435 | 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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 439 | default: |
| 440 | log.debug("Received message {} during switch-driver subhandshake " |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 441 | + "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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 471 | |
| 472 | } |
| 473 | } |
| 474 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 475 | 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 Das | 2d41a43 | 2014-10-21 20:40:10 -0700 | [diff] [blame] | 496 | // TODO -- actually do verification |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 497 | 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 Das | d84178f | 2014-09-29 17:48:54 -0700 | [diff] [blame] | 520 | private void sendHandshakeBarrier() throws IOException { |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 521 | 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 Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 615 | isEdgeRouter = srConfig.isEdgeRouter(); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 616 | } else { |
| 617 | log.error("Switch not configured as Segment-Router"); |
| 618 | } |
| 619 | |
| 620 | List<LinkConfig> linkConfigList = ncs.getConfiguredAllowedLinks(); |
| 621 | setNeighbors(linkConfigList); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 622 | |
| 623 | if (isEdgeRouter) { |
| 624 | List<SwitchConfig> switchList = ncs.getConfiguredAllowedSwitches(); |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 625 | getAllNodeSegmentIds(switchList); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 626 | } |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 627 | } |
| 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 Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 737 | 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 Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 748 | 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 Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 761 | for (LinkConfig lg : linkConfigList) { |
| 762 | if (!lg.getType().equals(NetworkConfigManager.PKT_LINK)) { |
Saurav Das | 80d1739 | 2014-10-01 10:24:56 -0700 | [diff] [blame] | 763 | continue; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 764 | } |
| 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 Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 777 | /* Update NeighborToPort database */ |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 778 | 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 Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 785 | |
| 786 | /* Update portToNeighbors database */ |
| 787 | if (portToNeighbors.get(portToNeighbor) == null) |
| 788 | portToNeighbors.put(portToNeighbor, neighborDpid); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 791 | private void getAllNodeSegmentIds(List<SwitchConfig> switchList) { |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 792 | for (SwitchConfig sc : switchList) { |
| 793 | /* TODO: Do we need to check if the SwitchConfig is of |
| 794 | * type SegmentRouter? |
| 795 | */ |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 796 | if (sc.getDpid() == getId()) { |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 797 | continue; |
| 798 | } |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 799 | segmentIds.add(((SegmentRouterConfig) sc).getNodeSid()); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 800 | } |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 801 | log.debug("getAllNodeSegmentIds: at sw {} are {}", |
| 802 | getStringId(), segmentIds); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 805 | private boolean isSegmentIdSameAsNodeSegmentId(Dpid dpid, int sId) { |
Srikanth Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 806 | INetworkConfigService ncs = floodlightProvider.getNetworkConfigService(); |
| 807 | SwitchConfigStatus scs = ncs.checkSwitchConfig(dpid); |
| 808 | if (scs.getConfigState() == NetworkConfigState.ACCEPT_ADD) { |
| 809 | return (((SegmentRouterConfig) scs.getSwitchConfig()). |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 810 | getNodeSid() == sId); |
Srikanth Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 811 | } else { |
| 812 | // TODO: return false if router not allowed |
| 813 | return false; |
| 814 | } |
| 815 | } |
| 816 | |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 817 | 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 Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 830 | for (int j = 0; j < elements; j++) { |
| 831 | if ((i >> j) % 2 == 1) { |
| 832 | dpidSubSet.add(list.get(j)); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 833 | } |
| 834 | } |
Srikanth Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 835 | /* NOTE: Avoid any pairings of edge routers only |
| 836 | * at a backbone router */ |
| 837 | boolean avoidEdgeRouterPairing = true; |
| 838 | if ((!isEdgeRouter) && (dpidSubSet.size() > 1)) { |
Srikanth Vavilapalli | 696010f | 2014-10-08 17:00:17 -0700 | [diff] [blame] | 839 | for (Dpid dpid : dpidSubSet) { |
| 840 | if (!isEdgeRouter(dpid)) { |
Srikanth Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 841 | avoidEdgeRouterPairing = false; |
Srikanth Vavilapalli | 696010f | 2014-10-08 17:00:17 -0700 | [diff] [blame] | 842 | break; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | else |
Srikanth Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 847 | avoidEdgeRouterPairing = false; |
Srikanth Vavilapalli | 696010f | 2014-10-08 17:00:17 -0700 | [diff] [blame] | 848 | |
Srikanth Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 849 | if (!avoidEdgeRouterPairing) |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 850 | sets.add(dpidSubSet); |
| 851 | } |
| 852 | return sets; |
| 853 | } |
| 854 | |
Srikanth Vavilapalli | 25fbe39 | 2014-10-10 15:04:51 -0700 | [diff] [blame] | 855 | 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, |
| 862 | ns.getEdgeLabel()); |
| 863 | 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 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 887 | private void createGroupForMplsLabel(int groupId, String nodeId, |
| 888 | int nextGroupId, boolean bos) { |
| 889 | List<BucketInfo> buckets = new ArrayList<BucketInfo>(); |
| 890 | BucketInfo bucket = new BucketInfo(nextGroupId, |
| 891 | Integer.parseInt(nodeId), bos); |
| 892 | buckets.add(bucket); |
| 893 | EcmpInfo ecmpInfo = new EcmpInfo(groupId, buckets); |
| 894 | setPolicyEcmpGroup(ecmpInfo); |
| 895 | // ecmpGroups.put(ns, ecmpInfo); |
| 896 | log.debug( |
| 897 | "createGroupForANeighborSet: Creating ecmp group {} in sw {} " |
| 898 | + "for pushing label {} and group to {}", |
| 899 | groupId, getStringId(), nodeId, nextGroupId); |
| 900 | return; |
| 901 | } |
| 902 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 903 | /** |
| 904 | * createGroups creates ECMP groups for all ports on this router connected |
| 905 | * to other routers (in the OF network). The information for ports is |
| 906 | * gleaned from the configured links. If no links are configured no groups |
| 907 | * will be created, and it is up to the caller of the IOF13Switch API to |
| 908 | * create groups. |
| 909 | * <p> |
| 910 | * By default all ports connected to the same neighbor router will be part |
| 911 | * of the same ECMP group. In addition, groups will be created for all |
| 912 | * possible combinations of neighbor routers. |
| 913 | * <p> |
| 914 | * For example, consider this router (R0) connected to 3 neighbors (R1, R2, |
| 915 | * and R3). The following groups will be created in R0: |
| 916 | * <li>1) all ports to R1, |
| 917 | * <li>2) all ports to R2, |
| 918 | * <li>3) all ports to R3, |
| 919 | * <li>4) all ports to R1 and R2 |
| 920 | * <li>5) all ports to R1 and R3 |
| 921 | * <li>6) all ports to R2 and R3 |
| 922 | * <li>7) all ports to R1, R2, and R3 |
| 923 | */ |
| 924 | private void createGroups() { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 925 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 926 | Set<Dpid> dpids = neighbors.keySet(); |
| 927 | if (dpids == null || dpids.isEmpty()) { |
| 928 | return; |
| 929 | } |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 930 | /* Create all possible Neighbor sets from this router |
| 931 | * NOTE: Avoid any pairings of edge routers only |
| 932 | */ |
| 933 | Set<Set<Dpid>> powerSet = getAllNeighborSets(dpids); |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 934 | log.debug("createGroups: The size of neighbor powerset for sw {} is {}", |
Srikanth Vavilapalli | ca2263c | 2014-10-09 15:19:11 -0700 | [diff] [blame] | 935 | getStringId(), powerSet.size()); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 936 | Set<NeighborSet> nsSet = new HashSet<NeighborSet>(); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 937 | for (Set<Dpid> combo : powerSet) { |
Srikanth Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 938 | if (combo.isEmpty()) |
| 939 | continue; |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 940 | if (isEdgeRouter && !segmentIds.isEmpty()) { |
| 941 | for (Integer sId : segmentIds) { |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 942 | NeighborSet ns = new NeighborSet(); |
| 943 | ns.addDpids(combo); |
Srikanth Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 944 | /* Check if the edge label being set is of the |
| 945 | * same node in the Neighbor set |
| 946 | */ |
| 947 | if ((combo.size() != 1) || |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 948 | (!isSegmentIdSameAsNodeSegmentId( |
| 949 | combo.iterator().next(), sId))) { |
| 950 | ns.setEdgeLabel(sId); |
Srikanth Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 951 | } |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 952 | nsSet.add(ns); |
| 953 | } |
| 954 | } else { |
| 955 | NeighborSet ns = new NeighborSet(); |
| 956 | ns.addDpids(combo); |
| 957 | nsSet.add(ns); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 958 | } |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 959 | } |
Srikanth Vavilapalli | 2cd0ff1 | 2014-10-10 13:07:46 -0700 | [diff] [blame] | 960 | log.debug("createGroups: The neighborset with label for sw {} is {}", |
Srikanth Vavilapalli | ca2263c | 2014-10-09 15:19:11 -0700 | [diff] [blame] | 961 | getStringId(), nsSet); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 962 | |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 963 | for (NeighborSet ns : nsSet) { |
Srikanth Vavilapalli | 25fbe39 | 2014-10-10 15:04:51 -0700 | [diff] [blame] | 964 | createGroupForANeighborSet(ns, groupid.incrementAndGet()); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | |
| 968 | private class EcmpInfo { |
| 969 | int groupId; |
| 970 | List<BucketInfo> buckets; |
| 971 | |
| 972 | EcmpInfo(int gid, List<BucketInfo> bucketInfos) { |
| 973 | groupId = gid; |
| 974 | buckets = bucketInfos; |
| 975 | } |
| 976 | |
| 977 | @Override |
| 978 | public String toString() { |
| 979 | return "groupId: " + groupId + ", buckets: " + buckets; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | private class BucketInfo { |
| 984 | Dpid neighborDpid; |
| 985 | MacAddress srcMac; |
| 986 | MacAddress dstMac; |
| 987 | PortNumber outport; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 988 | int groupNo; |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 989 | int mplsLabel; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 990 | boolean bos; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 991 | |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 992 | BucketInfo(Dpid nDpid, MacAddress smac, MacAddress dmac, |
| 993 | PortNumber p, int label) { |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 994 | neighborDpid = nDpid; |
| 995 | srcMac = smac; |
| 996 | dstMac = dmac; |
| 997 | outport = p; |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 998 | mplsLabel = label; |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 999 | groupNo = -1; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1002 | BucketInfo(int no, int label, boolean b) { |
| 1003 | neighborDpid = null; |
| 1004 | srcMac = null; |
| 1005 | dstMac = null; |
| 1006 | outport = null; |
| 1007 | groupNo = no; |
| 1008 | mplsLabel = label; |
| 1009 | bos = b; |
| 1010 | } |
| 1011 | |
| 1012 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1013 | @Override |
| 1014 | public String toString() { |
| 1015 | return " {neighborDpid: " + neighborDpid + ", dstMac: " + dstMac + |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 1016 | ", srcMac: " + srcMac + ", outport: " + outport + |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1017 | ", groupNo: " + groupNo + |
| 1018 | ", mplsLabel: " + mplsLabel + "}"; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | private void setEcmpGroup(EcmpInfo ecmpInfo) { |
| 1023 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 1024 | OFGroup group = OFGroup.of(ecmpInfo.groupId); |
| 1025 | |
| 1026 | List<OFBucket> buckets = new ArrayList<OFBucket>(); |
| 1027 | for (BucketInfo b : ecmpInfo.buckets) { |
| 1028 | OFOxmEthDst dmac = factory.oxms() |
| 1029 | .ethDst(b.dstMac); |
| 1030 | OFAction setDA = factory.actions().buildSetField() |
| 1031 | .setField(dmac).build(); |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1032 | OFOxmEthSrc smac = factory.oxms() |
| 1033 | .ethSrc(b.srcMac); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1034 | OFAction setSA = factory.actions().buildSetField() |
| 1035 | .setField(smac).build(); |
| 1036 | OFAction outp = factory.actions().buildOutput() |
| 1037 | .setPort(OFPort.of(b.outport.shortValue())) |
| 1038 | .build(); |
| 1039 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 1040 | actions.add(setSA); |
| 1041 | actions.add(setDA); |
| 1042 | actions.add(outp); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 1043 | if (b.mplsLabel != -1) { |
| 1044 | OFAction pushLabel = factory.actions().buildPushMpls() |
| 1045 | .setEthertype(EthType.MPLS_UNICAST).build(); |
Srikanth Vavilapalli | 11dc607 | 2014-10-13 13:32:11 -0700 | [diff] [blame] | 1046 | OFOxmMplsBos bosX = factory.oxms() |
| 1047 | .mplsBos(OFBooleanValue.TRUE); |
| 1048 | OFAction setBX = factory.actions().buildSetField() |
| 1049 | .setField(bosX).build(); |
Sangho Shin | 62ce5c1 | 2014-10-08 16:24:40 -0700 | [diff] [blame] | 1050 | OFOxmMplsLabel lid = factory.oxms() |
| 1051 | .mplsLabel(U32.of(b.mplsLabel)); |
| 1052 | OFAction setLabel = factory.actions().buildSetField() |
| 1053 | .setField(lid).build(); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 1054 | OFAction copyTtl = factory.actions().copyTtlOut(); |
| 1055 | OFAction decrTtl = factory.actions().decMplsTtl(); |
| 1056 | actions.add(pushLabel); |
| 1057 | actions.add(setLabel); |
Srikanth Vavilapalli | 11dc607 | 2014-10-13 13:32:11 -0700 | [diff] [blame] | 1058 | actions.add(setBX); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 1059 | actions.add(copyTtl); |
| 1060 | actions.add(decrTtl); |
| 1061 | } |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1062 | OFBucket ofb = factory.buildBucket() |
| 1063 | .setWeight(1) |
| 1064 | .setActions(actions) |
| 1065 | .build(); |
| 1066 | buckets.add(ofb); |
| 1067 | } |
| 1068 | |
| 1069 | OFMessage gm = factory.buildGroupAdd() |
| 1070 | .setGroup(group) |
| 1071 | .setBuckets(buckets) |
| 1072 | .setGroupType(OFGroupType.SELECT) |
| 1073 | .setXid(getNextTransactionId()) |
| 1074 | .build(); |
| 1075 | msglist.add(gm); |
| 1076 | try { |
| 1077 | write(msglist); |
| 1078 | } catch (IOException e) { |
| 1079 | // TODO Auto-generated catch block |
| 1080 | e.printStackTrace(); |
| 1081 | } |
| 1082 | } |
| 1083 | |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1084 | private void setPolicyEcmpGroup(EcmpInfo ecmpInfo) { |
| 1085 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 1086 | OFGroup group = OFGroup.of(ecmpInfo.groupId); |
| 1087 | |
| 1088 | List<OFBucket> buckets = new ArrayList<OFBucket>(); |
| 1089 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 1090 | for (BucketInfo b : ecmpInfo.buckets) { |
| 1091 | if (b.dstMac != null && b.srcMac != null && b.outport != null) { |
| 1092 | OFOxmEthDst dmac = factory.oxms() |
| 1093 | .ethDst(b.dstMac); |
| 1094 | OFAction setDA = factory.actions().buildSetField() |
| 1095 | .setField(dmac).build(); |
| 1096 | OFOxmEthSrc smac = factory.oxms() |
| 1097 | .ethSrc(b.srcMac); |
| 1098 | OFAction setSA = factory.actions().buildSetField() |
| 1099 | .setField(smac).build(); |
| 1100 | OFAction outp = factory.actions().buildOutput() |
| 1101 | .setPort(OFPort.of(b.outport.shortValue())) |
| 1102 | .build(); |
| 1103 | actions.add(setSA); |
| 1104 | actions.add(setDA); |
| 1105 | actions.add(outp); |
| 1106 | } |
| 1107 | if (b.groupNo > 0) { |
| 1108 | OFAction groupTo = factory.actions().buildGroup() |
| 1109 | .setGroup(OFGroup.of(b.groupNo)) |
| 1110 | .build(); |
| 1111 | actions.add(groupTo); |
| 1112 | } |
| 1113 | if (b.mplsLabel != -1) { |
| 1114 | OFAction pushLabel = factory.actions().buildPushMpls() |
| 1115 | .setEthertype(EthType.MPLS_UNICAST).build(); |
| 1116 | |
| 1117 | OFBooleanValue bosValue = null; |
| 1118 | if (b.bos) |
| 1119 | bosValue = OFBooleanValue.TRUE; |
| 1120 | else |
| 1121 | bosValue = OFBooleanValue.FALSE; |
| 1122 | OFOxmMplsBos bosX = factory.oxms() |
| 1123 | .mplsBos(bosValue); |
| 1124 | OFAction setBX = factory.actions().buildSetField() |
| 1125 | .setField(bosX).build(); |
| 1126 | OFOxmMplsLabel lid = factory.oxms() |
| 1127 | .mplsLabel(U32.of(b.mplsLabel)); |
| 1128 | OFAction setLabel = factory.actions().buildSetField() |
| 1129 | .setField(lid).build(); |
| 1130 | OFAction copyTtl = factory.actions().copyTtlOut(); |
| 1131 | OFAction decrTtl = factory.actions().decMplsTtl(); |
| 1132 | actions.add(pushLabel); |
| 1133 | actions.add(setLabel); |
| 1134 | actions.add(setBX); |
| 1135 | actions.add(copyTtl); |
| 1136 | // decrement TTL only when the first MPLS label is pushed |
| 1137 | if (b.bos) |
| 1138 | actions.add(decrTtl); |
| 1139 | } |
| 1140 | OFBucket ofb = factory.buildBucket() |
| 1141 | .setWeight(1) |
| 1142 | .setActions(actions) |
| 1143 | .build(); |
| 1144 | buckets.add(ofb); |
| 1145 | } |
| 1146 | |
| 1147 | OFMessage gm = factory.buildGroupAdd() |
| 1148 | .setGroup(group) |
| 1149 | .setBuckets(buckets) |
| 1150 | .setGroupType(OFGroupType.SELECT) |
| 1151 | .setXid(getNextTransactionId()) |
| 1152 | .build(); |
| 1153 | msglist.add(gm); |
| 1154 | try { |
| 1155 | write(msglist); |
| 1156 | } catch (IOException e) { |
| 1157 | // TODO Auto-generated catch block |
| 1158 | e.printStackTrace(); |
| 1159 | } |
| 1160 | } |
| 1161 | |
Sangho Shin | 55d00e1 | 2014-10-20 12:13:07 -0700 | [diff] [blame] | 1162 | private void deleteGroup(int groupId) { |
| 1163 | |
| 1164 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 1165 | OFGroup group = OFGroup.of(groupId); |
| 1166 | |
| 1167 | OFMessage gm = factory.buildGroupDelete() |
| 1168 | .setGroup(group) |
| 1169 | .setGroupType(OFGroupType.SELECT) |
| 1170 | .setXid(getNextTransactionId()) |
| 1171 | .build(); |
| 1172 | msglist.add(gm); |
| 1173 | try { |
| 1174 | write(msglist); |
| 1175 | } catch (IOException e) { |
| 1176 | // TODO Auto-generated catch block |
| 1177 | e.printStackTrace(); |
| 1178 | } |
| 1179 | } |
| 1180 | |
Srikanth Vavilapalli | 7d873fd | 2014-10-04 07:50:57 -0700 | [diff] [blame] | 1181 | private void modifyEcmpGroup(EcmpInfo ecmpInfo) { |
| 1182 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 1183 | OFGroup group = OFGroup.of(ecmpInfo.groupId); |
| 1184 | |
| 1185 | List<OFBucket> buckets = new ArrayList<OFBucket>(); |
| 1186 | for (BucketInfo b : ecmpInfo.buckets) { |
| 1187 | OFOxmEthDst dmac = factory.oxms() |
| 1188 | .ethDst(b.dstMac); |
| 1189 | OFAction setDA = factory.actions().buildSetField() |
| 1190 | .setField(dmac).build(); |
| 1191 | OFOxmEthSrc smac = factory.oxms() |
| 1192 | .ethSrc(b.srcMac); |
| 1193 | OFAction setSA = factory.actions().buildSetField() |
| 1194 | .setField(smac).build(); |
| 1195 | OFAction outp = factory.actions().buildOutput() |
| 1196 | .setPort(OFPort.of(b.outport.shortValue())) |
| 1197 | .build(); |
| 1198 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 1199 | actions.add(setSA); |
| 1200 | actions.add(setDA); |
| 1201 | actions.add(outp); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 1202 | if (b.mplsLabel != -1) { |
| 1203 | OFAction pushLabel = factory.actions().buildPushMpls() |
| 1204 | .setEthertype(EthType.MPLS_UNICAST).build(); |
Srikanth Vavilapalli | 11dc607 | 2014-10-13 13:32:11 -0700 | [diff] [blame] | 1205 | OFOxmMplsBos bosX = factory.oxms() |
| 1206 | .mplsBos(OFBooleanValue.TRUE); |
| 1207 | OFAction setBX = factory.actions().buildSetField() |
| 1208 | .setField(bosX).build(); |
Srikanth Vavilapalli | 4ae146e | 2014-10-09 14:35:26 -0700 | [diff] [blame] | 1209 | OFOxmMplsLabel lid = factory.oxms() |
| 1210 | .mplsLabel(U32.of(b.mplsLabel)); |
| 1211 | OFAction setLabel = factory.actions().buildSetField() |
| 1212 | .setField(lid).build(); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 1213 | OFAction copyTtl = factory.actions().copyTtlOut(); |
| 1214 | OFAction decrTtl = factory.actions().decMplsTtl(); |
| 1215 | actions.add(pushLabel); |
| 1216 | actions.add(setLabel); |
Srikanth Vavilapalli | 11dc607 | 2014-10-13 13:32:11 -0700 | [diff] [blame] | 1217 | actions.add(setBX); |
Srikanth Vavilapalli | 6814430 | 2014-10-08 15:55:24 -0700 | [diff] [blame] | 1218 | actions.add(copyTtl); |
| 1219 | actions.add(decrTtl); |
| 1220 | } |
Srikanth Vavilapalli | 7d873fd | 2014-10-04 07:50:57 -0700 | [diff] [blame] | 1221 | OFBucket ofb = factory.buildBucket() |
| 1222 | .setWeight(1) |
| 1223 | .setActions(actions) |
| 1224 | .build(); |
| 1225 | buckets.add(ofb); |
| 1226 | } |
| 1227 | |
| 1228 | OFMessage gm = factory.buildGroupModify() |
| 1229 | .setGroup(group) |
| 1230 | .setBuckets(buckets) |
| 1231 | .setGroupType(OFGroupType.SELECT) |
| 1232 | .setXid(getNextTransactionId()) |
| 1233 | .build(); |
| 1234 | msglist.add(gm); |
| 1235 | try { |
| 1236 | write(msglist); |
| 1237 | } catch (IOException e) { |
| 1238 | // TODO Auto-generated catch block |
| 1239 | e.printStackTrace(); |
| 1240 | } |
| 1241 | } |
| 1242 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1243 | private void verifyGroups() throws IOException { |
| 1244 | sendGroupDescRequest(); |
| 1245 | } |
| 1246 | |
| 1247 | private void sendGroupDescRequest() throws IOException { |
| 1248 | OFMessage gdr = factory.buildGroupDescStatsRequest() |
| 1249 | .setXid(getNextTransactionId()) |
| 1250 | .build(); |
| 1251 | write(gdr, null); |
| 1252 | } |
| 1253 | |
| 1254 | private void assignAdjacencyLabels() { |
Saurav Das | 2d41a43 | 2014-10-21 20:40:10 -0700 | [diff] [blame] | 1255 | List<AdjacencySid> autogenAdjSids = new ArrayList<AdjacencySid>(); |
| 1256 | publishAttributes = new HashMap<String, String>(); |
| 1257 | for (OFPortDesc p : getPorts()) { |
| 1258 | int pnum = p.getPortNo().getPortNumber(); |
| 1259 | |
| 1260 | if (U32.ofRaw(pnum).compareTo(U32.ofRaw(OFPort.MAX.getPortNumber())) >= 1) { |
| 1261 | continue; |
| 1262 | } |
| 1263 | // create unique adj-sid assuming that operator only |
| 1264 | // enters adjSids for multiple-ports and only in the range |
| 1265 | // 1-10k XXX make sure that happens |
| 1266 | int adjSid = srConfig.getNodeSid() * 1000 + pnum; |
| 1267 | AdjacencySid as = new AdjacencySid(adjSid, |
| 1268 | Collections.singletonList(pnum)); |
| 1269 | autogenAdjSids.add(as); |
| 1270 | } |
| 1271 | ObjectMapper mapper = new ObjectMapper(); |
| 1272 | try { |
| 1273 | publishAttributes.put("autogenAdjSids", |
| 1274 | mapper.writeValueAsString(autogenAdjSids)); |
| 1275 | } catch (IOException e1) { |
| 1276 | log.error("Error while writing adjacency labels: {}", e1.getCause()); |
| 1277 | } |
| 1278 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1279 | try { |
| 1280 | nextDriverState(); |
| 1281 | } catch (IOException e) { |
| 1282 | // TODO Auto-generated catch block |
| 1283 | e.printStackTrace(); |
| 1284 | } |
| 1285 | } |
| 1286 | |
Saurav Das | 1cd1015 | 2014-09-26 09:38:07 -0700 | [diff] [blame] | 1287 | private OFAction getOFAction(Action action) { |
| 1288 | OFAction ofAction = null; |
| 1289 | if (action instanceof OutputAction) { |
| 1290 | OutputAction outputAction = (OutputAction) action; |
| 1291 | OFPort port = OFPort.of((int) outputAction.getPortNumber().value()); |
| 1292 | ofAction = factory.actions().output(port, Short.MAX_VALUE); |
| 1293 | } else if (action instanceof ModifyDstMacAction) { |
| 1294 | long dstMac = ((ModifyDstMacAction) action).getDstMac().toLong(); |
| 1295 | OFOxmEthDst dmac = factory.oxms() |
| 1296 | .ethDst(MacAddress.of(dstMac)); |
| 1297 | ofAction = factory.actions().buildSetField() |
| 1298 | .setField(dmac).build(); |
| 1299 | } else if (action instanceof ModifySrcMacAction) { |
| 1300 | long srcMac = ((ModifySrcMacAction) action).getSrcMac().toLong(); |
| 1301 | OFOxmEthSrc smac = factory.oxms() |
| 1302 | .ethSrc(MacAddress.of(srcMac)); |
| 1303 | ofAction = factory.actions().buildSetField() |
| 1304 | .setField(smac).build(); |
Saurav Das | a962a69 | 2014-10-17 14:52:38 -0700 | [diff] [blame] | 1305 | } else if (action instanceof PushMplsAction) { |
| 1306 | ofAction = factory.actions().pushMpls(EthType.MPLS_UNICAST); |
| 1307 | } else if (action instanceof SetMplsIdAction) { |
| 1308 | int labelid = ((SetMplsIdAction) action).getMplsId(); |
| 1309 | OFOxmMplsLabel lid = factory.oxms() |
| 1310 | .mplsLabel(U32.of(labelid)); |
| 1311 | ofAction = factory.actions().buildSetField() |
| 1312 | .setField(lid).build(); |
| 1313 | } else if (action instanceof SetMplsBosAction) { |
| 1314 | OFBooleanValue val = OFBooleanValue.of( |
| 1315 | ((SetMplsBosAction) action).isSet()); |
| 1316 | OFOxmMplsBos bos = factory.oxms().mplsBos(val); |
| 1317 | OFAction setBos = factory.actions().buildSetField() |
| 1318 | .setField(bos).build(); |
| 1319 | } else if (action instanceof PopMplsAction) { |
Saurav Das | 1cd1015 | 2014-09-26 09:38:07 -0700 | [diff] [blame] | 1320 | EthType ethertype = ((PopMplsAction) action).getEthType(); |
| 1321 | ofAction = factory.actions().popMpls(ethertype); |
| 1322 | } else if (action instanceof GroupAction) { |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1323 | int gid = -1; |
| 1324 | GroupAction ga = (GroupAction)action; |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1325 | if (ga.getTunnelId() != null) { |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1326 | List<Integer> groupIds = tunnelGroupIdTable.get(ga.getTunnelId()); |
Sangho Shin | 8165544 | 2014-10-20 14:22:46 -0700 | [diff] [blame] | 1327 | gid = groupIds.get(groupIds.size()-1); |
Saurav Das | 1cd1015 | 2014-09-26 09:38:07 -0700 | [diff] [blame] | 1328 | } |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1329 | else { |
| 1330 | NeighborSet ns = ((GroupAction) action).getDpids(); |
| 1331 | EcmpInfo ei = ecmpGroups.get(ns); |
| 1332 | if (ei == null) { |
| 1333 | log.debug("Unable to find ecmp group for neighbors {} at " |
| 1334 | + "switch {} and hence creating it", ns, getStringId()); |
| 1335 | createGroupForANeighborSet(ns, groupid.incrementAndGet()); |
| 1336 | ei = ecmpGroups.get(ns); |
| 1337 | } |
| 1338 | gid = ei.groupId; |
| 1339 | } |
Srikanth Vavilapalli | 846df29 | 2014-10-10 15:47:16 -0700 | [diff] [blame] | 1340 | ofAction = factory.actions().buildGroup() |
| 1341 | .setGroup(OFGroup.of(gid)) |
| 1342 | .build(); |
Saurav Das | 1cd1015 | 2014-09-26 09:38:07 -0700 | [diff] [blame] | 1343 | } else if (action instanceof DecNwTtlAction) { |
| 1344 | ofAction = factory.actions().decNwTtl(); |
| 1345 | } else if (action instanceof DecMplsTtlAction) { |
| 1346 | ofAction = factory.actions().decMplsTtl(); |
| 1347 | } else if (action instanceof CopyTtlInAction) { |
| 1348 | ofAction = factory.actions().copyTtlIn(); |
| 1349 | } else if (action instanceof CopyTtlOutAction) { |
| 1350 | ofAction = factory.actions().copyTtlOut(); |
| 1351 | } else { |
| 1352 | log.warn("Unsupported Action type: {}", action.getClass().getName()); |
| 1353 | return null; |
| 1354 | } |
| 1355 | |
Saurav Das | 1cd1015 | 2014-09-26 09:38:07 -0700 | [diff] [blame] | 1356 | return ofAction; |
| 1357 | } |
| 1358 | |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1359 | private OFMessage getIpEntry(MatchActionOperationEntry mao) { |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1360 | MatchAction ma = mao.getTarget(); |
| 1361 | Operator op = mao.getOperator(); |
| 1362 | Ipv4Match ipm = (Ipv4Match) ma.getMatch(); |
| 1363 | |
| 1364 | // set match |
| 1365 | IPv4Net ipdst = ipm.getDestination(); |
| 1366 | OFOxmEthType ethTypeIp = factory.oxms() |
| 1367 | .ethType(EthType.IPv4); |
| 1368 | OFOxmIpv4DstMasked ipPrefix = factory.oxms() |
| 1369 | .ipv4DstMasked( |
| 1370 | IPv4Address.of(ipdst.address().value()), |
| 1371 | IPv4Address.ofCidrMaskLength(ipdst.prefixLen()) |
| 1372 | ); |
| 1373 | OFOxmList oxmList = OFOxmList.of(ethTypeIp, ipPrefix); |
| 1374 | OFMatchV3 match = factory.buildMatchV3() |
| 1375 | .setOxmList(oxmList).build(); |
| 1376 | |
| 1377 | // set actions |
| 1378 | List<OFAction> writeActions = new ArrayList<OFAction>(); |
| 1379 | for (Action action : ma.getActions()) { |
Saurav Das | 1cd1015 | 2014-09-26 09:38:07 -0700 | [diff] [blame] | 1380 | OFAction ofAction = getOFAction(action); |
| 1381 | if (ofAction != null) { |
| 1382 | writeActions.add(ofAction); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1383 | } |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1386 | // set instructions |
| 1387 | OFInstruction writeInstr = factory.instructions().buildWriteActions() |
| 1388 | .setActions(writeActions).build(); |
| 1389 | OFInstruction gotoInstr = factory.instructions().buildGotoTable() |
| 1390 | .setTableId(TableId.of(TABLE_ACL)).build(); |
| 1391 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 1392 | instructions.add(writeInstr); |
| 1393 | instructions.add(gotoInstr); |
| 1394 | |
Saurav Das | 1cd1015 | 2014-09-26 09:38:07 -0700 | [diff] [blame] | 1395 | // set flow priority to emulate longest prefix match |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1396 | int priority = ipdst.prefixLen() * PRIORITY_MULTIPLIER; |
| 1397 | if (ipdst.prefixLen() == (short) 32) { |
| 1398 | priority = MAX_PRIORITY; |
| 1399 | } |
| 1400 | |
Saurav Das | bc594a4 | 2014-09-25 20:13:50 -0700 | [diff] [blame] | 1401 | // set flow-mod |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1402 | OFFlowMod.Builder fmBuilder = null; |
| 1403 | switch (op) { |
| 1404 | case ADD: |
| 1405 | fmBuilder = factory.buildFlowAdd(); |
| 1406 | break; |
| 1407 | case REMOVE: |
| 1408 | fmBuilder = factory.buildFlowDeleteStrict(); |
| 1409 | break; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 1410 | case MODIFY: // TODO |
| 1411 | fmBuilder = factory.buildFlowModifyStrict(); |
| 1412 | break; |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1413 | default: |
| 1414 | log.warn("Unsupported MatchAction Operator: {}", op); |
| 1415 | return null; |
Saurav Das | bc594a4 | 2014-09-25 20:13:50 -0700 | [diff] [blame] | 1416 | } |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1417 | OFMessage ipFlow = fmBuilder |
| 1418 | .setTableId(TableId.of(TABLE_IPv4_UNICAST)) |
| 1419 | .setMatch(match) |
| 1420 | .setInstructions(instructions) |
| 1421 | .setPriority(priority) |
| 1422 | .setBufferId(OFBufferId.NO_BUFFER) |
| 1423 | .setIdleTimeout(0) |
| 1424 | .setHardTimeout(0) |
| 1425 | .setXid(getNextTransactionId()) |
| 1426 | .build(); |
Saurav Das | bc594a4 | 2014-09-25 20:13:50 -0700 | [diff] [blame] | 1427 | log.debug("{} ip-rule {}-{} in sw {}", |
| 1428 | (op == MatchActionOperations.Operator.ADD) ? "Adding" : "Deleting", |
| 1429 | match, writeActions, |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1430 | getStringId()); |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1431 | return ipFlow; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1434 | private OFMessage getMplsEntry(MatchActionOperationEntry mao) { |
Saurav Das | 1cd1015 | 2014-09-26 09:38:07 -0700 | [diff] [blame] | 1435 | MatchAction ma = mao.getTarget(); |
| 1436 | Operator op = mao.getOperator(); |
| 1437 | MplsMatch mplsm = (MplsMatch) ma.getMatch(); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1438 | |
Saurav Das | 1cd1015 | 2014-09-26 09:38:07 -0700 | [diff] [blame] | 1439 | // set match |
| 1440 | OFOxmEthType ethTypeMpls = factory.oxms() |
| 1441 | .ethType(EthType.MPLS_UNICAST); |
| 1442 | OFOxmMplsLabel labelid = factory.oxms() |
| 1443 | .mplsLabel(U32.of(mplsm.getMplsLabel())); |
Sangho Shin | c1b8dea | 2014-10-13 12:03:28 -0700 | [diff] [blame] | 1444 | OFOxmMplsBos bos = factory.oxms() |
| 1445 | .mplsBos(OFBooleanValue.of(mplsm.isBos())); |
| 1446 | OFOxmList oxmList = OFOxmList.of(ethTypeMpls, labelid, bos); |
Saurav Das | 1cd1015 | 2014-09-26 09:38:07 -0700 | [diff] [blame] | 1447 | OFMatchV3 matchlabel = factory.buildMatchV3() |
| 1448 | .setOxmList(oxmList).build(); |
| 1449 | |
| 1450 | // set actions |
| 1451 | List<OFAction> writeActions = new ArrayList<OFAction>(); |
| 1452 | for (Action action : ma.getActions()) { |
| 1453 | OFAction ofAction = getOFAction(action); |
| 1454 | if (ofAction != null) { |
| 1455 | writeActions.add(ofAction); |
| 1456 | } |
| 1457 | } |
| 1458 | |
| 1459 | // set instructions |
| 1460 | OFInstruction writeInstr = factory.instructions().buildWriteActions() |
| 1461 | .setActions(writeActions).build(); |
| 1462 | OFInstruction gotoInstr = factory.instructions().buildGotoTable() |
| 1463 | .setTableId(TableId.of(TABLE_ACL)).build(); |
| 1464 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 1465 | instructions.add(writeInstr); |
| 1466 | instructions.add(gotoInstr); |
| 1467 | |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1468 | // set flow-mod |
| 1469 | OFFlowMod.Builder fmBuilder = null; |
| 1470 | switch (op) { |
| 1471 | case ADD: |
| 1472 | fmBuilder = factory.buildFlowAdd(); |
| 1473 | break; |
| 1474 | case REMOVE: |
| 1475 | fmBuilder = factory.buildFlowDeleteStrict(); |
| 1476 | break; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 1477 | case MODIFY: // TODO |
| 1478 | fmBuilder = factory.buildFlowModifyStrict(); |
| 1479 | break; |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1480 | default: |
| 1481 | log.warn("Unsupported MatchAction Operator: {}", op); |
| 1482 | return null; |
Saurav Das | 1cd1015 | 2014-09-26 09:38:07 -0700 | [diff] [blame] | 1483 | } |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1484 | |
| 1485 | OFMessage mplsFlow = fmBuilder |
| 1486 | .setTableId(TableId.of(TABLE_MPLS)) |
| 1487 | .setMatch(matchlabel) |
| 1488 | .setInstructions(instructions) |
| 1489 | .setPriority(MAX_PRIORITY) // exact match and exclusive |
| 1490 | .setBufferId(OFBufferId.NO_BUFFER) |
| 1491 | .setIdleTimeout(0) |
| 1492 | .setHardTimeout(0) |
| 1493 | .setXid(getNextTransactionId()) |
| 1494 | .build(); |
Saurav Das | 1cd1015 | 2014-09-26 09:38:07 -0700 | [diff] [blame] | 1495 | log.debug("{} mpls-rule {}-{} in sw {}", |
| 1496 | (op == MatchActionOperations.Operator.ADD) ? "Adding" : "Deleting", |
| 1497 | matchlabel, writeActions, |
| 1498 | getStringId()); |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1499 | return mplsFlow; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1500 | } |
| 1501 | |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1502 | private OFMessage getAclEntry(MatchActionOperationEntry mao) { |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1503 | MatchAction ma = mao.getTarget(); |
| 1504 | Operator op = mao.getOperator(); |
| 1505 | PacketMatch packetMatch = (PacketMatch) ma.getMatch(); |
| 1506 | Builder matchBuilder = factory.buildMatch(); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1507 | |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1508 | // set match |
| 1509 | int inport = 0; |
| 1510 | if (ma.getSwitchPort() != null) { |
| 1511 | inport = (int) ma.getSwitchPort().getPortNumber().value(); |
| 1512 | } |
| 1513 | final MACAddress srcMac = packetMatch.getSrcMacAddress(); |
| 1514 | final MACAddress dstMac = packetMatch.getDstMacAddress(); |
| 1515 | final Short etherType = packetMatch.getEtherType(); |
| 1516 | final IPv4Net srcIp = packetMatch.getSrcIpAddress(); |
| 1517 | final IPv4Net dstIp = packetMatch.getDstIpAddress(); |
| 1518 | final Byte ipProto = packetMatch.getIpProtocolNumber(); |
| 1519 | final Short srcTcpPort = packetMatch.getSrcTcpPortNumber(); |
| 1520 | final Short dstTcpPort = packetMatch.getDstTcpPortNumber(); |
| 1521 | if (inport > 0) { |
| 1522 | matchBuilder.setExact(MatchField.IN_PORT, |
| 1523 | OFPort.of(inport)); |
| 1524 | } |
| 1525 | if (srcMac != null) { |
| 1526 | matchBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(srcMac.toLong())); |
| 1527 | } |
| 1528 | if (dstMac != null) { |
| 1529 | matchBuilder.setExact(MatchField.ETH_DST, MacAddress.of(dstMac.toLong())); |
| 1530 | } |
| 1531 | if (etherType != null) { |
| 1532 | matchBuilder.setExact(MatchField.ETH_TYPE, EthType.of(etherType)); |
| 1533 | } |
| 1534 | if (srcIp != null) { |
| 1535 | matchBuilder.setMasked(MatchField.IPV4_SRC, |
| 1536 | IPv4Address.of(srcIp.address().value()) |
| 1537 | .withMaskOfLength(srcIp.prefixLen())); |
| 1538 | } |
| 1539 | if (dstIp != null) { |
| 1540 | matchBuilder.setMasked(MatchField.IPV4_DST, |
| 1541 | IPv4Address.of(dstIp.address().value()) |
| 1542 | .withMaskOfLength(dstIp.prefixLen())); |
| 1543 | } |
| 1544 | if (ipProto != null) { |
| 1545 | matchBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(ipProto)); |
| 1546 | } |
| 1547 | if (srcTcpPort != null) { |
| 1548 | matchBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(srcTcpPort)); |
| 1549 | } |
| 1550 | if (dstTcpPort != null) { |
| 1551 | matchBuilder.setExact(MatchField.TCP_DST, TransportPort.of(dstTcpPort)); |
| 1552 | } |
| 1553 | |
| 1554 | // set actions |
Saurav Das | cc3e35f | 2014-10-10 15:33:32 -0700 | [diff] [blame] | 1555 | List<OFAction> writeActions = new ArrayList<OFAction>(); |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1556 | for (Action action : ma.getActions()) { |
| 1557 | OFAction ofAction = getOFAction(action); |
| 1558 | if (ofAction != null) { |
Saurav Das | cc3e35f | 2014-10-10 15:33:32 -0700 | [diff] [blame] | 1559 | writeActions.add(ofAction); |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | // set instructions |
| 1564 | OFInstruction clearInstr = factory.instructions().clearActions(); |
Saurav Das | cc3e35f | 2014-10-10 15:33:32 -0700 | [diff] [blame] | 1565 | OFInstruction writeInstr = factory.instructions().buildWriteActions() |
| 1566 | .setActions(writeActions).build(); |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1567 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 1568 | instructions.add(clearInstr); |
Saurav Das | cc3e35f | 2014-10-10 15:33:32 -0700 | [diff] [blame] | 1569 | instructions.add(writeInstr); |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1570 | |
| 1571 | // set flow-mod |
| 1572 | OFFlowMod.Builder fmBuilder = null; |
| 1573 | switch (op) { |
| 1574 | case ADD: |
| 1575 | fmBuilder = factory.buildFlowAdd(); |
| 1576 | break; |
| 1577 | case REMOVE: |
| 1578 | fmBuilder = factory.buildFlowDeleteStrict(); |
| 1579 | break; |
Sangho Shin | 5be3e53 | 2014-10-03 17:20:58 -0700 | [diff] [blame] | 1580 | case MODIFY: // TODO |
| 1581 | fmBuilder = factory.buildFlowModifyStrict(); |
| 1582 | break; |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1583 | default: |
| 1584 | log.warn("Unsupported MatchAction Operator: {}", op); |
| 1585 | return null; |
| 1586 | } |
| 1587 | |
| 1588 | OFMessage aclFlow = fmBuilder |
| 1589 | .setTableId(TableId.of(TABLE_ACL)) |
| 1590 | .setMatch(matchBuilder.build()) |
| 1591 | .setInstructions(instructions) |
Sangho Shin | 6f47dd3 | 2014-10-17 23:10:39 -0700 | [diff] [blame] | 1592 | .setPriority(ma.getPriority()) // exact match and exclusive |
Saurav Das | e972b3a | 2014-09-26 15:41:06 -0700 | [diff] [blame] | 1593 | .setBufferId(OFBufferId.NO_BUFFER) |
| 1594 | .setIdleTimeout(0) |
| 1595 | .setHardTimeout(0) |
| 1596 | .setXid(getNextTransactionId()) |
| 1597 | .build(); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1598 | |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1599 | return aclFlow; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | // ***************************** |
| 1603 | // IOF13Switch |
| 1604 | // ***************************** |
| 1605 | |
| 1606 | @Override |
| 1607 | public void pushFlow(MatchActionOperationEntry matchActionOp) throws IOException { |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1608 | OFMessage ofm = getFlow(matchActionOp); |
| 1609 | if (ofm != null) { |
| 1610 | write(Collections.singletonList(ofm)); |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | private OFMessage getFlow(MatchActionOperationEntry matchActionOp) { |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1615 | final MatchAction matchAction = matchActionOp.getTarget(); |
| 1616 | final Match match = matchAction.getMatch(); |
| 1617 | if (match instanceof Ipv4Match) { |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1618 | return getIpEntry(matchActionOp); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1619 | } else if (match instanceof MplsMatch) { |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1620 | return getMplsEntry(matchActionOp); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1621 | } else if (match instanceof PacketMatch) { |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1622 | return getAclEntry(matchActionOp); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1623 | } else { |
| 1624 | log.error("Unknown match type {} pushed to switch {}", match, |
| 1625 | getStringId()); |
| 1626 | } |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1627 | return null; |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | @Override |
| 1631 | public void pushFlows(Collection<MatchActionOperationEntry> matchActionOps) |
| 1632 | throws IOException { |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1633 | List<OFMessage> flowMods = new ArrayList<OFMessage>(); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1634 | for (MatchActionOperationEntry matchActionOp : matchActionOps) { |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1635 | OFMessage ofm = getFlow(matchActionOp); |
| 1636 | if (ofm != null) { |
| 1637 | flowMods.add(ofm); |
| 1638 | } |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1639 | } |
Saurav Das | 0a344b0 | 2014-09-26 14:18:52 -0700 | [diff] [blame] | 1640 | write(flowMods); |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | @Override |
| 1644 | public int getEcmpGroupId(NeighborSet ns) { |
| 1645 | EcmpInfo ei = ecmpGroups.get(ns); |
| 1646 | if (ei == null) { |
| 1647 | return -1; |
| 1648 | } else { |
| 1649 | return ei.groupId; |
| 1650 | } |
| 1651 | } |
| 1652 | |
Saurav Das | cc3e35f | 2014-10-10 15:33:32 -0700 | [diff] [blame] | 1653 | @Override |
| 1654 | public TableId getTableId(String tableType) { |
| 1655 | tableType = tableType.toLowerCase(); |
| 1656 | if (tableType.contentEquals("ip")) { |
| 1657 | return TableId.of(OFSwitchImplCPqD13.TABLE_IPv4_UNICAST); |
| 1658 | } |
| 1659 | else if (tableType.contentEquals("mpls")) { |
| 1660 | return TableId.of(OFSwitchImplCPqD13.TABLE_MPLS); |
| 1661 | } |
| 1662 | else if (tableType.contentEquals("acl")) { |
| 1663 | return TableId.of(OFSwitchImplCPqD13.TABLE_ACL); |
| 1664 | } |
| 1665 | else { |
| 1666 | log.warn("Invalid tableType: {}", tableType); |
| 1667 | return null; |
| 1668 | } |
| 1669 | } |
| 1670 | |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1671 | @Override |
| 1672 | public void createTunnel(String tunnelId, List<String> route, NeighborSet ns) { |
| 1673 | |
| 1674 | List<Integer> groups = new ArrayList<Integer>(); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1675 | |
| 1676 | // create a last group of the group chaining |
| 1677 | int finalGroupId = groupid.incrementAndGet(); |
| 1678 | createGroupForANeighborSet(ns, finalGroupId); |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1679 | groups.add(Integer.valueOf(finalGroupId)); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1680 | |
| 1681 | int groupId = 0; |
| 1682 | int nextGroupId = finalGroupId; |
| 1683 | boolean bos = false; |
| 1684 | |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1685 | // process the node ID in order |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1686 | for (int i = 0; i < route.size(); i++) { |
| 1687 | String nodeId = route.get(i); |
| 1688 | groupId = groupid.incrementAndGet(); |
Sangho Shin | e020cc3 | 2014-10-20 13:28:02 -0700 | [diff] [blame] | 1689 | groups.add(Integer.valueOf(groupId)); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1690 | if (i == route.size()-1) |
| 1691 | bos = true; |
| 1692 | createGroupForMplsLabel(groupId, nodeId, nextGroupId, bos); |
| 1693 | nextGroupId = groupId; |
| 1694 | } |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1695 | tunnelGroupIdTable.putIfAbsent(tunnelId, groups); |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1698 | @Override |
| 1699 | public void removeTunnel(String tunnelId) { |
| 1700 | List<Integer> groups = tunnelGroupIdTable.get(tunnelId); |
Sangho Shin | 1ad7be0 | 2014-10-20 16:56:49 -0700 | [diff] [blame] | 1701 | if (groups == null) |
| 1702 | return; |
| 1703 | |
Sangho Shin | 4b46bcd | 2014-10-20 15:48:47 -0700 | [diff] [blame] | 1704 | // we need to delete groups in reverse order |
| 1705 | for (int i = groups.size() - 1; i >= 0; i--) { |
| 1706 | int groupId = groups.get(i); |
| 1707 | deleteGroup(groupId); |
| 1708 | } |
| 1709 | tunnelGroupIdTable.remove(tunnelId); |
| 1710 | } |
Sangho Shin | 15273b6 | 2014-10-16 22:22:05 -0700 | [diff] [blame] | 1711 | |
Sangho Shin | 1ad7be0 | 2014-10-20 16:56:49 -0700 | [diff] [blame] | 1712 | @Override |
| 1713 | public int getTunnelGroupId(String tunnelId) { |
| 1714 | List<Integer> groups = tunnelGroupIdTable.get(tunnelId); |
| 1715 | if (groups == null) |
| 1716 | return -1; |
| 1717 | |
| 1718 | return groups.get(0); |
| 1719 | } |
| 1720 | |
Saurav Das | 2d41a43 | 2014-10-21 20:40:10 -0700 | [diff] [blame] | 1721 | @Override |
| 1722 | public Map<String, String> getPublishAttributes() { |
| 1723 | return publishAttributes; |
| 1724 | } |
Sangho Shin | 1ad7be0 | 2014-10-20 16:56:49 -0700 | [diff] [blame] | 1725 | |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1726 | // ***************************** |
Saurav Das | 80d1739 | 2014-10-01 10:24:56 -0700 | [diff] [blame] | 1727 | // Unused |
Saurav Das | fc5e3eb | 2014-09-25 19:05:21 -0700 | [diff] [blame] | 1728 | // ***************************** |
| 1729 | |
Saurav Das | 80d1739 | 2014-10-01 10:24:56 -0700 | [diff] [blame] | 1730 | @SuppressWarnings("unused") |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1731 | private void setAsyncConfig() throws IOException { |
| 1732 | List<OFMessage> msglist = new ArrayList<OFMessage>(3); |
| 1733 | OFMessage setAC = null; |
| 1734 | |
| 1735 | if (role == Role.MASTER) { |
| 1736 | setAC = factory.buildAsyncSet() |
| 1737 | .setFlowRemovedMaskEqualMaster(SET_FLOW_REMOVED_MASK_MASTER) |
| 1738 | .setPacketInMaskEqualMaster(SET_PACKET_IN_MASK_MASTER) |
| 1739 | .setPortStatusMaskEqualMaster(SET_PORT_STATUS_MASK_MASTER) |
| 1740 | .setFlowRemovedMaskSlave(SET_ALL_SLAVE) |
| 1741 | .setPacketInMaskSlave(SET_ALL_SLAVE) |
| 1742 | .setPortStatusMaskSlave(SET_ALL_SLAVE) |
| 1743 | .setXid(getNextTransactionId()) |
| 1744 | .build(); |
| 1745 | } else if (role == Role.EQUAL) { |
| 1746 | setAC = factory.buildAsyncSet() |
| 1747 | .setFlowRemovedMaskEqualMaster(SET_FLOW_REMOVED_MASK_EQUAL) |
| 1748 | .setPacketInMaskEqualMaster(SET_PACKET_IN_MASK_EQUAL) |
| 1749 | .setPortStatusMaskEqualMaster(SET_PORT_STATUS_MASK_EQUAL) |
| 1750 | .setFlowRemovedMaskSlave(SET_ALL_SLAVE) |
| 1751 | .setPacketInMaskSlave(SET_ALL_SLAVE) |
| 1752 | .setPortStatusMaskSlave(SET_ALL_SLAVE) |
| 1753 | .setXid(getNextTransactionId()) |
| 1754 | .build(); |
| 1755 | } |
| 1756 | msglist.add(setAC); |
| 1757 | |
| 1758 | OFMessage br = factory.buildBarrierRequest() |
| 1759 | .setXid(getNextTransactionId()) |
| 1760 | .build(); |
| 1761 | msglist.add(br); |
| 1762 | |
| 1763 | OFMessage getAC = factory.buildAsyncGetRequest() |
| 1764 | .setXid(getNextTransactionId()) |
| 1765 | .build(); |
| 1766 | msglist.add(getAC); |
| 1767 | |
| 1768 | write(msglist); |
| 1769 | } |
| 1770 | |
Saurav Das | 80d1739 | 2014-10-01 10:24:56 -0700 | [diff] [blame] | 1771 | @SuppressWarnings("unused") |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1772 | private void decodeAsyncGetReply(OFAsyncGetReply rep) { |
| 1773 | long frm = rep.getFlowRemovedMaskEqualMaster(); |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 1774 | long frs = rep.getFlowRemovedMaskSlave(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1775 | long pim = rep.getPacketInMaskEqualMaster(); |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 1776 | long pis = rep.getPacketInMaskSlave(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1777 | long psm = rep.getPortStatusMaskEqualMaster(); |
Sangho Shin | 01bca86 | 2014-09-12 11:18:59 -0700 | [diff] [blame] | 1778 | long pss = rep.getPortStatusMaskSlave(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1779 | |
| 1780 | if (role == Role.MASTER || role == Role.EQUAL) { // should separate |
| 1781 | log.info("FRM:{}", HexString.toHexString((frm & TEST_FLOW_REMOVED_MASK))); |
| 1782 | log.info("PIM:{}", HexString.toHexString((pim & TEST_PACKET_IN_MASK))); |
| 1783 | log.info("PSM:{}", HexString.toHexString((psm & TEST_PORT_STATUS_MASK))); |
| 1784 | } |
| 1785 | |
| 1786 | } |
| 1787 | |
Saurav Das | 80d1739 | 2014-10-01 10:24:56 -0700 | [diff] [blame] | 1788 | @SuppressWarnings("unused") |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1789 | private void getTableFeatures() throws IOException { |
| 1790 | OFMessage gtf = factory.buildTableFeaturesStatsRequest() |
| 1791 | .setXid(getNextTransactionId()) |
| 1792 | .build(); |
| 1793 | write(gtf, null); |
| 1794 | } |
| 1795 | |
Saurav Das | 80d1739 | 2014-10-01 10:24:56 -0700 | [diff] [blame] | 1796 | @SuppressWarnings("unused") |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1797 | private void sendGroupFeaturesRequest() throws IOException { |
| 1798 | OFMessage gfr = factory.buildGroupFeaturesStatsRequest() |
| 1799 | .setXid(getNextTransactionId()) |
| 1800 | .build(); |
| 1801 | write(gfr, null); |
| 1802 | } |
| 1803 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1804 | private void processGroupFeatures(OFGroupFeaturesStatsReply gfsr) { |
| 1805 | log.info("Sw: {} Group Features {}", getStringId(), gfsr); |
| 1806 | } |
| 1807 | |
Saurav Das | cc3e35f | 2014-10-10 15:33:32 -0700 | [diff] [blame] | 1808 | @SuppressWarnings("unused") |
| 1809 | private void testMultipleLabels() { |
| 1810 | if (getId() == 1) { |
| 1811 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 1812 | |
| 1813 | // first all the indirect groups |
| 1814 | |
| 1815 | // the group to switch 2 with outer label |
| 1816 | OFGroup g1 = OFGroup.of(201); |
| 1817 | OFOxmEthDst dmac1 = factory.oxms().ethDst(MacAddress.of("00:00:02:02:02:80")); |
| 1818 | OFAction push1 = factory.actions().pushMpls(EthType.MPLS_UNICAST); |
| 1819 | OFOxmMplsLabel lid1 = factory.oxms() |
| 1820 | .mplsLabel(U32.of(105)); // outer label |
| 1821 | OFAction setMpls1 = factory.actions().buildSetField() |
| 1822 | .setField(lid1).build(); |
| 1823 | OFOxmMplsBos bos1 = factory.oxms() |
| 1824 | .mplsBos(OFBooleanValue.FALSE); |
| 1825 | OFAction setB1 = factory.actions().buildSetField() |
| 1826 | .setField(bos1).build(); |
| 1827 | OFAction setDA1 = factory.actions().buildSetField() |
| 1828 | .setField(dmac1).build(); |
| 1829 | OFAction outp1 = factory.actions().buildOutput() |
| 1830 | .setPort(OFPort.of(2)) |
| 1831 | .build(); |
| 1832 | List<OFAction> a1 = new ArrayList<OFAction>(); |
| 1833 | a1.add(push1); |
| 1834 | a1.add(setMpls1); |
| 1835 | a1.add(setB1); |
| 1836 | a1.add(setDA1); |
| 1837 | a1.add(outp1); |
| 1838 | OFBucket b1 = factory.buildBucket() |
| 1839 | .setActions(a1) |
| 1840 | .build(); |
| 1841 | OFMessage gm1 = factory.buildGroupAdd() |
| 1842 | .setGroup(g1) |
| 1843 | .setBuckets(Collections.singletonList(b1)) |
| 1844 | .setGroupType(OFGroupType.INDIRECT) |
| 1845 | .setXid(getNextTransactionId()) |
| 1846 | .build(); |
| 1847 | msglist.add(gm1); |
| 1848 | |
| 1849 | // the group to switch 3 with outer label |
| 1850 | OFGroup g2 = OFGroup.of(301); |
| 1851 | OFOxmEthDst dmac2 = factory.oxms().ethDst(MacAddress.of("00:00:03:03:03:80")); |
| 1852 | OFAction push2 = factory.actions().pushMpls(EthType.MPLS_UNICAST); |
| 1853 | OFOxmMplsLabel lid2 = factory.oxms() |
| 1854 | .mplsLabel(U32.of(104)); // outer label |
| 1855 | OFAction setMpls2 = factory.actions().buildSetField() |
| 1856 | .setField(lid2).build(); |
| 1857 | OFOxmMplsBos bos2 = factory.oxms() |
| 1858 | .mplsBos(OFBooleanValue.FALSE); |
| 1859 | OFAction setB2 = factory.actions().buildSetField() |
| 1860 | .setField(bos2).build(); |
| 1861 | OFAction setDA2 = factory.actions().buildSetField() |
| 1862 | .setField(dmac2).build(); |
| 1863 | OFAction outp2 = factory.actions().buildOutput() |
| 1864 | .setPort(OFPort.of(3)) |
| 1865 | .build(); |
| 1866 | List<OFAction> a2 = new ArrayList<OFAction>(); |
| 1867 | a2.add(push2); |
| 1868 | a2.add(setMpls2); |
| 1869 | a2.add(setB2); |
| 1870 | a2.add(setDA2); |
| 1871 | a2.add(outp2); |
| 1872 | OFBucket b2 = factory.buildBucket() |
| 1873 | .setActions(a2) |
| 1874 | .build(); |
| 1875 | OFMessage gm2 = factory.buildGroupAdd() |
| 1876 | .setGroup(g2) |
| 1877 | .setBuckets(Collections.singletonList(b2)) |
| 1878 | .setGroupType(OFGroupType.INDIRECT) |
| 1879 | .setXid(getNextTransactionId()) |
| 1880 | .build(); |
| 1881 | msglist.add(gm2); |
| 1882 | |
| 1883 | // now add main ECMP group with inner labels |
| 1884 | OFGroup group = OFGroup.of(786); |
| 1885 | List<OFBucket> buckets = new ArrayList<OFBucket>(); |
| 1886 | for (int i = 0; i < 2; i++) { // 2 buckets |
| 1887 | |
| 1888 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 1889 | OFOxmEthSrc smac = factory.oxms() |
| 1890 | .ethSrc(MacAddress.of("00:00:01:01:01:80")); |
| 1891 | OFAction setSA = factory.actions().buildSetField() |
| 1892 | .setField(smac).build(); |
| 1893 | actions.add(setSA); |
| 1894 | |
| 1895 | if (i == 0) { |
| 1896 | // send to switch 2 |
| 1897 | OFAction pushX = factory.actions().pushMpls(EthType.MPLS_UNICAST); |
| 1898 | OFOxmMplsLabel lidX = factory.oxms() |
| 1899 | .mplsLabel(U32.of(106)); // inner label |
| 1900 | OFAction setX = factory.actions().buildSetField() |
| 1901 | .setField(lidX).build(); |
| 1902 | OFOxmMplsBos bosX = factory.oxms() |
| 1903 | .mplsBos(OFBooleanValue.TRUE); |
| 1904 | OFAction setBX = factory.actions().buildSetField() |
| 1905 | .setField(bosX).build(); |
| 1906 | OFAction ogX = factory.actions().buildGroup() |
| 1907 | .setGroup(g1).build(); |
| 1908 | actions.add(pushX); |
| 1909 | actions.add(setX); |
| 1910 | actions.add(setBX); |
| 1911 | actions.add(ogX); |
| 1912 | |
| 1913 | } else { |
| 1914 | // send to switch 3 |
| 1915 | OFAction pushY = factory.actions().pushMpls(EthType.MPLS_UNICAST); |
| 1916 | OFOxmMplsLabel lidY = factory.oxms() |
| 1917 | .mplsLabel(U32.of(106)); // inner label |
| 1918 | OFAction setY = factory.actions().buildSetField() |
| 1919 | .setField(lidY).build(); |
| 1920 | OFOxmMplsBos bosY = factory.oxms() |
| 1921 | .mplsBos(OFBooleanValue.TRUE); |
| 1922 | OFAction setBY = factory.actions().buildSetField() |
| 1923 | .setField(bosY).build(); |
| 1924 | OFAction ogY = factory.actions().buildGroup() |
| 1925 | .setGroup(g2).build(); |
| 1926 | actions.add(pushY); |
| 1927 | actions.add(setY); |
| 1928 | actions.add(setBY); |
| 1929 | actions.add(ogY); |
| 1930 | } |
| 1931 | |
| 1932 | OFBucket ofb = factory.buildBucket() |
| 1933 | .setWeight(1) |
| 1934 | .setActions(actions) |
| 1935 | .build(); |
| 1936 | buckets.add(ofb); |
| 1937 | } |
| 1938 | |
| 1939 | OFMessage gm = factory.buildGroupAdd() |
| 1940 | .setGroup(group) |
| 1941 | .setBuckets(buckets) |
| 1942 | .setGroupType(OFGroupType.SELECT) |
| 1943 | .setXid(getNextTransactionId()) |
| 1944 | .build(); |
| 1945 | msglist.add(gm); |
| 1946 | |
| 1947 | // create an ACL entry to use this ecmp group |
| 1948 | Builder matchBuilder = factory.buildMatch(); |
| 1949 | matchBuilder.setExact(MatchField.ETH_TYPE, EthType.of(0x800)); |
| 1950 | matchBuilder.setMasked(MatchField.IPV4_DST, |
| 1951 | IPv4Address.of("7.7.7.0") |
| 1952 | .withMaskOfLength(24)); |
| 1953 | |
| 1954 | OFAction grp = factory.actions().buildGroup() |
| 1955 | .setGroup(OFGroup.of(786)) |
| 1956 | .build(); |
| 1957 | List<OFAction> writeActions = Collections.singletonList(grp); |
| 1958 | |
| 1959 | OFInstruction clearInstr = factory.instructions().clearActions(); |
| 1960 | OFInstruction writeInstr = factory.instructions().buildWriteActions() |
| 1961 | .setActions(writeActions).build(); |
| 1962 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 1963 | instructions.add(clearInstr); |
| 1964 | instructions.add(writeInstr); |
| 1965 | |
| 1966 | OFFlowMod.Builder fmBuilder = factory.buildFlowAdd(); |
| 1967 | |
| 1968 | OFMessage aclFlow = fmBuilder |
| 1969 | .setTableId(TableId.of(TABLE_ACL)) |
| 1970 | .setMatch(matchBuilder.build()) |
| 1971 | .setInstructions(instructions) |
| 1972 | .setPriority(10) // TODO: wrong - should be MA |
| 1973 | // priority |
| 1974 | .setBufferId(OFBufferId.NO_BUFFER) |
| 1975 | .setIdleTimeout(0) |
| 1976 | .setHardTimeout(0) |
| 1977 | .setXid(getNextTransactionId()) |
| 1978 | .build(); |
| 1979 | msglist.add(aclFlow); |
| 1980 | |
| 1981 | try { |
| 1982 | write(msglist); |
| 1983 | } catch (IOException e) { |
| 1984 | // TODO Auto-generated catch block |
| 1985 | e.printStackTrace(); |
| 1986 | } |
Fahad Naeem Khan | d563af6 | 2014-10-08 17:37:25 -0700 | [diff] [blame] | 1987 | } |
| 1988 | } |
| 1989 | |
Saurav Das | 2d41a43 | 2014-10-21 20:40:10 -0700 | [diff] [blame] | 1990 | |
| 1991 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1992 | } |