Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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.provider.of.flow.impl; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Jonathan Hart | d0ef752 | 2014-12-11 11:51:18 -0800 | [diff] [blame] | 19 | import org.onlab.packet.Ip4Address; |
| 20 | import org.onlab.packet.Ip4Prefix; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 21 | import org.onlab.packet.Ip6Address; |
Charles M.C. Chan | 52fae7d | 2015-01-17 00:35:53 +0800 | [diff] [blame] | 22 | import org.onlab.packet.Ip6Prefix; |
Jonathan Hart | d0ef752 | 2014-12-11 11:51:18 -0800 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 24 | import org.onlab.packet.MplsLabel; |
Jonathan Hart | d0ef752 | 2014-12-11 11:51:18 -0800 | [diff] [blame] | 25 | import org.onlab.packet.VlanId; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 26 | import org.onosproject.core.DefaultGroupId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 30 | import org.onosproject.net.flow.DefaultFlowRule; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 33 | import org.onosproject.net.flow.FlowEntry; |
| 34 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
| 35 | import org.onosproject.net.flow.FlowRule; |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 36 | import org.onosproject.net.flow.FlowRule.Type; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
| 39 | import org.onosproject.openflow.controller.Dpid; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 40 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 41 | import org.projectfloodlight.openflow.protocol.OFFlowRemoved; |
| 42 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 43 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 44 | import org.projectfloodlight.openflow.protocol.action.OFActionCircuit; |
| 45 | import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 46 | import org.projectfloodlight.openflow.protocol.action.OFActionGroup; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 48 | import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst; |
| 50 | import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc; |
Jonathan Hart | d0ef752 | 2014-12-11 11:51:18 -0800 | [diff] [blame] | 51 | import org.projectfloodlight.openflow.protocol.action.OFActionSetField; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 52 | import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst; |
| 53 | import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc; |
| 54 | import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp; |
| 55 | import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 56 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
Jonathan Hart | cf78320 | 2014-11-24 18:55:42 -0800 | [diff] [blame] | 57 | import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 58 | import org.projectfloodlight.openflow.protocol.instruction.OFInstructionWriteActions; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 60 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
Jonathan Hart | d0ef752 | 2014-12-11 11:51:18 -0800 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 63 | import org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13; |
sangho | c0b3c34 | 2015-03-12 17:12:10 -0700 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.types.EthType; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 65 | import org.projectfloodlight.openflow.types.IPv4Address; |
Charles M.C. Chan | 52fae7d | 2015-01-17 00:35:53 +0800 | [diff] [blame] | 66 | import org.projectfloodlight.openflow.types.IPv6Address; |
Jonathan Hart | 0e12fad | 2014-10-17 14:54:58 -0700 | [diff] [blame] | 67 | import org.projectfloodlight.openflow.types.Masked; |
Jonathan Hart | d0ef752 | 2014-12-11 11:51:18 -0800 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 69 | import org.projectfloodlight.openflow.types.U32; |
Jonathan Hart | d0ef752 | 2014-12-11 11:51:18 -0800 | [diff] [blame] | 70 | import org.projectfloodlight.openflow.types.VlanPcp; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 71 | import org.slf4j.Logger; |
| 72 | |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 73 | import java.util.List; |
| 74 | |
| 75 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 76 | |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 77 | public class FlowEntryBuilder { |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 78 | private final Logger log = getLogger(getClass()); |
| 79 | |
| 80 | private final OFFlowStatsEntry stat; |
| 81 | private final OFFlowRemoved removed; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 82 | private final OFFlowMod flowMod; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 83 | |
| 84 | private final Match match; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 85 | |
Jonathan Hart | 67fc097 | 2015-03-19 15:21:20 -0700 | [diff] [blame] | 86 | // All actions are contained in an OFInstruction. For OF1.0 |
| 87 | // the instruction type is apply instruction (immediate set in ONOS speak) |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 88 | private final List<OFInstruction> instructions; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 89 | |
| 90 | private final Dpid dpid; |
| 91 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 92 | public enum FlowType { STAT, REMOVED, MOD } |
| 93 | |
| 94 | private final FlowType type; |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 95 | private Type tableType = FlowRule.Type.DEFAULT; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 96 | |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 97 | public FlowEntryBuilder(Dpid dpid, OFFlowStatsEntry entry, Type tableType) { |
| 98 | this.stat = entry; |
| 99 | this.match = entry.getMatch(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 100 | this.instructions = getInstructions(entry); |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 101 | this.dpid = dpid; |
| 102 | this.removed = null; |
| 103 | this.flowMod = null; |
| 104 | this.type = FlowType.STAT; |
| 105 | this.tableType = tableType; |
| 106 | } |
| 107 | |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 108 | public FlowEntryBuilder(Dpid dpid, OFFlowRemoved removed, Type tableType) { |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 109 | this.match = removed.getMatch(); |
| 110 | this.removed = removed; |
| 111 | |
| 112 | this.dpid = dpid; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 113 | this.instructions = null; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 114 | this.stat = null; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 115 | this.flowMod = null; |
| 116 | this.type = FlowType.REMOVED; |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 117 | this.tableType = tableType; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 118 | |
| 119 | } |
| 120 | |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 121 | public FlowEntryBuilder(Dpid dpid, OFFlowMod fm, Type tableType) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 122 | this.match = fm.getMatch(); |
| 123 | this.dpid = dpid; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 124 | this.instructions = getInstructions(fm); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 125 | this.type = FlowType.MOD; |
| 126 | this.flowMod = fm; |
| 127 | this.stat = null; |
| 128 | this.removed = null; |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 129 | this.tableType = tableType; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 130 | } |
alshabib | 1c319ff | 2014-10-04 20:29:09 -0700 | [diff] [blame] | 131 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 132 | public FlowEntry build(FlowEntryState... state) { |
| 133 | FlowRule rule; |
| 134 | switch (this.type) { |
| 135 | case STAT: |
| 136 | rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)), |
| 137 | buildSelector(), buildTreatment(), stat.getPriority(), |
Saurav Das | fa2fa93 | 2015-03-03 11:29:48 -0800 | [diff] [blame] | 138 | stat.getCookie().getValue(), stat.getIdleTimeout(), false, |
| 139 | tableType); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 140 | return new DefaultFlowEntry(rule, FlowEntryState.ADDED, |
| 141 | stat.getDurationSec(), stat.getPacketCount().getValue(), |
| 142 | stat.getByteCount().getValue()); |
| 143 | case REMOVED: |
| 144 | rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)), |
| 145 | buildSelector(), null, removed.getPriority(), |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 146 | removed.getCookie().getValue(), removed.getIdleTimeout(), false, |
| 147 | tableType); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 148 | return new DefaultFlowEntry(rule, FlowEntryState.REMOVED, removed.getDurationSec(), |
| 149 | removed.getPacketCount().getValue(), removed.getByteCount().getValue()); |
| 150 | case MOD: |
| 151 | FlowEntryState flowState = state.length > 0 ? state[0] : FlowEntryState.FAILED; |
| 152 | rule = new DefaultFlowRule(DeviceId.deviceId(Dpid.uri(dpid)), |
| 153 | buildSelector(), buildTreatment(), flowMod.getPriority(), |
alshabib | da1644e | 2015-03-13 14:01:35 -0700 | [diff] [blame] | 154 | flowMod.getCookie().getValue(), flowMod.getIdleTimeout(), false, |
| 155 | tableType); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 156 | return new DefaultFlowEntry(rule, flowState, 0, 0, 0); |
| 157 | default: |
| 158 | log.error("Unknown flow type : {}", this.type); |
| 159 | return null; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 160 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 161 | |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 162 | } |
| 163 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 164 | private List<OFInstruction> getInstructions(OFFlowMod entry) { |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 165 | switch (entry.getVersion()) { |
| 166 | case OF_10: |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 167 | return Lists.newArrayList(OFFactoryVer13.INSTANCE.instructions() |
| 168 | .applyActions( |
| 169 | entry.getActions())); |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 170 | case OF_11: |
| 171 | case OF_12: |
| 172 | case OF_13: |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 173 | return entry.getInstructions(); |
| 174 | default: |
| 175 | log.warn("Unknown OF version {}", entry.getVersion()); |
| 176 | } |
| 177 | return Lists.newLinkedList(); |
| 178 | } |
| 179 | |
| 180 | private List<OFInstruction> getInstructions(OFFlowStatsEntry entry) { |
| 181 | switch (entry.getVersion()) { |
| 182 | case OF_10: |
| 183 | return Lists.newArrayList( |
| 184 | OFFactoryVer13.INSTANCE.instructions().applyActions(entry.getActions())); |
| 185 | case OF_11: |
| 186 | case OF_12: |
| 187 | case OF_13: |
| 188 | return entry.getInstructions(); |
alshabib | 19fdc12 | 2014-10-03 11:38:19 -0700 | [diff] [blame] | 189 | default: |
| 190 | log.warn("Unknown OF version {}", entry.getVersion()); |
| 191 | } |
| 192 | return Lists.newLinkedList(); |
| 193 | } |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 194 | |
| 195 | private TrafficTreatment buildTreatment() { |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 196 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 197 | // If this is a drop rule |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 198 | if (instructions.size() == 0) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 199 | builder.drop(); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 200 | return builder.build(); |
| 201 | } |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 202 | for (OFInstruction in : instructions) { |
| 203 | switch (in.getType()) { |
| 204 | case GOTO_TABLE: |
| 205 | builder.transition(tableType); |
| 206 | break; |
| 207 | case WRITE_METADATA: |
| 208 | break; |
| 209 | case WRITE_ACTIONS: |
| 210 | builder.deferred(); |
| 211 | buildActions(((OFInstructionWriteActions) in).getActions(), |
| 212 | builder); |
| 213 | break; |
| 214 | case APPLY_ACTIONS: |
| 215 | builder.immediate(); |
| 216 | buildActions(((OFInstructionApplyActions) in).getActions(), |
| 217 | builder); |
| 218 | break; |
| 219 | case CLEAR_ACTIONS: |
| 220 | builder.wipeDeferred(); |
| 221 | break; |
| 222 | case EXPERIMENTER: |
| 223 | break; |
| 224 | case METER: |
| 225 | break; |
| 226 | default: |
| 227 | log.warn("Unknown instructions type {}", in.getType()); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | return builder.build(); |
| 232 | } |
| 233 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 234 | private TrafficTreatment.Builder buildActions(List<OFAction> actions, |
| 235 | TrafficTreatment.Builder builder) { |
| 236 | for (OFAction act : actions) { |
| 237 | switch (act.getType()) { |
| 238 | case OUTPUT: |
| 239 | OFActionOutput out = (OFActionOutput) act; |
| 240 | builder.setOutput( |
| 241 | PortNumber.portNumber(out.getPort().getPortNumber())); |
| 242 | break; |
| 243 | case SET_VLAN_VID: |
| 244 | OFActionSetVlanVid vlan = (OFActionSetVlanVid) act; |
| 245 | builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan())); |
| 246 | break; |
| 247 | case SET_VLAN_PCP: |
| 248 | OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act; |
| 249 | builder.setVlanPcp(pcp.getVlanPcp().getValue()); |
| 250 | break; |
| 251 | case SET_DL_DST: |
| 252 | OFActionSetDlDst dldst = (OFActionSetDlDst) act; |
| 253 | builder.setEthDst( |
| 254 | MacAddress.valueOf(dldst.getDlAddr().getLong())); |
| 255 | break; |
| 256 | case SET_DL_SRC: |
| 257 | OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act; |
| 258 | builder.setEthSrc( |
| 259 | MacAddress.valueOf(dlsrc.getDlAddr().getLong())); |
| 260 | |
| 261 | break; |
| 262 | case SET_NW_DST: |
| 263 | OFActionSetNwDst nwdst = (OFActionSetNwDst) act; |
| 264 | IPv4Address di = nwdst.getNwAddr(); |
| 265 | builder.setIpDst(Ip4Address.valueOf(di.getInt())); |
| 266 | break; |
| 267 | case SET_NW_SRC: |
| 268 | OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act; |
| 269 | IPv4Address si = nwsrc.getNwAddr(); |
| 270 | builder.setIpSrc(Ip4Address.valueOf(si.getInt())); |
| 271 | break; |
| 272 | case EXPERIMENTER: |
| 273 | OFActionExperimenter exp = (OFActionExperimenter) act; |
| 274 | if (exp.getExperimenter() == 0x80005A06 || |
| 275 | exp.getExperimenter() == 0x748771) { |
| 276 | OFActionCircuit ct = (OFActionCircuit) exp; |
| 277 | builder.setLambda(((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber()); |
| 278 | } else { |
| 279 | log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter()); |
| 280 | } |
| 281 | break; |
| 282 | case SET_FIELD: |
| 283 | OFActionSetField setField = (OFActionSetField) act; |
| 284 | handleSetField(builder, setField.getField()); |
| 285 | break; |
| 286 | case POP_MPLS: |
| 287 | OFActionPopMpls popMpls = (OFActionPopMpls) act; |
| 288 | builder.popMpls((short) popMpls.getEthertype().getValue()); |
| 289 | break; |
| 290 | case PUSH_MPLS: |
| 291 | builder.pushMpls(); |
| 292 | break; |
| 293 | case COPY_TTL_IN: |
| 294 | builder.copyTtlIn(); |
| 295 | break; |
| 296 | case COPY_TTL_OUT: |
| 297 | builder.copyTtlOut(); |
| 298 | break; |
| 299 | case DEC_MPLS_TTL: |
| 300 | builder.decMplsTtl(); |
| 301 | break; |
| 302 | case DEC_NW_TTL: |
| 303 | builder.decNwTtl(); |
| 304 | break; |
| 305 | case GROUP: |
| 306 | OFActionGroup group = (OFActionGroup) act; |
| 307 | builder.group(new DefaultGroupId(group.getGroup().getGroupNumber())); |
| 308 | break; |
Jonathan Hart | 67fc097 | 2015-03-19 15:21:20 -0700 | [diff] [blame] | 309 | case STRIP_VLAN: |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 310 | case POP_VLAN: |
| 311 | builder.popVlan(); |
| 312 | break; |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 313 | case PUSH_VLAN: |
| 314 | builder.pushVlan(); |
| 315 | break; |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 316 | case SET_TP_DST: |
| 317 | case SET_TP_SRC: |
| 318 | case POP_PBB: |
| 319 | case PUSH_PBB: |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 320 | case SET_MPLS_LABEL: |
| 321 | case SET_MPLS_TC: |
| 322 | case SET_MPLS_TTL: |
| 323 | case SET_NW_ECN: |
| 324 | case SET_NW_TOS: |
| 325 | case SET_NW_TTL: |
| 326 | case SET_QUEUE: |
| 327 | |
| 328 | case ENQUEUE: |
| 329 | default: |
| 330 | log.warn("Action type {} not yet implemented.", act.getType()); |
| 331 | } |
| 332 | } |
| 333 | return builder; |
| 334 | } |
| 335 | |
| 336 | |
Jonathan Hart | d0ef752 | 2014-12-11 11:51:18 -0800 | [diff] [blame] | 337 | private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) { |
| 338 | switch (oxm.getMatchField().id) { |
| 339 | case VLAN_PCP: |
| 340 | @SuppressWarnings("unchecked") |
| 341 | OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm; |
| 342 | builder.setVlanPcp(vlanpcp.getValue().getValue()); |
| 343 | break; |
| 344 | case VLAN_VID: |
| 345 | @SuppressWarnings("unchecked") |
| 346 | OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm; |
| 347 | builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan())); |
| 348 | break; |
| 349 | case ETH_DST: |
| 350 | @SuppressWarnings("unchecked") |
| 351 | OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst = |
| 352 | (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm; |
| 353 | builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong())); |
| 354 | break; |
| 355 | case ETH_SRC: |
| 356 | @SuppressWarnings("unchecked") |
| 357 | OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc = |
| 358 | (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm; |
| 359 | builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong())); |
| 360 | break; |
| 361 | case IPV4_DST: |
| 362 | @SuppressWarnings("unchecked") |
| 363 | OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm; |
| 364 | builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt())); |
| 365 | break; |
| 366 | case IPV4_SRC: |
| 367 | @SuppressWarnings("unchecked") |
| 368 | OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm; |
| 369 | builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt())); |
| 370 | break; |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 371 | case MPLS_LABEL: |
| 372 | @SuppressWarnings("unchecked") |
| 373 | OFOxm<U32> labelId = (OFOxm<U32>) oxm; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 374 | builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue())); |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 375 | break; |
Jonathan Hart | d0ef752 | 2014-12-11 11:51:18 -0800 | [diff] [blame] | 376 | case ARP_OP: |
| 377 | case ARP_SHA: |
| 378 | case ARP_SPA: |
| 379 | case ARP_THA: |
| 380 | case ARP_TPA: |
| 381 | case BSN_EGR_PORT_GROUP_ID: |
| 382 | case BSN_GLOBAL_VRF_ALLOWED: |
| 383 | case BSN_IN_PORTS_128: |
| 384 | case BSN_L3_DST_CLASS_ID: |
| 385 | case BSN_L3_INTERFACE_CLASS_ID: |
| 386 | case BSN_L3_SRC_CLASS_ID: |
| 387 | case BSN_LAG_ID: |
| 388 | case BSN_TCP_FLAGS: |
| 389 | case BSN_UDF0: |
| 390 | case BSN_UDF1: |
| 391 | case BSN_UDF2: |
| 392 | case BSN_UDF3: |
| 393 | case BSN_UDF4: |
| 394 | case BSN_UDF5: |
| 395 | case BSN_UDF6: |
| 396 | case BSN_UDF7: |
| 397 | case BSN_VLAN_XLATE_PORT_GROUP_ID: |
| 398 | case BSN_VRF: |
| 399 | case ETH_TYPE: |
| 400 | case ICMPV4_CODE: |
| 401 | case ICMPV4_TYPE: |
| 402 | case ICMPV6_CODE: |
| 403 | case ICMPV6_TYPE: |
| 404 | case IN_PHY_PORT: |
| 405 | case IN_PORT: |
| 406 | case IPV6_DST: |
| 407 | case IPV6_FLABEL: |
| 408 | case IPV6_ND_SLL: |
| 409 | case IPV6_ND_TARGET: |
| 410 | case IPV6_ND_TLL: |
| 411 | case IPV6_SRC: |
| 412 | case IP_DSCP: |
| 413 | case IP_ECN: |
| 414 | case IP_PROTO: |
| 415 | case METADATA: |
Jonathan Hart | d0ef752 | 2014-12-11 11:51:18 -0800 | [diff] [blame] | 416 | case MPLS_TC: |
| 417 | case OCH_SIGID: |
| 418 | case OCH_SIGID_BASIC: |
| 419 | case OCH_SIGTYPE: |
| 420 | case OCH_SIGTYPE_BASIC: |
| 421 | case SCTP_DST: |
| 422 | case SCTP_SRC: |
| 423 | case TCP_DST: |
| 424 | case TCP_SRC: |
| 425 | case TUNNEL_ID: |
| 426 | case UDP_DST: |
| 427 | case UDP_SRC: |
| 428 | default: |
| 429 | log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id); |
| 430 | break; |
| 431 | } |
| 432 | } |
| 433 | |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 434 | private TrafficSelector buildSelector() { |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 435 | MacAddress mac; |
| 436 | Ip4Prefix ip4Prefix; |
| 437 | Ip6Address ip6Address; |
| 438 | Ip6Prefix ip6Prefix; |
| 439 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 440 | TrafficSelector.Builder builder = DefaultTrafficSelector.builder(); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 441 | for (MatchField<?> field : match.getMatchFields()) { |
| 442 | switch (field.id) { |
| 443 | case IN_PORT: |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 444 | builder.matchInPort(PortNumber |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 445 | .portNumber(match.get(MatchField.IN_PORT).getPortNumber())); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 446 | break; |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 447 | case IN_PHY_PORT: |
| 448 | builder.matchInPhyPort(PortNumber |
| 449 | .portNumber(match.get(MatchField.IN_PHY_PORT).getPortNumber())); |
| 450 | break; |
| 451 | case METADATA: |
| 452 | long metadata = |
| 453 | match.get(MatchField.METADATA).getValue().getValue(); |
| 454 | builder.matchMetadata(metadata); |
| 455 | break; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 456 | case ETH_DST: |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 457 | mac = MacAddress.valueOf(match.get(MatchField.ETH_DST).getLong()); |
| 458 | builder.matchEthDst(mac); |
| 459 | break; |
| 460 | case ETH_SRC: |
| 461 | mac = MacAddress.valueOf(match.get(MatchField.ETH_SRC).getLong()); |
| 462 | builder.matchEthSrc(mac); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 463 | break; |
| 464 | case ETH_TYPE: |
| 465 | int ethType = match.get(MatchField.ETH_TYPE).getValue(); |
sangho | c0b3c34 | 2015-03-12 17:12:10 -0700 | [diff] [blame] | 466 | if (ethType == EthType.VLAN_FRAME.getValue()) { |
| 467 | builder.matchVlanId(VlanId.ANY); |
| 468 | } else { |
| 469 | builder.matchEthType((short) ethType); |
| 470 | } |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 471 | break; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 472 | case VLAN_VID: |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 473 | VlanId vlanId = null; |
| 474 | if (match.isPartiallyMasked(MatchField.VLAN_VID)) { |
| 475 | Masked<OFVlanVidMatch> masked = match.getMasked(MatchField.VLAN_VID); |
| 476 | if (masked.getValue().equals(OFVlanVidMatch.PRESENT) |
| 477 | && masked.getMask().equals(OFVlanVidMatch.PRESENT)) { |
| 478 | vlanId = VlanId.ANY; |
| 479 | } |
| 480 | } else { |
| 481 | vlanId = VlanId.vlanId(match.get(MatchField.VLAN_VID).getVlan()); |
| 482 | } |
| 483 | if (vlanId != null) { |
| 484 | builder.matchVlanId(vlanId); |
| 485 | } |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 486 | break; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 487 | case VLAN_PCP: |
| 488 | byte vlanPcp = match.get(MatchField.VLAN_PCP).getValue(); |
| 489 | builder.matchVlanPcp(vlanPcp); |
| 490 | break; |
Pavlin Radoslavov | d0fd841 | 2015-02-04 13:57:00 -0800 | [diff] [blame] | 491 | case IP_DSCP: |
| 492 | byte ipDscp = match.get(MatchField.IP_DSCP).getDscpValue(); |
| 493 | builder.matchIPDscp(ipDscp); |
| 494 | break; |
| 495 | case IP_ECN: |
| 496 | byte ipEcn = match.get(MatchField.IP_ECN).getEcnValue(); |
| 497 | builder.matchIPEcn(ipEcn); |
| 498 | break; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 499 | case IP_PROTO: |
| 500 | short proto = match.get(MatchField.IP_PROTO).getIpProtocolNumber(); |
| 501 | builder.matchIPProtocol((byte) proto); |
| 502 | break; |
| 503 | case IPV4_SRC: |
| 504 | if (match.isPartiallyMasked(MatchField.IPV4_SRC)) { |
| 505 | Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_SRC); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 506 | ip4Prefix = Ip4Prefix.valueOf( |
| 507 | maskedIp.getValue().getInt(), |
| 508 | maskedIp.getMask().asCidrMaskLength()); |
| 509 | } else { |
| 510 | ip4Prefix = Ip4Prefix.valueOf( |
| 511 | match.get(MatchField.IPV4_SRC).getInt(), |
| 512 | Ip4Prefix.MAX_MASK_LENGTH); |
| 513 | } |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 514 | builder.matchIPSrc(ip4Prefix); |
| 515 | break; |
| 516 | case IPV4_DST: |
| 517 | if (match.isPartiallyMasked(MatchField.IPV4_DST)) { |
| 518 | Masked<IPv4Address> maskedIp = match.getMasked(MatchField.IPV4_DST); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 519 | ip4Prefix = Ip4Prefix.valueOf( |
| 520 | maskedIp.getValue().getInt(), |
| 521 | maskedIp.getMask().asCidrMaskLength()); |
| 522 | } else { |
| 523 | ip4Prefix = Ip4Prefix.valueOf( |
| 524 | match.get(MatchField.IPV4_DST).getInt(), |
| 525 | Ip4Prefix.MAX_MASK_LENGTH); |
| 526 | } |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 527 | builder.matchIPDst(ip4Prefix); |
Jonathan Hart | 34bc614 | 2014-10-17 11:00:43 -0700 | [diff] [blame] | 528 | break; |
| 529 | case TCP_SRC: |
| 530 | builder.matchTcpSrc((short) match.get(MatchField.TCP_SRC).getPort()); |
| 531 | break; |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 532 | case TCP_DST: |
| 533 | builder.matchTcpDst((short) match.get(MatchField.TCP_DST).getPort()); |
| 534 | break; |
| 535 | case UDP_SRC: |
| 536 | builder.matchUdpSrc((short) match.get(MatchField.UDP_SRC).getPort()); |
| 537 | break; |
| 538 | case UDP_DST: |
| 539 | builder.matchUdpDst((short) match.get(MatchField.UDP_DST).getPort()); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 540 | case MPLS_LABEL: |
| 541 | builder.matchMplsLabel(MplsLabel.mplsLabel((int) match.get(MatchField.MPLS_LABEL) |
| 542 | .getValue())); |
Pavlin Radoslavov | 320e6c9 | 2015-02-02 16:51:58 -0800 | [diff] [blame] | 543 | break; |
| 544 | case SCTP_SRC: |
| 545 | builder.matchSctpSrc((short) match.get(MatchField.SCTP_SRC).getPort()); |
| 546 | break; |
| 547 | case SCTP_DST: |
| 548 | builder.matchSctpDst((short) match.get(MatchField.SCTP_DST).getPort()); |
| 549 | break; |
| 550 | case ICMPV4_TYPE: |
| 551 | byte icmpType = (byte) match.get(MatchField.ICMPV4_TYPE).getType(); |
| 552 | builder.matchIcmpType(icmpType); |
| 553 | break; |
| 554 | case ICMPV4_CODE: |
| 555 | byte icmpCode = (byte) match.get(MatchField.ICMPV4_CODE).getCode(); |
| 556 | builder.matchIcmpCode(icmpCode); |
| 557 | break; |
| 558 | case IPV6_SRC: |
| 559 | if (match.isPartiallyMasked(MatchField.IPV6_SRC)) { |
| 560 | Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_SRC); |
| 561 | ip6Prefix = Ip6Prefix.valueOf( |
| 562 | maskedIp.getValue().getBytes(), |
| 563 | maskedIp.getMask().asCidrMaskLength()); |
| 564 | } else { |
| 565 | ip6Prefix = Ip6Prefix.valueOf( |
| 566 | match.get(MatchField.IPV6_SRC).getBytes(), |
| 567 | Ip6Prefix.MAX_MASK_LENGTH); |
| 568 | } |
| 569 | builder.matchIPv6Src(ip6Prefix); |
| 570 | break; |
| 571 | case IPV6_DST: |
| 572 | if (match.isPartiallyMasked(MatchField.IPV6_DST)) { |
| 573 | Masked<IPv6Address> maskedIp = match.getMasked(MatchField.IPV6_DST); |
| 574 | ip6Prefix = Ip6Prefix.valueOf( |
| 575 | maskedIp.getValue().getBytes(), |
| 576 | maskedIp.getMask().asCidrMaskLength()); |
| 577 | } else { |
| 578 | ip6Prefix = Ip6Prefix.valueOf( |
| 579 | match.get(MatchField.IPV6_DST).getBytes(), |
| 580 | Ip6Prefix.MAX_MASK_LENGTH); |
| 581 | } |
| 582 | builder.matchIPv6Dst(ip6Prefix); |
| 583 | break; |
| 584 | case IPV6_FLABEL: |
| 585 | int flowLabel = |
| 586 | match.get(MatchField.IPV6_FLABEL).getIPv6FlowLabelValue(); |
| 587 | builder.matchIPv6FlowLabel(flowLabel); |
| 588 | break; |
| 589 | case ICMPV6_TYPE: |
| 590 | byte icmpv6type = (byte) match.get(MatchField.ICMPV6_TYPE).getValue(); |
| 591 | builder.matchIcmpv6Type(icmpv6type); |
| 592 | break; |
| 593 | case ICMPV6_CODE: |
| 594 | byte icmpv6code = (byte) match.get(MatchField.ICMPV6_CODE).getValue(); |
| 595 | builder.matchIcmpv6Code(icmpv6code); |
| 596 | break; |
| 597 | case IPV6_ND_TARGET: |
| 598 | ip6Address = |
| 599 | Ip6Address.valueOf(match.get(MatchField.IPV6_ND_TARGET).getBytes()); |
| 600 | builder.matchIPv6NDTargetAddress(ip6Address); |
| 601 | break; |
| 602 | case IPV6_ND_SLL: |
| 603 | mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_SLL).getLong()); |
| 604 | builder.matchIPv6NDSourceLinkLayerAddress(mac); |
| 605 | break; |
| 606 | case IPV6_ND_TLL: |
| 607 | mac = MacAddress.valueOf(match.get(MatchField.IPV6_ND_TLL).getLong()); |
| 608 | builder.matchIPv6NDTargetLinkLayerAddress(mac); |
| 609 | break; |
Pavlin Radoslavov | 5e4f754 | 2015-02-06 18:18:21 -0800 | [diff] [blame] | 610 | case IPV6_EXTHDR: |
| 611 | builder.matchIPv6ExthdrFlags((int) match.get(MatchField.IPV6_EXTHDR) |
| 612 | .getValue()); |
| 613 | break; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 614 | case OCH_SIGID: |
| 615 | builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber()); |
| 616 | break; |
Praseed Balakrishnan | 2dd5abd | 2014-11-03 14:56:28 -0800 | [diff] [blame] | 617 | case OCH_SIGTYPE: |
| 618 | builder.matchOpticalSignalType(match.get(MatchField |
| 619 | .OCH_SIGTYPE).getValue()); |
| 620 | break; |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 621 | case ARP_OP: |
| 622 | case ARP_SHA: |
| 623 | case ARP_SPA: |
| 624 | case ARP_THA: |
| 625 | case ARP_TPA: |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 626 | case MPLS_TC: |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 627 | case TUNNEL_ID: |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 628 | default: |
| 629 | log.warn("Match type {} not yet implemented.", field.id); |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | return builder.build(); |
| 633 | } |
alshabib | 6b5cfec | 2014-09-18 17:42:18 -0700 | [diff] [blame] | 634 | } |