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