sangho | 11c30ac | 2015-01-22 14:30:55 -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 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 19 | import org.onosproject.openflow.controller.Dpid; |
| 20 | import org.onosproject.openflow.controller.driver.AbstractOpenFlowSwitch; |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 21 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeAlreadyStarted; |
| 22 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeCompleted; |
| 23 | import org.onosproject.openflow.controller.driver.SwitchDriverSubHandshakeNotStarted; |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 24 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 25 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 26 | import org.projectfloodlight.openflow.protocol.OFFactory; |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 27 | import org.projectfloodlight.openflow.protocol.OFMessage; |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 28 | import org.projectfloodlight.openflow.protocol.OFType; |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 29 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 30 | import org.projectfloodlight.openflow.protocol.instruction.OFInstructionGotoTable; |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 31 | import org.projectfloodlight.openflow.types.TableId; |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 32 | |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 33 | import java.util.Collections; |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 34 | import java.util.List; |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 35 | import java.util.concurrent.atomic.AtomicBoolean; |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 36 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 37 | //TODO: Knock-off this class as we don't need any switch/app specific |
| 38 | //drivers in the south bound layers. |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 39 | public class OFSwitchImplSpringOpenTTP extends AbstractOpenFlowSwitch { |
| 40 | |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 41 | private OFFactory factory; |
| 42 | |
| 43 | private final AtomicBoolean driverHandshakeComplete; |
| 44 | private AtomicBoolean haltStateMachine; |
| 45 | |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 46 | /* Default table ID - compatible with CpqD switch */ |
| 47 | private static final int TABLE_VLAN = 0; |
| 48 | private static final int TABLE_TMAC = 1; |
| 49 | private static final int TABLE_IPV4_UNICAST = 2; |
| 50 | private static final int TABLE_MPLS = 3; |
| 51 | private static final int TABLE_ACL = 5; |
| 52 | |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 53 | /* |
| 54 | * Set the default values. These variables will get overwritten based on the |
| 55 | * switch vendor type |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 56 | */ |
| 57 | protected int vlanTableId = TABLE_VLAN; |
| 58 | protected int tmacTableId = TABLE_TMAC; |
| 59 | protected int ipv4UnicastTableId = TABLE_IPV4_UNICAST; |
| 60 | protected int mplsTableId = TABLE_MPLS; |
| 61 | protected int aclTableId = TABLE_ACL; |
| 62 | |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 63 | protected OFSwitchImplSpringOpenTTP(Dpid dpid, OFDescStatsReply desc) { |
| 64 | super(dpid); |
| 65 | driverHandshakeComplete = new AtomicBoolean(false); |
| 66 | haltStateMachine = new AtomicBoolean(false); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 67 | setSwitchDescription(desc); |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 68 | } |
| 69 | |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 70 | @Override |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 71 | public String toString() { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 72 | return "OFSwitchImplSpringOpenTTP [" |
| 73 | + ((channel != null) ? channel.getRemoteAddress() : "?") |
| 74 | + " DPID[" |
| 75 | + ((this.getStringId() != null) ? this.getStringId() : "?") |
| 76 | + "]]"; |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public Boolean supportNxRole() { |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void startDriverHandshake() { |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 86 | log.debug("Starting driver handshake for sw {}", getStringId()); |
| 87 | if (startDriverHandshakeCalled) { |
| 88 | throw new SwitchDriverSubHandshakeAlreadyStarted(); |
| 89 | } |
| 90 | startDriverHandshakeCalled = true; |
| 91 | factory = this.factory(); |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 92 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 93 | driverHandshakeComplete.set(true); |
| 94 | log.debug("Driver handshake is complete"); |
| 95 | |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public boolean isDriverHandshakeComplete() { |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 100 | if (!startDriverHandshakeCalled) { |
| 101 | throw new SwitchDriverSubHandshakeNotStarted(); |
| 102 | } |
| 103 | return driverHandshakeComplete.get(); |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void processDriverHandshakeMessage(OFMessage m) { |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 108 | if (!startDriverHandshakeCalled) { |
| 109 | throw new SwitchDriverSubHandshakeNotStarted(); |
| 110 | } |
| 111 | if (driverHandshakeComplete.get()) { |
| 112 | throw new SwitchDriverSubHandshakeCompleted(m); |
| 113 | } |
sangho | 11c30ac | 2015-01-22 14:30:55 -0800 | [diff] [blame] | 114 | } |
| 115 | |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 116 | @Override |
| 117 | public void write(OFMessage msg) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 118 | channel.write(Collections.singletonList(msg)); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void write(List<OFMessage> msgs) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 123 | channel.write(msgs); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 127 | public void transformAndSendMsg(OFMessage msg, TableType type) { |
| 128 | if (msg.getType() == OFType.FLOW_MOD) { |
| 129 | OFFlowMod flowMod = (OFFlowMod) msg; |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 130 | OFFlowMod.Builder builder = flowMod.createBuilder(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 131 | List<OFInstruction> instructions = flowMod.getInstructions(); |
| 132 | List<OFInstruction> newInstructions = Lists.newArrayList(); |
| 133 | for (OFInstruction i : instructions) { |
| 134 | if (i instanceof OFInstructionGotoTable) { |
| 135 | OFInstructionGotoTable gotoTable = (OFInstructionGotoTable) i; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 136 | TableType tid = TableType.values()[gotoTable.getTableId() |
| 137 | .getValue()]; |
| 138 | newInstructions.add(gotoTable.createBuilder() |
| 139 | .setTableId(getTableId(tid)).build()); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 140 | } else { |
| 141 | newInstructions.add(i); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 142 | } |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 143 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 144 | builder.setTableId(getTableId(type)); |
| 145 | builder.setInstructions(newInstructions); |
| 146 | OFMessage msgnew = builder.build(); |
| 147 | channel.write(Collections.singletonList(msgnew)); |
| 148 | log.trace("Installed {}", msgnew); |
| 149 | |
| 150 | } else { |
| 151 | channel.write(Collections.singletonList(msg)); |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 152 | } |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 153 | } |
| 154 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public TableType getTableType(TableId tid) { |
| 157 | switch (tid.getValue()) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 158 | case TABLE_IPV4_UNICAST: |
| 159 | return TableType.IP; |
| 160 | case TABLE_MPLS: |
| 161 | return TableType.MPLS; |
| 162 | case TABLE_ACL: |
| 163 | return TableType.ACL; |
| 164 | case TABLE_VLAN: |
| 165 | return TableType.VLAN; |
| 166 | case TABLE_TMAC: |
| 167 | return TableType.ETHER; |
| 168 | default: |
| 169 | log.error("Table type for Table id {} is not supported in the driver", |
| 170 | tid); |
| 171 | return TableType.NONE; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 172 | } |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private TableId getTableId(TableType tableType) { |
| 176 | switch (tableType) { |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 177 | case IP: |
| 178 | return TableId.of(ipv4UnicastTableId); |
| 179 | case MPLS: |
| 180 | return TableId.of(mplsTableId); |
| 181 | case ACL: |
| 182 | return TableId.of(aclTableId); |
| 183 | case VLAN: |
| 184 | return TableId.of(vlanTableId); |
| 185 | case ETHER: |
| 186 | return TableId.of(tmacTableId); |
| 187 | default: { |
| 188 | log.error("Table type {} is not supported in the driver", tableType); |
| 189 | return TableId.NONE; |
| 190 | } |
sangho | 87af811 | 2015-01-29 12:53:08 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Ray Milkey | 51365f3 | 2015-02-04 11:24:22 -0800 | [diff] [blame] | 194 | } |