alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.openflow.drivers; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
sangho | c0b3c34 | 2015-03-12 17:12:10 -0700 | [diff] [blame] | 19 | import org.onlab.packet.Ethernet; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 20 | import org.onosproject.openflow.controller.Dpid; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 21 | import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch; |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 22 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeAlreadyStarted; |
| 23 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeCompleted; |
| 24 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeNotStarted; |
| 25 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 26 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 27 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
sangho | c0b3c34 | 2015-03-12 17:12:10 -0700 | [diff] [blame] | 28 | import org.projectfloodlight.openflow.protocol.OFMatchV3; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 29 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 30 | import org.projectfloodlight.openflow.protocol.OFType; |
| 31 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
| 32 | import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable; |
sangho | c0b3c34 | 2015-03-12 17:12:10 -0700 | [diff] [blame] | 33 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 34 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
| 35 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
| 36 | import org.projectfloodlight.openflow.types.EthType; |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 37 | import org.projectfloodlight.openflow.types.OFGroup; |
sangho | c0b3c34 | 2015-03-12 17:12:10 -0700 | [diff] [blame] | 38 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 39 | import org.projectfloodlight.openflow.types.TableId; |
| 40 | |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 41 | import java.util.ArrayList; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 42 | import java.util.Collections; |
| 43 | import java.util.List; |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 44 | import java.util.concurrent.atomic.AtomicBoolean; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Corsa switch driver for BGP Router deployment. |
| 48 | */ |
| 49 | public class OFCorsaSwitchDriver extends AbstractOpenFlowSwitch { |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 50 | |
sangho | c0b3c34 | 2015-03-12 17:12:10 -0700 | [diff] [blame] | 51 | protected static final int FIRST_TABLE = 0; |
| 52 | protected static final int VLAN_MPLS_TABLE = 1; |
| 53 | protected static final int VLAN_TABLE = 2; |
| 54 | protected static final int MPLS_TABLE = 3; |
| 55 | protected static final int ETHER_TABLE = 4; |
| 56 | protected static final int COS_MAP_TABLE = 5; |
| 57 | protected static final int FIB_TABLE = 6; |
| 58 | protected static final int LOCAL_TABLE = 9; |
| 59 | |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 60 | |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 61 | private AtomicBoolean handShakeComplete = new AtomicBoolean(false); |
| 62 | |
| 63 | private int barrierXid; |
| 64 | |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 65 | OFCorsaSwitchDriver(Dpid dpid, OFDescStatsReply desc) { |
| 66 | super(dpid); |
| 67 | |
| 68 | setSwitchDescription(desc); |
| 69 | } |
| 70 | |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 71 | /** |
| 72 | * Used by the default sendMsg to 'write' to the switch. |
| 73 | * This method is indirectly used by generic onos services like proxyarp |
| 74 | * to request packets from the default flow table. In a multi-table |
| 75 | * pipeline, these requests are redirected to the correct table. |
| 76 | * |
| 77 | * For the Corsa switch, the equivalent table is the LOCAL TABLE |
| 78 | * |
| 79 | */ |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 80 | @Override |
| 81 | public void write(OFMessage msg) { |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 82 | if (msg.getType() == OFType.FLOW_MOD) { |
| 83 | OFFlowMod flowMod = (OFFlowMod) msg; |
| 84 | OFFlowMod.Builder builder = flowMod.createBuilder(); |
| 85 | builder.setTableId(TableId.of(LOCAL_TABLE)); |
| 86 | channel.write(Collections.singletonList(builder.build())); |
| 87 | } else { |
| 88 | channel.write(Collections.singletonList(msg)); |
| 89 | } |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void write(List<OFMessage> msgs) { |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 94 | List<OFMessage> newMsgs = new ArrayList<OFMessage>(); |
| 95 | for (OFMessage msg : msgs) { |
| 96 | if (msg.getType() == OFType.FLOW_MOD) { |
| 97 | OFFlowMod flowMod = (OFFlowMod) msg; |
| 98 | OFFlowMod.Builder builder = flowMod.createBuilder(); |
| 99 | builder.setTableId(TableId.of(LOCAL_TABLE)); |
| 100 | newMsgs.add(builder.build()); |
| 101 | } else { |
| 102 | newMsgs.add(msg); |
| 103 | } |
| 104 | } |
| 105 | channel.write(newMsgs); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 109 | public void transformAndSendMsg(OFMessage msg, TableType type) { |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 110 | log.trace("Trying to send {} of TableType {}", msg, type); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 111 | if (msg.getType() == OFType.FLOW_MOD) { |
| 112 | OFFlowMod flowMod = (OFFlowMod) msg; |
| 113 | OFFlowMod.Builder builder = flowMod.createBuilder(); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 114 | List<OFInstruction> instructions = flowMod.getInstructions(); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 115 | List<OFInstruction> newInstructions = Lists.newArrayList(); |
| 116 | for (OFInstruction i : instructions) { |
| 117 | if (i instanceof OFInstructionGotoTable) { |
| 118 | OFInstructionGotoTable gotoTable = (OFInstructionGotoTable) i; |
| 119 | TableType tid = TableType.values()[gotoTable.getTableId().getValue()]; |
| 120 | switch (tid) { |
| 121 | case VLAN_MPLS: |
| 122 | newInstructions.add( |
| 123 | gotoTable.createBuilder() |
| 124 | .setTableId(TableId.of(VLAN_MPLS_TABLE)).build()); |
| 125 | break; |
| 126 | case VLAN: |
| 127 | newInstructions.add( |
| 128 | gotoTable.createBuilder() |
| 129 | .setTableId(TableId.of(VLAN_TABLE)).build()); |
| 130 | break; |
| 131 | case ETHER: |
| 132 | newInstructions.add( |
| 133 | gotoTable.createBuilder() |
| 134 | .setTableId(TableId.of(ETHER_TABLE)).build()); |
| 135 | break; |
| 136 | case COS: |
| 137 | newInstructions.add( |
| 138 | gotoTable.createBuilder() |
| 139 | .setTableId(TableId.of(COS_MAP_TABLE)).build()); |
| 140 | break; |
| 141 | case IP: |
| 142 | newInstructions.add( |
| 143 | gotoTable.createBuilder() |
| 144 | .setTableId(TableId.of(FIB_TABLE)).build()); |
| 145 | break; |
| 146 | case MPLS: |
| 147 | newInstructions.add( |
| 148 | gotoTable.createBuilder() |
| 149 | .setTableId(TableId.of(MPLS_TABLE)).build()); |
| 150 | break; |
| 151 | case ACL: |
| 152 | newInstructions.add( |
| 153 | gotoTable.createBuilder() |
| 154 | .setTableId(TableId.of(LOCAL_TABLE)).build()); |
| 155 | break; |
| 156 | case NONE: |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 157 | log.error("Should never have to go to Table 0"); |
| 158 | /*newInstructions.add( |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 159 | gotoTable.createBuilder() |
| 160 | .setTableId(TableId.of(0)).build()); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 161 | */ |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 162 | break; |
| 163 | default: |
| 164 | log.warn("Unknown table type: {}", tid); |
| 165 | } |
| 166 | |
| 167 | } else { |
| 168 | newInstructions.add(i); |
| 169 | } |
| 170 | } |
| 171 | switch (type) { |
| 172 | case VLAN_MPLS: |
| 173 | builder.setTableId(TableId.of(VLAN_MPLS_TABLE)); |
| 174 | break; |
| 175 | case VLAN: |
| 176 | builder.setTableId(TableId.of(VLAN_TABLE)); |
| 177 | break; |
| 178 | case ETHER: |
| 179 | builder.setTableId(TableId.of(ETHER_TABLE)); |
| 180 | break; |
| 181 | case COS: |
| 182 | builder.setTableId(TableId.of(COS_MAP_TABLE)); |
| 183 | break; |
| 184 | case IP: |
| 185 | builder.setTableId(TableId.of(FIB_TABLE)); |
| 186 | break; |
| 187 | case MPLS: |
| 188 | builder.setTableId(TableId.of(MPLS_TABLE)); |
| 189 | break; |
| 190 | case ACL: |
| 191 | builder.setTableId(TableId.of(LOCAL_TABLE)); |
| 192 | break; |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 193 | case FIRST: |
| 194 | builder.setTableId(TableId.of(FIRST_TABLE)); |
| 195 | break; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 196 | case NONE: |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 197 | builder.setTableId(TableId.of(LOCAL_TABLE)); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 198 | break; |
| 199 | default: |
| 200 | log.warn("Unknown table type: {}", type); |
| 201 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 202 | builder.setInstructions(newInstructions); |
sangho | c0b3c34 | 2015-03-12 17:12:10 -0700 | [diff] [blame] | 203 | |
| 204 | OFMatchV3 match = (OFMatchV3) flowMod.getMatch(); |
| 205 | for (OFOxm oxm: match.getOxmList()) { |
| 206 | if (oxm.getMatchField() == MatchField.VLAN_VID && |
| 207 | oxm.getValue().equals(OFVlanVidMatch.PRESENT)) { |
| 208 | Match.Builder mBuilder = factory().buildMatchV3(); |
| 209 | mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(Ethernet.TYPE_VLAN)); |
| 210 | builder.setMatch(mBuilder.build()); |
| 211 | } |
| 212 | } |
| 213 | |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 214 | OFMessage msgnew = builder.build(); |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 215 | channel.write(Collections.singletonList(msgnew)); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 216 | log.debug("Installed {}", msgnew); |
| 217 | |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 218 | } else { |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 219 | channel.write(Collections.singletonList(msg)); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public TableType getTableType(TableId tid) { |
| 225 | switch (tid.getValue()) { |
| 226 | case VLAN_MPLS_TABLE: |
| 227 | return TableType.VLAN_MPLS; |
| 228 | case VLAN_TABLE: |
| 229 | return TableType.VLAN; |
| 230 | case ETHER_TABLE: |
| 231 | return TableType.ETHER; |
| 232 | case COS_MAP_TABLE: |
| 233 | return TableType.COS; |
| 234 | case FIB_TABLE: |
| 235 | return TableType.IP; |
| 236 | case MPLS_TABLE: |
| 237 | return TableType.MPLS; |
| 238 | case LOCAL_TABLE: |
| 239 | return TableType.NONE; |
| 240 | case FIRST_TABLE: |
| 241 | return TableType.FIRST; |
| 242 | default: |
| 243 | log.warn("Unknown table type: {}", tid.getValue()); |
| 244 | return TableType.NONE; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public Boolean supportNxRole() { |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | @Override |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 254 | public void startDriverHandshake() { |
| 255 | if (startDriverHandshakeCalled) { |
| 256 | throw new SwitchDriverSubHandshakeAlreadyStarted(); |
| 257 | } |
| 258 | startDriverHandshakeCalled = true; |
| 259 | OFFlowMod fm = factory().buildFlowDelete() |
| 260 | .setTableId(TableId.ALL) |
| 261 | .setOutGroup(OFGroup.ANY) |
| 262 | .build(); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 263 | |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 264 | channel.write(Collections.singletonList(fm)); |
| 265 | |
| 266 | barrierXid = getNextTransactionId(); |
| 267 | OFBarrierRequest barrier = factory().buildBarrierRequest() |
| 268 | .setXid(barrierXid).build(); |
| 269 | |
| 270 | |
| 271 | channel.write(Collections.singletonList(barrier)); |
| 272 | |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | @Override |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 276 | public boolean isDriverHandshakeComplete() { |
| 277 | if (!startDriverHandshakeCalled) { |
| 278 | throw new SwitchDriverSubHandshakeAlreadyStarted(); |
| 279 | } |
| 280 | return handShakeComplete.get(); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void processDriverHandshakeMessage(OFMessage m) { |
| 285 | if (!startDriverHandshakeCalled) { |
| 286 | throw new SwitchDriverSubHandshakeNotStarted(); |
| 287 | } |
| 288 | if (handShakeComplete.get()) { |
| 289 | throw new SwitchDriverSubHandshakeCompleted(m); |
| 290 | } |
| 291 | if (m.getType() == OFType.BARRIER_REPLY && |
| 292 | m.getXid() == barrierXid) { |
| 293 | handShakeComplete.set(true); |
| 294 | } |
| 295 | } |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 296 | } |