Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.openflow.drivers; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 47f2dde | 2015-01-14 17:45:06 -0800 | [diff] [blame] | 18 | import java.io.IOException; |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.Collections; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | import java.util.concurrent.atomic.AtomicBoolean; |
| 25 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.openflow.controller.Dpid; |
| 27 | import org.onosproject.openflow.controller.RoleState; |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 28 | import org.onosproject.openflow.controller.OpenFlowSwitch.TableType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch; |
| 30 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeAlreadyStarted; |
| 31 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeCompleted; |
| 32 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeNotStarted; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 33 | import org.projectfloodlight.openflow.protocol.OFAsyncGetReply; |
| 34 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 35 | import org.projectfloodlight.openflow.protocol.OFBucket; |
| 36 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 37 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 38 | import org.projectfloodlight.openflow.protocol.OFGroupDescStatsReply; |
| 39 | import org.projectfloodlight.openflow.protocol.OFGroupFeaturesStatsReply; |
| 40 | import org.projectfloodlight.openflow.protocol.OFGroupType; |
| 41 | import org.projectfloodlight.openflow.protocol.OFMatchV3; |
| 42 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 43 | import org.projectfloodlight.openflow.protocol.OFOxmList; |
| 44 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 45 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
| 46 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 47 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
| 48 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthDst; |
| 49 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthSrc; |
| 50 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmEthType; |
| 51 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmInPort; |
| 52 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmIpv4DstMasked; |
| 53 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmMetadataMasked; |
| 54 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmMplsLabel; |
| 55 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid; |
| 56 | import org.projectfloodlight.openflow.types.EthType; |
| 57 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 58 | import org.projectfloodlight.openflow.types.MacAddress; |
| 59 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 60 | import org.projectfloodlight.openflow.types.OFGroup; |
| 61 | import org.projectfloodlight.openflow.types.OFMetadata; |
| 62 | import org.projectfloodlight.openflow.types.OFPort; |
| 63 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
| 64 | import org.projectfloodlight.openflow.types.TableId; |
| 65 | import org.projectfloodlight.openflow.types.U32; |
| 66 | import org.projectfloodlight.openflow.types.U64; |
| 67 | import org.projectfloodlight.openflow.util.HexString; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * OFDescriptionStatistics Vendor (Manufacturer Desc.): Stanford University, |
| 71 | * Ericsson Research and CPqD Research. Make (Hardware Desc.) : OpenFlow 1.3 |
| 72 | * Reference Userspace Switch Model (Datapath Desc.) : None Software : Serial : |
| 73 | * None |
| 74 | */ |
| 75 | public class OFSwitchImplCPqD13 extends AbstractOpenFlowSwitch { |
| 76 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 77 | private static final int VLAN_ID_OFFSET = 16; |
| 78 | private final AtomicBoolean driverHandshakeComplete; |
| 79 | private OFFactory factory; |
| 80 | private static final int OFPCML_NO_BUFFER = 0xffff; |
| 81 | // Configuration of asynch messages to controller. We need different |
| 82 | // asynch messages depending on role-equal or role-master. |
| 83 | // We don't want to get anything if we are slave. |
| 84 | private static final long SET_FLOW_REMOVED_MASK_MASTER = 0xf; |
| 85 | private static final long SET_PACKET_IN_MASK_MASTER = 0x7; |
| 86 | private static final long SET_PORT_STATUS_MASK_MASTER = 0x7; |
| 87 | private static final long SET_FLOW_REMOVED_MASK_EQUAL = 0x0; |
| 88 | private static final long SET_PACKET_IN_MASK_EQUAL = 0x0; |
| 89 | private static final long SET_PORT_STATUS_MASK_EQUAL = 0x7; |
| 90 | private static final long SET_ALL_SLAVE = 0x0; |
| 91 | |
| 92 | private static final long TEST_FLOW_REMOVED_MASK = 0xf; |
| 93 | private static final long TEST_PACKET_IN_MASK = 0x7; |
| 94 | private static final long TEST_PORT_STATUS_MASK = 0x7; |
| 95 | private long barrierXidToWaitFor = -1; |
| 96 | |
| 97 | private static final int TABLE_VLAN = 0; |
| 98 | private static final int TABLE_TMAC = 1; |
| 99 | private static final int TABLE_IPV4_UNICAST = 2; |
| 100 | private static final int TABLE_MPLS = 3; |
| 101 | private static final int TABLE_META = 4; |
| 102 | private static final int TABLE_ACL = 5; |
| 103 | |
| 104 | private static final short MAX_PRIORITY = (short) 0xffff; |
| 105 | private static final short SLASH_24_PRIORITY = (short) 0xfff0; |
| 106 | private static final short MIN_PRIORITY = 0x0; |
| 107 | private static final U64 METADATA_MASK = U64.of(Long.MAX_VALUE << 1 | 0x1); |
| 108 | |
| 109 | private final Map<Integer, OFGroup> l2groups; |
| 110 | |
Jonathan Hart | 47f2dde | 2015-01-14 17:45:06 -0800 | [diff] [blame] | 111 | public OFSwitchImplCPqD13(Dpid dpid, OFDescStatsReply desc) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 112 | super(dpid); |
| 113 | driverHandshakeComplete = new AtomicBoolean(false); |
| 114 | l2groups = new ConcurrentHashMap<Integer, OFGroup>(); |
| 115 | setSwitchDescription(desc); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* (non-Javadoc) |
| 119 | * @see java.lang.Object#toString() |
| 120 | */ |
| 121 | @Override |
| 122 | public String toString() { |
| 123 | return "OFSwitchImplCPqD13 [" + ((channel != null) |
| 124 | ? channel.getRemoteAddress() : "?") |
| 125 | + " DPID[" + ((this.getStringId() != null) ? this.getStringId() : "?") + "]]"; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void startDriverHandshake() { |
| 130 | log.debug("Starting driver handshake for sw {}", getStringId()); |
| 131 | if (startDriverHandshakeCalled) { |
| 132 | throw new SwitchDriverSubHandshakeAlreadyStarted(); |
| 133 | } |
| 134 | startDriverHandshakeCalled = true; |
| 135 | factory = this.factory(); |
Jonathan Hart | 47f2dde | 2015-01-14 17:45:06 -0800 | [diff] [blame] | 136 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 137 | sendBarrier(true); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public boolean isDriverHandshakeComplete() { |
| 142 | if (!startDriverHandshakeCalled) { |
| 143 | throw new SwitchDriverSubHandshakeNotStarted(); |
| 144 | } |
| 145 | return driverHandshakeComplete.get(); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void processDriverHandshakeMessage(OFMessage m) { |
Ray Milkey | 241b96a | 2014-11-17 13:08:20 -0800 | [diff] [blame] | 150 | if (!startDriverHandshakeCalled) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 151 | throw new SwitchDriverSubHandshakeNotStarted(); |
| 152 | } |
| 153 | if (driverHandshakeComplete.get()) { |
| 154 | throw new SwitchDriverSubHandshakeCompleted(m); |
| 155 | } |
| 156 | |
| 157 | switch (m.getType()) { |
| 158 | case BARRIER_REPLY: |
| 159 | if (m.getXid() == barrierXidToWaitFor) { |
| 160 | driverHandshakeComplete.set(true); |
| 161 | } |
| 162 | break; |
| 163 | |
| 164 | case ERROR: |
| 165 | log.error("Switch {} Error {}", getStringId(), m); |
| 166 | break; |
| 167 | |
| 168 | case GET_ASYNC_REPLY: |
| 169 | OFAsyncGetReply asrep = (OFAsyncGetReply) m; |
| 170 | decodeAsyncGetReply(asrep); |
| 171 | break; |
| 172 | case STATS_REPLY: |
| 173 | processStatsReply((OFStatsReply) m); |
| 174 | break; |
| 175 | case PACKET_IN: |
| 176 | case PORT_STATUS: |
| 177 | case QUEUE_GET_CONFIG_REPLY: |
| 178 | case ROLE_REPLY: |
| 179 | case FEATURES_REPLY: |
| 180 | case FLOW_REMOVED: |
| 181 | break; |
| 182 | |
| 183 | default: |
| 184 | log.debug("Received message {} during switch-driver subhandshake " |
| 185 | + "from switch {} ... Ignoring message", m, getStringId()); |
| 186 | |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private void configureSwitch() throws IOException { |
| 191 | // setAsyncConfig(); |
| 192 | // getTableFeatures(); |
| 193 | sendGroupFeaturesRequest(); |
| 194 | setL2Groups(); |
| 195 | sendBarrier(false); |
| 196 | setL3Groups(); |
| 197 | setL25Groups(); |
| 198 | sendGroupDescRequest(); |
| 199 | populateTableVlan(); |
| 200 | populateTableTMac(); |
| 201 | populateIpTable(); |
| 202 | populateMplsTable(); |
| 203 | populateTableMissEntry(TABLE_ACL, false, false, false, -1); |
| 204 | sendBarrier(true); |
| 205 | } |
| 206 | |
| 207 | private void setAsyncConfig() throws IOException { |
| 208 | List<OFMessage> msglist = new ArrayList<OFMessage>(3); |
| 209 | OFMessage setAC = null; |
| 210 | |
| 211 | if (role == RoleState.MASTER) { |
| 212 | setAC = factory.buildAsyncSet() |
| 213 | .setFlowRemovedMaskEqualMaster(SET_FLOW_REMOVED_MASK_MASTER) |
| 214 | .setPacketInMaskEqualMaster(SET_PACKET_IN_MASK_MASTER) |
| 215 | .setPortStatusMaskEqualMaster(SET_PORT_STATUS_MASK_MASTER) |
| 216 | .setFlowRemovedMaskSlave(SET_ALL_SLAVE) |
| 217 | .setPacketInMaskSlave(SET_ALL_SLAVE) |
| 218 | .setPortStatusMaskSlave(SET_ALL_SLAVE) |
| 219 | .setXid(getNextTransactionId()) |
| 220 | .build(); |
| 221 | } else if (role == RoleState.EQUAL) { |
| 222 | setAC = factory.buildAsyncSet() |
| 223 | .setFlowRemovedMaskEqualMaster(SET_FLOW_REMOVED_MASK_EQUAL) |
| 224 | .setPacketInMaskEqualMaster(SET_PACKET_IN_MASK_EQUAL) |
| 225 | .setPortStatusMaskEqualMaster(SET_PORT_STATUS_MASK_EQUAL) |
| 226 | .setFlowRemovedMaskSlave(SET_ALL_SLAVE) |
| 227 | .setPacketInMaskSlave(SET_ALL_SLAVE) |
| 228 | .setPortStatusMaskSlave(SET_ALL_SLAVE) |
| 229 | .setXid(getNextTransactionId()) |
| 230 | .build(); |
| 231 | } |
| 232 | msglist.add(setAC); |
| 233 | |
| 234 | OFMessage br = factory.buildBarrierRequest() |
| 235 | .setXid(getNextTransactionId()) |
| 236 | .build(); |
| 237 | msglist.add(br); |
| 238 | |
| 239 | OFMessage getAC = factory.buildAsyncGetRequest() |
| 240 | .setXid(getNextTransactionId()) |
| 241 | .build(); |
| 242 | msglist.add(getAC); |
| 243 | |
| 244 | sendMsg(msglist); |
| 245 | } |
| 246 | |
| 247 | private void decodeAsyncGetReply(OFAsyncGetReply rep) { |
| 248 | long frm = rep.getFlowRemovedMaskEqualMaster(); |
| 249 | //long frs = rep.getFlowRemovedMaskSlave(); |
| 250 | long pim = rep.getPacketInMaskEqualMaster(); |
| 251 | //long pis = rep.getPacketInMaskSlave(); |
| 252 | long psm = rep.getPortStatusMaskEqualMaster(); |
| 253 | //long pss = rep.getPortStatusMaskSlave(); |
| 254 | |
| 255 | if (role == RoleState.MASTER || role == RoleState.EQUAL) { // should separate |
| 256 | log.info("FRM:{}", HexString.toHexString((frm & TEST_FLOW_REMOVED_MASK))); |
| 257 | log.info("PIM:{}", HexString.toHexString((pim & TEST_PACKET_IN_MASK))); |
| 258 | log.info("PSM:{}", HexString.toHexString((psm & TEST_PORT_STATUS_MASK))); |
| 259 | } |
| 260 | |
| 261 | } |
| 262 | |
| 263 | private void getTableFeatures() throws IOException { |
| 264 | OFMessage gtf = factory.buildTableFeaturesStatsRequest() |
| 265 | .setXid(getNextTransactionId()) |
| 266 | .build(); |
| 267 | sendMsg(gtf); |
| 268 | } |
| 269 | |
| 270 | private void sendGroupFeaturesRequest() throws IOException { |
| 271 | OFMessage gfr = factory.buildGroupFeaturesStatsRequest() |
| 272 | .setXid(getNextTransactionId()) |
| 273 | .build(); |
| 274 | sendMsg(gfr); |
| 275 | } |
| 276 | |
| 277 | private void sendGroupDescRequest() throws IOException { |
| 278 | OFMessage gdr = factory.buildGroupDescStatsRequest() |
| 279 | .setXid(getNextTransactionId()) |
| 280 | .build(); |
| 281 | sendMsg(gdr); |
| 282 | } |
| 283 | |
| 284 | /*Create L2 interface groups for all physical ports |
| 285 | Naming convention followed is the same as OF-DPA spec |
| 286 | eg. port 1 with allowed vlan 10, is enveloped in group with id, |
| 287 | 0x0 00a 0001, where the uppermost 4 bits identify an L2 interface, |
| 288 | the next 12 bits identify the vlan-id, and the lowermost 16 bits |
| 289 | identify the port number.*/ |
| 290 | private void setL2Groups() throws IOException { |
| 291 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 292 | for (OFPortDesc p : getPorts()) { |
| 293 | int pnum = p.getPortNo().getPortNumber(); |
| 294 | int portVlan = getVlanConfig(pnum); |
| 295 | if (U32.of(pnum).compareTo(U32.of(OFPort.MAX.getPortNumber())) < 1) { |
| 296 | OFGroup gl2 = OFGroup.of(pnum | (portVlan << VLAN_ID_OFFSET)); |
| 297 | OFAction out = factory.actions().buildOutput() |
| 298 | .setPort(p.getPortNo()).build(); |
| 299 | OFAction popVlan = factory.actions().popVlan(); |
| 300 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 301 | actions.add(popVlan); |
| 302 | actions.add(out); |
| 303 | OFBucket bucket = factory.buildBucket() |
| 304 | .setActions(actions).build(); |
| 305 | List<OFBucket> buckets = Collections.singletonList(bucket); |
| 306 | OFMessage gmAdd = factory.buildGroupAdd() |
| 307 | .setGroup(gl2) |
| 308 | .setBuckets(buckets) |
| 309 | .setGroupType(OFGroupType.INDIRECT) |
| 310 | .setXid(getNextTransactionId()) |
| 311 | .build(); |
| 312 | msglist.add(gmAdd); |
| 313 | l2groups.put(pnum, gl2); |
| 314 | } |
| 315 | } |
| 316 | log.debug("Creating {} L2 groups in sw {}", msglist.size(), getStringId()); |
| 317 | sendMsg(msglist); |
| 318 | } |
| 319 | |
| 320 | private int getVlanConfig(int portnum) { |
| 321 | int portVlan = 10 * portnum; |
| 322 | if ((getId() == 0x1 && portnum == 6) || |
| 323 | (getId() == 0x2) || |
| 324 | (getId() == 0x3 && portnum == 2)) { |
| 325 | portVlan = 192; // 0xc0 |
| 326 | } |
| 327 | return portVlan; |
| 328 | } |
| 329 | |
| 330 | private MacAddress getRouterMacAddr() { |
| 331 | if (getId() == 0x3) { |
| 332 | return MacAddress.of("00:00:07:07:07:80"); // router mac |
| 333 | } |
| 334 | if (getId() == 0x1) { |
| 335 | return MacAddress.of("00:00:01:01:01:80"); |
| 336 | } |
| 337 | // switch 0x2 |
| 338 | return MacAddress.of("00:00:02:02:02:80"); |
| 339 | } |
| 340 | |
| 341 | // only for ports connected to other routers |
| 342 | private OFAction getDestAction(int portnum) { |
| 343 | OFAction setDA = null; |
| 344 | MacAddress dAddr = null; |
| 345 | if (getId() == 0x1 && portnum == 6) { // connected to switch 2 |
| 346 | dAddr = MacAddress.of("00:00:02:02:02:80"); |
| 347 | } |
| 348 | if (getId() == 0x2) { |
| 349 | if (portnum == 1) { // connected to sw 1 |
| 350 | dAddr = MacAddress.of("00:00:01:01:01:80"); |
| 351 | } else if (portnum == 2) { // connected to sw 3 |
| 352 | dAddr = MacAddress.of("00:00:07:07:07:80"); |
| 353 | } |
| 354 | } |
| 355 | if (getId() == 0x3) { |
| 356 | if (portnum == 2) { // connected to switch 2 |
| 357 | dAddr = MacAddress.of("00:00:02:02:02:80"); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (dAddr != null) { |
| 362 | OFOxmEthDst dstAddr = factory.oxms().ethDst(dAddr); |
| 363 | setDA = factory.actions().buildSetField() |
| 364 | .setField(dstAddr).build(); |
| 365 | } |
| 366 | return setDA; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * L3 groups are created for all router ports and they all point to corresponding |
| 371 | * L2 groups. Only the ports that connect to other routers will have the |
| 372 | * DA set. |
| 373 | */ |
| 374 | private void setL3Groups() throws IOException { |
| 375 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 376 | for (OFGroup gl2 : l2groups.values()) { |
| 377 | int gnum = gl2.getGroupNumber(); |
| 378 | int portnum = gnum & 0x0000ffff; |
| 379 | int vlanid = ((gnum & 0x0fff0000) >> VLAN_ID_OFFSET); |
| 380 | MacAddress sAddr = getRouterMacAddr(); |
| 381 | |
| 382 | OFGroup gl3 = OFGroup.of(0x20000000 | portnum); |
| 383 | OFAction group = factory.actions().buildGroup() |
| 384 | .setGroup(gl2).build(); |
| 385 | OFOxmEthSrc srcAddr = factory.oxms().ethSrc(sAddr); |
| 386 | OFAction setSA = factory.actions().buildSetField() |
| 387 | .setField(srcAddr).build(); |
| 388 | OFOxmVlanVid vid = factory.oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanid)); |
| 389 | OFAction setVlan = factory.actions().buildSetField() |
| 390 | .setField(vid).build(); |
| 391 | OFAction decTtl = factory.actions().decNwTtl(); |
| 392 | |
| 393 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 394 | actions.add(decTtl); // decrement the IP TTL/do-checksum/check TTL |
| 395 | // and MTU |
| 396 | actions.add(setVlan); // set the vlan-id of the exit-port (and |
| 397 | // l2group) |
| 398 | actions.add(setSA); // set this routers mac address |
| 399 | // make L3Unicast group setDA for known (configured) ports |
| 400 | // that connect to other routers |
| 401 | OFAction setDA = getDestAction(portnum); |
| 402 | if (setDA != null) { |
| 403 | actions.add(setDA); |
| 404 | } |
| 405 | actions.add(group); |
| 406 | |
| 407 | OFBucket bucket = factory.buildBucket() |
| 408 | .setActions(actions).build(); |
| 409 | List<OFBucket> buckets = Collections.singletonList(bucket); |
| 410 | OFMessage gmAdd = factory.buildGroupAdd() |
| 411 | .setGroup(gl3) |
| 412 | .setBuckets(buckets) |
| 413 | .setGroupType(OFGroupType.INDIRECT) |
| 414 | .setXid(getNextTransactionId()) |
| 415 | .build(); |
| 416 | msglist.add(gmAdd); |
| 417 | } |
| 418 | sendMsg(msglist); |
| 419 | log.debug("Creating {} L3 groups in sw {}", msglist.size(), getStringId()); |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * L2.5 or mpls-unicast groups are only created for those router ports |
| 424 | * connected to other router ports. They differ from the corresponding |
| 425 | * L3-unicast group only by the fact that they decrement the MPLS TTL |
| 426 | * instead of the IP ttl |
| 427 | */ |
| 428 | private void setL25Groups() throws IOException { |
| 429 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 430 | for (OFGroup gl2 : l2groups.values()) { |
| 431 | int gnum = gl2.getGroupNumber(); |
| 432 | int portnum = gnum & 0x0000ffff; |
| 433 | int vlanid = ((gnum & 0x0fff0000) >> VLAN_ID_OFFSET); |
| 434 | MacAddress sAddr = getRouterMacAddr(); |
| 435 | OFAction setDA = getDestAction(portnum); |
| 436 | // setDA will only be non-null for ports connected to routers |
| 437 | if (setDA != null) { |
| 438 | OFGroup gl3 = OFGroup.of(0xa0000000 | portnum); // different id |
| 439 | // for mpls |
| 440 | // group |
| 441 | OFAction group = factory.actions().buildGroup() |
| 442 | .setGroup(gl2).build(); |
| 443 | OFOxmEthSrc srcAddr = factory.oxms().ethSrc(sAddr); |
| 444 | OFAction setSA = factory.actions().buildSetField() |
| 445 | .setField(srcAddr).build(); |
| 446 | OFOxmVlanVid vid = factory.oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanid)); |
| 447 | OFAction setVlan = factory.actions().buildSetField() |
| 448 | .setField(vid).build(); |
| 449 | OFAction decMplsTtl = factory.actions().decMplsTtl(); |
| 450 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 451 | actions.add(decMplsTtl); // decrement the MPLS |
| 452 | // TTL/do-checksum/check TTL and MTU |
| 453 | actions.add(setVlan); // set the vlan-id of the exit-port (and |
| 454 | // l2group) |
| 455 | actions.add(setSA); // set this routers mac address |
| 456 | actions.add(setDA); |
| 457 | actions.add(group); |
| 458 | OFBucket bucket = factory.buildBucket() |
| 459 | .setActions(actions).build(); |
| 460 | List<OFBucket> buckets = Collections.singletonList(bucket); |
| 461 | OFMessage gmAdd = factory.buildGroupAdd() |
| 462 | .setGroup(gl3) |
| 463 | .setBuckets(buckets) |
| 464 | .setGroupType(OFGroupType.INDIRECT) |
| 465 | .setXid(getNextTransactionId()) |
| 466 | .build(); |
| 467 | msglist.add(gmAdd); |
| 468 | } |
| 469 | } |
| 470 | sendMsg(msglist); |
| 471 | log.debug("Creating {} MPLS groups in sw {}", msglist.size(), getStringId()); |
| 472 | } |
| 473 | |
| 474 | /* Using ECMP groups |
| 475 | * |
| 476 | * OFGroup group47 = OFGroup.of(47); |
| 477 | OFAction outgroup1 = factory.actions() |
| 478 | .buildGroup() |
| 479 | .setGroup(group61) |
| 480 | .build(); |
| 481 | OFBucket buc47_1 = factory.buildBucket() |
| 482 | .setWeight(1) |
| 483 | .setActions(Collections.singletonList(outgroup1)) |
| 484 | .build(); |
| 485 | OFAction outgroup2 = factory.actions() |
| 486 | .buildGroup() |
| 487 | .setGroup(group62) |
| 488 | .build(); |
| 489 | OFBucket buc47_2 = factory.buildBucket() |
| 490 | .setWeight(1) |
| 491 | .setActions(Collections.singletonList(outgroup2)) |
| 492 | .build(); |
| 493 | List<OFBucket> buckets47 = new ArrayList<OFBucket>(); |
| 494 | buckets47.add(buc47_1); |
| 495 | buckets47.add(buc47_2); |
| 496 | OFMessage gmS12 = factory.buildGroupAdd() |
| 497 | .setGroup(group47) |
| 498 | .setBuckets(buckets47) |
| 499 | .setGroupType(OFGroupType.SELECT) |
| 500 | .setXid(getNextTransactionId()) |
| 501 | .build(); |
| 502 | write(gmS12, null); */ |
| 503 | |
| 504 | private void processStatsReply(OFStatsReply sr) { |
| 505 | switch (sr.getStatsType()) { |
| 506 | case AGGREGATE: |
| 507 | break; |
| 508 | case DESC: |
| 509 | break; |
| 510 | case EXPERIMENTER: |
| 511 | break; |
| 512 | case FLOW: |
| 513 | break; |
| 514 | case GROUP_DESC: |
| 515 | processGroupDesc((OFGroupDescStatsReply) sr); |
| 516 | break; |
| 517 | case GROUP_FEATURES: |
| 518 | processGroupFeatures((OFGroupFeaturesStatsReply) sr); |
| 519 | break; |
| 520 | case METER_CONFIG: |
| 521 | break; |
| 522 | case METER_FEATURES: |
| 523 | break; |
| 524 | case PORT_DESC: |
| 525 | break; |
| 526 | case TABLE_FEATURES: |
| 527 | break; |
| 528 | default: |
| 529 | break; |
| 530 | |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | private void processGroupFeatures(OFGroupFeaturesStatsReply gfsr) { |
| 535 | log.info("Sw: {} Group Features {}", getStringId(), gfsr); |
| 536 | } |
| 537 | |
| 538 | private void processGroupDesc(OFGroupDescStatsReply gdsr) { |
| 539 | log.info("Sw: {} Group Desc {}", getStringId(), gdsr); |
| 540 | } |
| 541 | |
| 542 | private void populateTableVlan() throws IOException { |
| 543 | // for all incoming ports assign configured port-vlans |
| 544 | // currently assign portnum*10 -> vlanid to access ports |
| 545 | // and vlan 192 to router to router ports |
| 546 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 547 | for (OFPortDesc p : getPorts()) { |
| 548 | int pnum = p.getPortNo().getPortNumber(); |
| 549 | if (U32.of(pnum).compareTo(U32.of(OFPort.MAX.getPortNumber())) < 1) { |
| 550 | int vlanid = getVlanConfig(pnum); |
| 551 | OFOxmInPort oxp = factory.oxms().inPort(p.getPortNo()); |
| 552 | OFOxmVlanVid oxv = factory.oxms() |
| 553 | .vlanVid(OFVlanVidMatch.UNTAGGED); |
| 554 | OFOxmList oxmList = OFOxmList.of(oxp, oxv); |
| 555 | OFMatchV3 match = factory.buildMatchV3() |
| 556 | .setOxmList(oxmList).build(); |
| 557 | OFOxmVlanVid vidToSet = factory.oxms() |
| 558 | .vlanVid(OFVlanVidMatch.ofVlan(vlanid)); |
| 559 | OFAction pushVlan = factory.actions().pushVlan(EthType.VLAN_FRAME); |
| 560 | OFAction setVlan = factory.actions().setField(vidToSet); |
| 561 | List<OFAction> actionlist = new ArrayList<OFAction>(); |
| 562 | actionlist.add(pushVlan); |
| 563 | actionlist.add(setVlan); |
| 564 | OFInstruction appAction = factory.instructions().buildApplyActions() |
| 565 | .setActions(actionlist).build(); |
| 566 | OFInstruction gotoTbl = factory.instructions().buildGotoTable() |
| 567 | .setTableId(TableId.of(TABLE_TMAC)).build(); |
| 568 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 569 | instructions.add(appAction); |
| 570 | instructions.add(gotoTbl); |
| 571 | OFMessage flowEntry = factory.buildFlowAdd() |
| 572 | .setTableId(TableId.of(TABLE_VLAN)) |
| 573 | .setMatch(match) |
| 574 | .setInstructions(instructions) |
| 575 | .setPriority(1000) // does not matter - all rules |
| 576 | // exclusive |
| 577 | .setBufferId(OFBufferId.NO_BUFFER) |
| 578 | .setIdleTimeout(0) |
| 579 | .setHardTimeout(0) |
| 580 | .setXid(getNextTransactionId()) |
| 581 | .build(); |
| 582 | msglist.add(flowEntry); |
| 583 | } |
| 584 | } |
| 585 | // table-vlan has no table-miss entry, and so packets that miss are |
| 586 | // essentially dropped |
| 587 | sendMsg(msglist); |
| 588 | log.debug("Adding {} vlan-rules in sw {}", msglist.size(), getStringId()); |
| 589 | } |
| 590 | |
| 591 | private void populateTableTMac() throws IOException { |
| 592 | // match for ip packets |
| 593 | OFOxmEthType oxe = factory.oxms().ethType(EthType.IPv4); |
| 594 | OFOxmList oxmListIp = OFOxmList.of(oxe); |
| 595 | OFMatchV3 matchIp = factory.buildMatchV3() |
| 596 | .setOxmList(oxmListIp).build(); |
| 597 | OFInstruction gotoTblIp = factory.instructions().buildGotoTable() |
| 598 | .setTableId(TableId.of(TABLE_IPV4_UNICAST)).build(); |
| 599 | List<OFInstruction> instructionsIp = Collections.singletonList(gotoTblIp); |
| 600 | OFMessage ipEntry = factory.buildFlowAdd() |
| 601 | .setTableId(TableId.of(TABLE_TMAC)) |
| 602 | .setMatch(matchIp) |
| 603 | .setInstructions(instructionsIp) |
| 604 | .setPriority(1000) // strict priority required lower than |
| 605 | // multicastMac |
| 606 | .setBufferId(OFBufferId.NO_BUFFER) |
| 607 | .setIdleTimeout(0) |
| 608 | .setHardTimeout(0) |
| 609 | .setXid(getNextTransactionId()) |
| 610 | .build(); |
| 611 | |
| 612 | // match for mpls packets |
| 613 | OFOxmEthType oxmpls = factory.oxms().ethType(EthType.MPLS_UNICAST); |
| 614 | OFOxmList oxmListMpls = OFOxmList.of(oxmpls); |
| 615 | OFMatchV3 matchMpls = factory.buildMatchV3() |
| 616 | .setOxmList(oxmListMpls).build(); |
| 617 | OFInstruction gotoTblMpls = factory.instructions().buildGotoTable() |
| 618 | .setTableId(TableId.of(TABLE_MPLS)).build(); |
| 619 | List<OFInstruction> instructionsMpls = Collections.singletonList(gotoTblMpls); |
| 620 | OFMessage mplsEntry = factory.buildFlowAdd() |
| 621 | .setTableId(TableId.of(TABLE_TMAC)) |
| 622 | .setMatch(matchMpls) |
| 623 | .setInstructions(instructionsMpls) |
| 624 | .setPriority(1001) // strict priority required lower than |
| 625 | // multicastMac |
| 626 | .setBufferId(OFBufferId.NO_BUFFER) |
| 627 | .setIdleTimeout(0) |
| 628 | .setHardTimeout(0) |
| 629 | .setXid(getNextTransactionId()) |
| 630 | .build(); |
| 631 | |
| 632 | // match for everything else to send to controller. Essentially |
| 633 | // the table miss flow entry |
| 634 | populateTableMissEntry(TABLE_TMAC, true, false, false, -1); |
| 635 | log.debug("Adding termination-mac-rules in sw {}", getStringId()); |
| 636 | List<OFMessage> msglist = new ArrayList<OFMessage>(2); |
| 637 | msglist.add(ipEntry); |
| 638 | msglist.add(mplsEntry); |
| 639 | sendMsg(msglist); |
| 640 | } |
| 641 | |
| 642 | private List<String> getMyIps() { // send to controller |
| 643 | List<String> myIps = new ArrayList<String>(); |
| 644 | if (getId() == 0x1) { |
| 645 | myIps.add("10.0.2.128"); |
| 646 | myIps.add("10.0.3.128"); |
| 647 | myIps.add("10.0.1.128"); |
| 648 | myIps.add("192.168.0.1"); |
| 649 | } |
| 650 | if (getId() == 0x2) { |
| 651 | myIps.add("192.168.0.2"); |
| 652 | } |
| 653 | if (getId() == 0x3) { |
| 654 | myIps.add("192.168.0.3"); |
| 655 | myIps.add("7.7.7.128"); |
| 656 | } |
| 657 | return myIps; |
| 658 | } |
| 659 | |
| 660 | private List<String> getMySubnetIps() { // send to controller |
| 661 | List<String> subnetIps = new ArrayList<String>(); |
| 662 | if (getId() == 0x1) { |
| 663 | subnetIps.add("10.0.2.0"); |
| 664 | subnetIps.add("10.0.3.0"); |
| 665 | subnetIps.add("10.0.1.0"); |
| 666 | } |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 667 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 668 | if (getId() == 0x3) { |
| 669 | subnetIps.add("7.7.7.0"); |
| 670 | } |
| 671 | return subnetIps; |
| 672 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 673 | private static class RouteEntry { |
| 674 | String prefix; |
| 675 | String mask; |
| 676 | int nextHopPort; |
| 677 | String dstMac; |
| 678 | int label; |
| 679 | |
| 680 | public RouteEntry(String prefix, String mask, int nextHopPort, int label) { |
| 681 | this.prefix = prefix; |
| 682 | this.mask = mask; |
| 683 | this.nextHopPort = nextHopPort; |
| 684 | this.label = label; |
| 685 | } |
| 686 | |
| 687 | public RouteEntry(String prefix, int nextHopPort, String dstMac) { |
| 688 | this.prefix = prefix; |
| 689 | this.nextHopPort = nextHopPort; |
| 690 | this.dstMac = dstMac; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | // send out of mpls-group where the next-hop mac-da is already set |
| 695 | private List<RouteEntry> getRouterNextHopIps() { |
| 696 | List<RouteEntry> routerNextHopIps = new ArrayList<RouteEntry>(); |
| 697 | if (getId() == 0x1) { |
| 698 | routerNextHopIps |
| 699 | .add(new RouteEntry("192.168.0.2", "255.255.255.255", 6, 102)); |
| 700 | routerNextHopIps |
| 701 | .add(new RouteEntry("192.168.0.3", "255.255.255.255", 6, 103)); |
| 702 | routerNextHopIps.add(new RouteEntry("7.7.7.0", "255.255.255.0", 6, 103)); |
| 703 | } |
| 704 | //if (getId() == 0x2) { |
| 705 | /* These are required for normal IP routing without labels. |
| 706 | routerNextHopIps.add(new RouteEntry("192.168.0.1","255.255.255.255",1)); |
| 707 | routerNextHopIps.add(new RouteEntry("192.168.0.3","255.255.255.255",2)); |
| 708 | routerNextHopIps.add(new RouteEntry("10.0.1.0","255.255.255.0",1)); |
| 709 | routerNextHopIps.add(new RouteEntry("10.0.2.0","255.255.255.0",1)); |
| 710 | routerNextHopIps.add(new RouteEntry("10.0.3.0","255.255.255.0",1)); |
| 711 | routerNextHopIps.add(new RouteEntry("7.7.7.0","255.255.255.0",2));*/ |
| 712 | //} |
| 713 | if (getId() == 0x3) { |
| 714 | routerNextHopIps |
| 715 | .add(new RouteEntry("192.168.0.2", "255.255.255.255", 2, 102)); |
| 716 | routerNextHopIps |
| 717 | .add(new RouteEntry("192.168.0.1", "255.255.255.255", 2, 101)); |
| 718 | routerNextHopIps.add(new RouteEntry("10.0.1.0", "255.255.255.0", 2, 101)); |
| 719 | routerNextHopIps.add(new RouteEntry("10.0.2.0", "255.255.255.0", 2, 101)); |
| 720 | routerNextHopIps.add(new RouteEntry("10.0.3.0", "255.255.255.0", 2, 101)); |
| 721 | } |
| 722 | return routerNextHopIps; |
| 723 | } |
| 724 | |
| 725 | // known host mac-addr, setDA/send out of l3group |
| 726 | private List<RouteEntry> getHostNextHopIps() { |
| 727 | List<RouteEntry> hostNextHopIps = new ArrayList<RouteEntry>(); |
| 728 | if (getId() == 0x1) { |
| 729 | hostNextHopIps.add(new RouteEntry("10.0.2.1", 4, "00:00:00:00:02:01")); |
| 730 | hostNextHopIps.add(new RouteEntry("10.0.3.1", 5, "00:00:00:00:03:01")); |
| 731 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 732 | if (getId() == 0x3) { |
| 733 | hostNextHopIps.add(new RouteEntry("7.7.7.7", 1, "00:00:07:07:07:07")); |
| 734 | } |
| 735 | return hostNextHopIps; |
| 736 | } |
| 737 | |
| 738 | private void populateIpTable() throws IOException { |
| 739 | populateMyIps(); |
| 740 | populateMySubnets(); |
| 741 | populateRoutes(); |
| 742 | populateHostRoutes(); |
| 743 | |
| 744 | // match for everything else to send to ACL table. Essentially |
| 745 | // the table miss flow entry |
| 746 | populateTableMissEntry(TABLE_IPV4_UNICAST, false, true, |
| 747 | true, TABLE_ACL); |
| 748 | } |
| 749 | |
| 750 | private void populateMyIps() throws IOException { |
| 751 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 752 | // first all my ip's as exact-matches |
| 753 | // write-action instruction to send to controller |
| 754 | List<String> myIps = getMyIps(); |
| 755 | for (int i = 0; i < myIps.size(); i++) { |
| 756 | OFOxmEthType ethTypeIp = factory.oxms() |
| 757 | .ethType(EthType.IPv4); |
| 758 | OFOxmIpv4DstMasked ipPrefix = factory.oxms() |
| 759 | .ipv4DstMasked(IPv4Address.of(myIps.get(i)), IPv4Address.NO_MASK); |
| 760 | OFOxmList oxmListSlash32 = OFOxmList.of(ethTypeIp, ipPrefix); |
| 761 | OFMatchV3 match = factory.buildMatchV3() |
| 762 | .setOxmList(oxmListSlash32).build(); |
| 763 | OFAction outc = factory.actions().buildOutput() |
| 764 | .setPort(OFPort.CONTROLLER).setMaxLen(OFPCML_NO_BUFFER) |
| 765 | .build(); |
| 766 | OFInstruction writeInstr = factory.instructions().buildWriteActions() |
| 767 | .setActions(Collections.singletonList(outc)).build(); |
| 768 | OFInstruction gotoInstr = factory.instructions().buildGotoTable() |
| 769 | .setTableId(TableId.of(TABLE_ACL)).build(); |
| 770 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 771 | instructions.add(writeInstr); |
| 772 | instructions.add(gotoInstr); |
| 773 | OFMessage myIpEntry = factory.buildFlowAdd() |
| 774 | .setTableId(TableId.of(TABLE_IPV4_UNICAST)) |
| 775 | .setMatch(match) |
| 776 | .setInstructions(instructions) |
| 777 | .setPriority(MAX_PRIORITY) // highest priority for exact |
| 778 | // match |
| 779 | .setBufferId(OFBufferId.NO_BUFFER) |
| 780 | .setIdleTimeout(0) |
| 781 | .setHardTimeout(0) |
| 782 | .setXid(getNextTransactionId()) |
| 783 | .build(); |
| 784 | msglist.add(myIpEntry); |
| 785 | } |
| 786 | sendMsg(msglist); |
| 787 | log.debug("Adding {} my-ip-rules in sw {}", msglist.size(), getStringId()); |
| 788 | } |
| 789 | |
| 790 | private void populateMySubnets() throws IOException { |
| 791 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 792 | // next prefix-based subnet-IP's configured on my interfaces |
| 793 | // need to ARP for exact-IP, so write-action instruction to send to |
| 794 | // controller |
| 795 | // this has different mask and priority than earlier case |
| 796 | List<String> subnetIps = getMySubnetIps(); |
| 797 | for (int i = 0; i < subnetIps.size(); i++) { |
| 798 | OFOxmEthType ethTypeIp = factory.oxms() |
| 799 | .ethType(EthType.IPv4); |
| 800 | OFOxmIpv4DstMasked ipPrefix = factory.oxms().ipv4DstMasked( |
| 801 | IPv4Address.of(subnetIps.get(i)), |
| 802 | IPv4Address.of(0xffffff00)); // '/24' mask |
| 803 | OFOxmList oxmListSlash24 = OFOxmList.of(ethTypeIp, ipPrefix); |
| 804 | OFMatchV3 match = factory.buildMatchV3() |
| 805 | .setOxmList(oxmListSlash24).build(); |
| 806 | OFAction outc = factory.actions().buildOutput() |
| 807 | .setPort(OFPort.CONTROLLER).setMaxLen(OFPCML_NO_BUFFER) |
| 808 | .build(); |
| 809 | OFInstruction writeInstr = factory.instructions().buildWriteActions() |
| 810 | .setActions(Collections.singletonList(outc)).build(); |
| 811 | OFInstruction gotoInstr = factory.instructions().buildGotoTable() |
| 812 | .setTableId(TableId.of(TABLE_ACL)).build(); |
| 813 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 814 | instructions.add(writeInstr); |
| 815 | instructions.add(gotoInstr); |
| 816 | OFMessage myIpEntry = factory.buildFlowAdd() |
| 817 | .setTableId(TableId.of(TABLE_IPV4_UNICAST)) |
| 818 | .setMatch(match) |
| 819 | .setInstructions(instructions) |
| 820 | .setPriority(SLASH_24_PRIORITY) |
| 821 | .setBufferId(OFBufferId.NO_BUFFER) |
| 822 | .setIdleTimeout(0) |
| 823 | .setHardTimeout(0) |
| 824 | .setXid(getNextTransactionId()) |
| 825 | .build(); |
| 826 | msglist.add(myIpEntry); |
| 827 | } |
| 828 | sendMsg(msglist); |
| 829 | log.debug("Adding {} subnet-ip-rules in sw {}", msglist.size(), getStringId()); |
| 830 | msglist.clear(); |
| 831 | } |
| 832 | |
| 833 | private void populateRoutes() throws IOException { |
| 834 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 835 | // addresses where I know the next-hop's mac-address because it is a |
| 836 | // router port - so I have an L3 interface to it (and an MPLS interface) |
| 837 | List<RouteEntry> routerNextHopIps = getRouterNextHopIps(); |
| 838 | for (int i = 0; i < routerNextHopIps.size(); i++) { |
| 839 | OFOxmEthType ethTypeIp = factory.oxms() |
| 840 | .ethType(EthType.IPv4); |
| 841 | OFOxmIpv4DstMasked ipPrefix = factory.oxms() |
| 842 | .ipv4DstMasked( |
| 843 | IPv4Address.of(routerNextHopIps.get(i).prefix), |
| 844 | IPv4Address.of(routerNextHopIps.get(i).mask) |
| 845 | ); |
| 846 | OFOxmList oxmListSlash32 = OFOxmList.of(ethTypeIp, ipPrefix); |
| 847 | OFMatchV3 match = factory.buildMatchV3() |
| 848 | .setOxmList(oxmListSlash32).build(); |
| 849 | OFAction outg = factory.actions().buildGroup() |
| 850 | .setGroup(OFGroup.of(0xa0000000 | // mpls group id |
| 851 | routerNextHopIps.get(i).nextHopPort)) |
| 852 | .build(); |
| 853 | // lots of actions before forwarding to mpls group, and |
| 854 | // unfortunately |
| 855 | // they need to be apply-actions |
| 856 | |
| 857 | OFAction pushlabel = factory.actions().pushMpls(EthType.MPLS_UNICAST); |
| 858 | OFOxmMplsLabel l = factory.oxms() |
| 859 | .mplsLabel(U32.of(routerNextHopIps.get(i).label)); |
| 860 | OFAction setlabelid = factory.actions().buildSetField() |
| 861 | .setField(l).build(); |
| 862 | OFAction copyTtlOut = factory.actions().copyTtlOut(); |
| 863 | // OFAction setBos = |
| 864 | // factory.actions().buildSetField().setField(bos).build(); |
| 865 | |
| 866 | /* |
| 867 | writeActions.add(pushlabel); // need to be apply actions so can be |
| 868 | writeActions.add(copyTtlOut); // matched in pseudo-table |
| 869 | //writeActions.add(setlabelid); // bad support in cpqd |
| 870 | //writeActions.add(setBos); no support in loxigen |
| 871 | */ |
| 872 | |
| 873 | List<OFAction> applyActions = new ArrayList<OFAction>(); |
| 874 | applyActions.add(pushlabel); |
| 875 | applyActions.add(copyTtlOut); |
| 876 | OFInstruction applyInstr = factory.instructions().buildApplyActions() |
| 877 | .setActions(applyActions).build(); |
| 878 | List<OFAction> writeActions = new ArrayList<OFAction>(); |
| 879 | writeActions.add(outg); // group will decr mpls-ttl, set mac-sa/da, |
| 880 | // vlan |
| 881 | OFInstruction writeInstr = factory.instructions().buildWriteActions() |
| 882 | .setActions(writeActions).build(); |
| 883 | |
| 884 | // necessary to match in pseudo-table to overcome cpqd 1.3 flaw |
| 885 | OFInstruction writeMeta = factory.instructions().buildWriteMetadata() |
| 886 | .setMetadata(U64.of(routerNextHopIps.get(i).label)) |
| 887 | .setMetadataMask(METADATA_MASK).build(); |
| 888 | /*OFInstruction gotoInstr = factory.instructions().buildGotoTable() |
| 889 | .setTableId(TableId.of(TABLE_ACL)).build();*/ |
| 890 | OFInstruction gotoInstr = factory.instructions().buildGotoTable() |
| 891 | .setTableId(TableId.of(TABLE_META)).build(); |
| 892 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 893 | instructions.add(applyInstr); |
| 894 | // instructions.add(writeInstr);// cannot write here - causes switch |
| 895 | // to crash |
| 896 | instructions.add(writeMeta); |
| 897 | instructions.add(gotoInstr); |
| 898 | |
| 899 | int priority = -1; |
| 900 | if (routerNextHopIps.get(i).mask.equals("255.255.255.255")) { |
| 901 | priority = MAX_PRIORITY; |
| 902 | } else { |
| 903 | priority = SLASH_24_PRIORITY; |
| 904 | } |
| 905 | OFMessage myIpEntry = factory.buildFlowAdd() |
| 906 | .setTableId(TableId.of(TABLE_IPV4_UNICAST)) |
| 907 | .setMatch(match) |
| 908 | .setInstructions(instructions) |
| 909 | .setPriority(priority) |
| 910 | .setBufferId(OFBufferId.NO_BUFFER) |
| 911 | .setIdleTimeout(0) |
| 912 | .setHardTimeout(0) |
| 913 | .setXid(getNextTransactionId()) |
| 914 | .build(); |
| 915 | msglist.add(myIpEntry); |
| 916 | |
| 917 | // need to also handle psuedo-table entries to match-metadata and |
| 918 | // set mpls |
| 919 | // label-id |
| 920 | OFOxmEthType ethTypeMpls = factory.oxms() |
| 921 | .ethType(EthType.MPLS_UNICAST); |
| 922 | OFOxmMetadataMasked meta = factory.oxms() |
| 923 | .metadataMasked( |
| 924 | OFMetadata.ofRaw(routerNextHopIps.get(i).label), |
| 925 | OFMetadata.NO_MASK); |
| 926 | OFOxmList oxmListMeta = OFOxmList.of(ethTypeMpls, meta); |
| 927 | OFMatchV3 matchMeta = factory.buildMatchV3() |
| 928 | .setOxmList(oxmListMeta).build(); |
| 929 | List<OFAction> writeActions2 = new ArrayList<OFAction>(); |
| 930 | writeActions2.add(setlabelid); |
| 931 | OFAction outg2 = factory.actions().buildGroup() |
| 932 | .setGroup(OFGroup.of(routerNextHopIps.get(i).nextHopPort | |
| 933 | (192 << VLAN_ID_OFFSET))) |
| 934 | .build(); |
| 935 | writeActions2.add(outg2); |
| 936 | OFInstruction writeInstr2 = factory.instructions().buildWriteActions() |
| 937 | .setActions(writeActions2).build(); |
| 938 | OFInstruction gotoInstr2 = factory.instructions().buildGotoTable() |
| 939 | .setTableId(TableId.of(TABLE_ACL)).build(); |
| 940 | List<OFInstruction> instructions2 = new ArrayList<OFInstruction>(); |
| 941 | // unfortunately have to apply this action too |
| 942 | OFInstruction applyInstr2 = factory.instructions().buildApplyActions() |
| 943 | .setActions(writeActions2).build(); |
| 944 | instructions2.add(applyInstr2); |
| 945 | // instructions2.add(writeInstr2); |
| 946 | // instructions2.add(gotoInstr2); |
| 947 | |
| 948 | /*OFMatchV3 match3 = factory.buildMatchV3() |
| 949 | .setOxmList(OFOxmList.of(meta)).build(); |
| 950 | OFInstruction clearInstruction = factory.instructions().clearActions(); |
| 951 | List<OFInstruction> instructions3 = new ArrayList<OFInstruction>(); |
| 952 | OFAction outc = factory.actions().buildOutput() |
| 953 | .setPort(OFPort.CONTROLLER).setMaxLen(OFPCML_NO_BUFFER) |
| 954 | .build(); |
| 955 | OFInstruction writec = factory.instructions() |
| 956 | .writeActions(Collections.singletonList(outc)); |
| 957 | instructions3.add(clearInstruction); |
| 958 | instructions3.add(writec); |
| 959 | instructions3.add(gotoInstr2); */ |
| 960 | OFMessage myMetaEntry = factory.buildFlowAdd() |
| 961 | .setTableId(TableId.of(TABLE_META)) |
| 962 | .setMatch(matchMeta) |
| 963 | .setInstructions(instructions2) |
| 964 | .setPriority(MAX_PRIORITY) |
| 965 | .setBufferId(OFBufferId.NO_BUFFER) |
| 966 | .setIdleTimeout(0) |
| 967 | .setHardTimeout(0) |
| 968 | .setXid(getNextTransactionId()) |
| 969 | .build(); |
| 970 | msglist.add(myMetaEntry); |
| 971 | |
| 972 | } |
| 973 | sendMsg(msglist); |
| 974 | log.debug("Adding {} next-hop-router-rules in sw {}", msglist.size(), |
| 975 | getStringId()); |
| 976 | |
| 977 | // add a table-miss entry to table 4 for debugging - leave it out |
| 978 | // unclear packet state - causes switch to crash |
| 979 | // populateTableMissEntry(TABLE_META, false, true, |
| 980 | // true, TABLE_ACL); |
| 981 | } |
| 982 | |
| 983 | private void populateHostRoutes() throws IOException { |
| 984 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 985 | // addresses where I know the next hop's mac-address and I can set the |
| 986 | // destination mac in the match-instruction.write-action |
| 987 | // either I sent out arp-request or I got an arp-request from this host |
| 988 | List<RouteEntry> hostNextHopIps = getHostNextHopIps(); |
| 989 | for (int i = 0; i < hostNextHopIps.size(); i++) { |
| 990 | OFOxmEthType ethTypeIp = factory.oxms() |
| 991 | .ethType(EthType.IPv4); |
| 992 | OFOxmIpv4DstMasked ipPrefix = factory.oxms() |
| 993 | .ipv4DstMasked( |
| 994 | IPv4Address.of(hostNextHopIps.get(i).prefix), |
| 995 | IPv4Address.NO_MASK); // host addr should be /32 |
| 996 | OFOxmList oxmListSlash32 = OFOxmList.of(ethTypeIp, ipPrefix); |
| 997 | OFMatchV3 match = factory.buildMatchV3() |
| 998 | .setOxmList(oxmListSlash32).build(); |
| 999 | OFAction setDmac = null, outg = null; |
| 1000 | OFOxmEthDst dmac = factory.oxms() |
| 1001 | .ethDst(MacAddress.of(hostNextHopIps.get(i).dstMac)); |
| 1002 | setDmac = factory.actions().buildSetField() |
| 1003 | .setField(dmac).build(); |
| 1004 | outg = factory.actions().buildGroup() |
| 1005 | .setGroup(OFGroup.of(0x20000000 | hostNextHopIps.get(i).nextHopPort)) // l3group |
| 1006 | // id |
| 1007 | .build(); |
| 1008 | List<OFAction> writeActions = new ArrayList<OFAction>(); |
| 1009 | writeActions.add(setDmac); |
| 1010 | writeActions.add(outg); |
| 1011 | OFInstruction writeInstr = factory.instructions().buildWriteActions() |
| 1012 | .setActions(writeActions).build(); |
| 1013 | OFInstruction gotoInstr = factory.instructions().buildGotoTable() |
| 1014 | .setTableId(TableId.of(TABLE_ACL)).build(); |
| 1015 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 1016 | instructions.add(writeInstr); |
| 1017 | instructions.add(gotoInstr); |
| 1018 | OFMessage myIpEntry = factory.buildFlowAdd() |
| 1019 | .setTableId(TableId.of(TABLE_IPV4_UNICAST)) |
| 1020 | .setMatch(match) |
| 1021 | .setInstructions(instructions) |
| 1022 | .setPriority(MAX_PRIORITY) // highest priority for exact |
| 1023 | // match |
| 1024 | .setBufferId(OFBufferId.NO_BUFFER) |
| 1025 | .setIdleTimeout(0) |
| 1026 | .setHardTimeout(0) |
| 1027 | .setXid(getNextTransactionId()) |
| 1028 | .build(); |
| 1029 | msglist.add(myIpEntry); |
| 1030 | } |
| 1031 | sendMsg(msglist); |
| 1032 | log.debug("Adding {} next-hop-host-rules in sw {}", msglist.size(), getStringId()); |
| 1033 | } |
| 1034 | |
| 1035 | private static class MplsEntry { |
| 1036 | int labelid; |
| 1037 | int portnum; |
| 1038 | |
| 1039 | public MplsEntry(int labelid, int portnum) { |
| 1040 | this.labelid = labelid; |
| 1041 | this.portnum = portnum; |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | private List<MplsEntry> getMplsEntries() { |
| 1046 | List<MplsEntry> myLabels = new ArrayList<MplsEntry>(); |
| 1047 | if (getId() == 0x1) { |
| 1048 | myLabels.add(new MplsEntry(101, OFPort.CONTROLLER.getPortNumber())); |
| 1049 | myLabels.add(new MplsEntry(103, 6)); |
| 1050 | } |
| 1051 | if (getId() == 0x2) { |
| 1052 | myLabels.add(new MplsEntry(103, 2)); |
| 1053 | myLabels.add(new MplsEntry(102, OFPort.CONTROLLER.getPortNumber())); |
| 1054 | myLabels.add(new MplsEntry(101, 1)); |
| 1055 | } |
| 1056 | if (getId() == 0x3) { |
| 1057 | myLabels.add(new MplsEntry(103, OFPort.CONTROLLER.getPortNumber())); |
| 1058 | myLabels.add(new MplsEntry(101, 2)); |
| 1059 | } |
| 1060 | return myLabels; |
| 1061 | } |
| 1062 | |
| 1063 | private void populateMplsTable() throws IOException { |
| 1064 | List<OFMessage> msglist = new ArrayList<OFMessage>(); |
| 1065 | List<MplsEntry> lfibEntries = getMplsEntries(); |
| 1066 | for (int i = 0; i < lfibEntries.size(); i++) { |
| 1067 | OFOxmEthType ethTypeMpls = factory.oxms() |
| 1068 | .ethType(EthType.MPLS_UNICAST); |
| 1069 | OFOxmMplsLabel labelid = factory.oxms() |
| 1070 | .mplsLabel(U32.of(lfibEntries.get(i).labelid)); |
| 1071 | OFOxmList oxmList = OFOxmList.of(ethTypeMpls, labelid); |
| 1072 | OFMatchV3 matchlabel = factory.buildMatchV3() |
| 1073 | .setOxmList(oxmList).build(); |
| 1074 | OFAction poplabel = factory.actions().popMpls(EthType.IPv4); |
| 1075 | OFAction sendTo = null; |
| 1076 | if (lfibEntries.get(i).portnum == OFPort.CONTROLLER.getPortNumber()) { |
| 1077 | sendTo = factory.actions().output(OFPort.CONTROLLER, |
| 1078 | OFPCML_NO_BUFFER); |
| 1079 | } else { |
| 1080 | sendTo = factory.actions().group(OFGroup.of( |
| 1081 | 0xa0000000 | lfibEntries.get(i).portnum)); |
| 1082 | } |
| 1083 | List<OFAction> writeActions = new ArrayList<OFAction>(); |
| 1084 | writeActions.add(poplabel); |
| 1085 | writeActions.add(sendTo); |
| 1086 | OFInstruction writeInstr = factory.instructions().buildWriteActions() |
| 1087 | .setActions(writeActions).build(); |
| 1088 | OFInstruction gotoInstr = factory.instructions().buildGotoTable() |
| 1089 | .setTableId(TableId.of(TABLE_ACL)).build(); |
| 1090 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 1091 | instructions.add(writeInstr); |
| 1092 | instructions.add(gotoInstr); |
| 1093 | OFMessage myMplsEntry = factory.buildFlowAdd() |
| 1094 | .setTableId(TableId.of(TABLE_MPLS)) |
| 1095 | .setMatch(matchlabel) |
| 1096 | .setInstructions(instructions) |
| 1097 | .setPriority(MAX_PRIORITY) // exact match and exclusive |
| 1098 | .setBufferId(OFBufferId.NO_BUFFER) |
| 1099 | .setIdleTimeout(0) |
| 1100 | .setHardTimeout(0) |
| 1101 | .setXid(getNextTransactionId()) |
| 1102 | .build(); |
| 1103 | msglist.add(myMplsEntry); |
| 1104 | } |
| 1105 | sendMsg(msglist); |
| 1106 | log.debug("Adding {} mpls-forwarding-rules in sw {}", msglist.size(), |
| 1107 | getStringId()); |
| 1108 | |
| 1109 | // match for everything else to send to ACL table. Essentially |
| 1110 | // the table miss flow entry |
| 1111 | populateTableMissEntry(TABLE_MPLS, false, true, |
| 1112 | true, TABLE_ACL); |
| 1113 | |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * By default if none of the booleans in the call are set, then the |
| 1118 | * table-miss entry is added with no instructions, which means that pipeline |
| 1119 | * execution will stop, and the action set associated with the packet will |
| 1120 | * be executed. |
| 1121 | * |
| 1122 | * @param tableToAdd |
| 1123 | * @param toControllerNow as an APPLY_ACTION instruction |
| 1124 | * @param toControllerWrite as a WRITE_ACITION instruction |
| 1125 | * @param toTable as a GOTO_TABLE instruction |
| 1126 | * @param tableToSend |
alshabib | 452234e | 2014-11-25 00:03:49 -0500 | [diff] [blame] | 1127 | * @throws java.io.IOException |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1128 | */ |
| 1129 | @SuppressWarnings("unchecked") |
| 1130 | private void populateTableMissEntry(int tableToAdd, boolean toControllerNow, |
| 1131 | boolean toControllerWrite, |
| 1132 | boolean toTable, int tableToSend) { |
| 1133 | OFOxmList oxmList = OFOxmList.EMPTY; |
| 1134 | OFMatchV3 match = factory.buildMatchV3() |
| 1135 | .setOxmList(oxmList) |
| 1136 | .build(); |
| 1137 | OFAction outc = factory.actions() |
| 1138 | .buildOutput() |
| 1139 | .setPort(OFPort.CONTROLLER) |
| 1140 | .setMaxLen(OFPCML_NO_BUFFER) |
| 1141 | .build(); |
| 1142 | List<OFInstruction> instructions = new ArrayList<OFInstruction>(); |
| 1143 | if (toControllerNow) { |
| 1144 | // table-miss instruction to send to controller immediately |
| 1145 | OFInstruction instr = factory.instructions() |
| 1146 | .buildApplyActions() |
| 1147 | .setActions(Collections.singletonList(outc)) |
| 1148 | .build(); |
| 1149 | instructions.add(instr); |
| 1150 | } |
| 1151 | |
| 1152 | if (toControllerWrite) { |
| 1153 | // table-miss instruction to write-action to send to controller |
| 1154 | // this will be executed whenever the action-set gets executed |
| 1155 | OFInstruction instr = factory.instructions() |
| 1156 | .buildWriteActions() |
| 1157 | .setActions(Collections.singletonList(outc)) |
| 1158 | .build(); |
| 1159 | instructions.add(instr); |
| 1160 | } |
| 1161 | |
| 1162 | if (toTable) { |
| 1163 | // table-miss instruction to goto-table x |
| 1164 | OFInstruction instr = factory.instructions() |
| 1165 | .gotoTable(TableId.of(tableToSend)); |
| 1166 | instructions.add(instr); |
| 1167 | } |
| 1168 | |
| 1169 | if (!toControllerNow && !toControllerWrite && !toTable) { |
| 1170 | // table-miss has no instruction - at which point action-set will be |
| 1171 | // executed - if there is an action to output/group in the action |
| 1172 | // set |
| 1173 | // the packet will be sent there, otherwise it will be dropped. |
| 1174 | instructions = Collections.EMPTY_LIST; |
| 1175 | } |
| 1176 | |
| 1177 | OFMessage tableMissEntry = factory.buildFlowAdd() |
| 1178 | .setTableId(TableId.of(tableToAdd)) |
| 1179 | .setMatch(match) // match everything |
| 1180 | .setInstructions(instructions) |
| 1181 | .setPriority(MIN_PRIORITY) |
| 1182 | .setBufferId(OFBufferId.NO_BUFFER) |
| 1183 | .setIdleTimeout(0) |
| 1184 | .setHardTimeout(0) |
| 1185 | .setXid(getNextTransactionId()) |
| 1186 | .build(); |
Jonathan Hart | 147b2ac | 2014-10-23 10:03:52 -0700 | [diff] [blame] | 1187 | |
| 1188 | write(tableMissEntry); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | private void sendBarrier(boolean finalBarrier) { |
| 1192 | int xid = getNextTransactionId(); |
| 1193 | if (finalBarrier) { |
| 1194 | barrierXidToWaitFor = xid; |
| 1195 | } |
| 1196 | OFBarrierRequest br = factory |
| 1197 | .buildBarrierRequest() |
| 1198 | .setXid(xid) |
| 1199 | .build(); |
Jonathan Hart | 147b2ac | 2014-10-23 10:03:52 -0700 | [diff] [blame] | 1200 | |
| 1201 | write(br); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | @Override |
| 1205 | public Boolean supportNxRole() { |
| 1206 | return false; |
| 1207 | } |
| 1208 | |
| 1209 | @Override |
| 1210 | public void write(OFMessage msg) { |
Jonathan Hart | 147b2ac | 2014-10-23 10:03:52 -0700 | [diff] [blame] | 1211 | this.channel.write(Collections.singletonList(msg)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1212 | |
| 1213 | } |
| 1214 | |
| 1215 | @Override |
| 1216 | public void write(List<OFMessage> msgs) { |
| 1217 | this.channel.write(msgs); |
| 1218 | } |
| 1219 | |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 1220 | |
| 1221 | @Override |
| 1222 | public TableType getTableType(TableId tid) { |
| 1223 | return TableType.NONE; // XXX this needs to be fixed |
| 1224 | } |
| 1225 | |
| 1226 | @Override |
| 1227 | public void transformAndSendMsg(OFMessage msg, TableType tableType) { |
| 1228 | // TODO Auto-generated method stub |
| 1229 | |
| 1230 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 1231 | } |