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; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 17 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Jonathan Hart | 29afca3 | 2015-01-20 18:17:39 -0800 | [diff] [blame] | 19 | import org.onlab.packet.Ip4Address; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 20 | import org.onlab.packet.Ip6Address; |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 21 | import org.onosproject.net.OchSignal; |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 22 | import org.onosproject.net.PortNumber; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 23 | import org.onosproject.net.driver.DefaultDriverData; |
| 24 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 25 | import org.onosproject.net.driver.Driver; |
| 26 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.flow.FlowRule; |
| 28 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | 880b644 | 2015-11-23 22:13:04 -0800 | [diff] [blame] | 29 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flow.instructions.Instruction; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.instructions.Instructions; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.instructions.Instructions.GroupInstruction; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
Steffen Gebert | bbfdaaa | 2015-09-29 11:01:46 +0200 | [diff] [blame] | 34 | import org.onosproject.net.flow.instructions.Instructions.SetQueueInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
| 36 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction; |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 39 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
Saurav Das | 73a7dd4 | 2015-08-19 22:20:31 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsBosInstruction; |
Jonathan Hart | 29afca3 | 2015-01-20 18:17:39 -0800 | [diff] [blame] | 41 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 42 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
| 44 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions; |
| 46 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 47 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame^] | 48 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpIPInstruction; |
| 49 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpEthInstruction; |
| 50 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpOpInstruction; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 51 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction; |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 52 | import org.onosproject.net.flow.instructions.L4ModificationInstruction; |
| 53 | import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction; |
alshabib | 880b644 | 2015-11-23 22:13:04 -0800 | [diff] [blame] | 54 | import org.onosproject.openflow.controller.ExtensionTreatmentInterpreter; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 55 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 56 | import org.projectfloodlight.openflow.protocol.OFFlowAdd; |
| 57 | import org.projectfloodlight.openflow.protocol.OFFlowDelete; |
| 58 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 59 | import org.projectfloodlight.openflow.protocol.OFFlowModFlags; |
| 60 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 61 | import org.projectfloodlight.openflow.protocol.action.OFActionGroup; |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
Steffen Gebert | bbfdaaa | 2015-09-29 11:01:46 +0200 | [diff] [blame] | 63 | import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 65 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 66 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame^] | 67 | import org.projectfloodlight.openflow.types.ArpOpcode; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 68 | import org.projectfloodlight.openflow.types.CircuitSignalID; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 69 | import org.projectfloodlight.openflow.types.EthType; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 70 | import org.projectfloodlight.openflow.types.IPv4Address; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 71 | import org.projectfloodlight.openflow.types.IPv6Address; |
| 72 | import org.projectfloodlight.openflow.types.IPv6FlowLabel; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 73 | import org.projectfloodlight.openflow.types.MacAddress; |
Saurav Das | 73a7dd4 | 2015-08-19 22:20:31 -0700 | [diff] [blame] | 74 | import org.projectfloodlight.openflow.types.OFBooleanValue; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 75 | import org.projectfloodlight.openflow.types.OFBufferId; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 76 | import org.projectfloodlight.openflow.types.OFGroup; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 77 | import org.projectfloodlight.openflow.types.OFPort; |
| 78 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 79 | import org.projectfloodlight.openflow.types.TableId; |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 80 | import org.projectfloodlight.openflow.types.TransportPort; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 81 | import org.projectfloodlight.openflow.types.U32; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 82 | import org.projectfloodlight.openflow.types.U64; |
| 83 | import org.projectfloodlight.openflow.types.VlanPcp; |
| 84 | import org.slf4j.Logger; |
| 85 | import org.slf4j.LoggerFactory; |
| 86 | |
Jonathan Hart | 29afca3 | 2015-01-20 18:17:39 -0800 | [diff] [blame] | 87 | import java.util.Collections; |
| 88 | import java.util.LinkedList; |
| 89 | import java.util.List; |
| 90 | import java.util.Optional; |
| 91 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Flow mod builder for OpenFlow 1.3+. |
| 94 | */ |
| 95 | public class FlowModBuilderVer13 extends FlowModBuilder { |
| 96 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 97 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 98 | private static final int OFPCML_NO_BUFFER = 0xffff; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 99 | |
| 100 | private final TrafficTreatment treatment; |
| 101 | |
| 102 | /** |
| 103 | * Constructor for a flow mod builder for OpenFlow 1.3. |
| 104 | * |
| 105 | * @param flowRule the flow rule to transform into a flow mod |
| 106 | * @param factory the OpenFlow factory to use to build the flow mod |
Pavlin Radoslavov | 119fd5c | 2014-11-25 19:08:19 -0800 | [diff] [blame] | 107 | * @param xid the transaction ID |
Charles Chan | 30ba400 | 2015-11-05 14:45:16 -0800 | [diff] [blame] | 108 | * @param driverService the device driver service |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 109 | */ |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 110 | protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid, |
| 111 | Optional<DriverService> driverService) { |
| 112 | super(flowRule, factory, xid, driverService); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 113 | |
| 114 | this.treatment = flowRule.treatment(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public OFFlowAdd buildFlowAdd() { |
| 119 | Match match = buildMatch(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 120 | List<OFAction> deferredActions = buildActions(treatment.deferred()); |
| 121 | List<OFAction> immediateActions = buildActions(treatment.immediate()); |
| 122 | List<OFInstruction> instructions = Lists.newLinkedList(); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 123 | |
Jonathan Hart | d4a8bba | 2014-10-28 12:44:20 -0700 | [diff] [blame] | 124 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 125 | if (treatment.clearedDeferred()) { |
| 126 | instructions.add(factory().instructions().clearActions()); |
| 127 | } |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 128 | if (immediateActions.size() > 0) { |
| 129 | instructions.add(factory().instructions().applyActions(immediateActions)); |
| 130 | } |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 131 | if (deferredActions.size() > 0) { |
| 132 | instructions.add(factory().instructions().writeActions(deferredActions)); |
| 133 | } |
| 134 | if (treatment.tableTransition() != null) { |
| 135 | instructions.add(buildTableGoto(treatment.tableTransition())); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 136 | } |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 137 | if (treatment.writeMetadata() != null) { |
| 138 | instructions.add(buildMetadata(treatment.writeMetadata())); |
| 139 | } |
chengfan | e264bef | 2015-09-28 08:38:19 +0800 | [diff] [blame] | 140 | if (treatment.metered() != null) { |
| 141 | instructions.add(buildMeter(treatment.metered())); |
| 142 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 143 | |
| 144 | long cookie = flowRule().id().value(); |
| 145 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 146 | OFFlowAdd fm = factory().buildFlowAdd() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 147 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 148 | .setCookie(U64.of(cookie)) |
| 149 | .setBufferId(OFBufferId.NO_BUFFER) |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 150 | .setInstructions(instructions) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 151 | .setMatch(match) |
| 152 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 153 | .setPriority(flowRule().priority()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 154 | .setTableId(TableId.of(flowRule().tableId())) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 155 | .build(); |
| 156 | |
| 157 | return fm; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public OFFlowMod buildFlowMod() { |
| 162 | Match match = buildMatch(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 163 | List<OFAction> deferredActions = buildActions(treatment.deferred()); |
| 164 | List<OFAction> immediateActions = buildActions(treatment.immediate()); |
| 165 | List<OFInstruction> instructions = Lists.newLinkedList(); |
Jonathan Hart | eda3387 | 2015-02-18 10:22:32 -0800 | [diff] [blame] | 166 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 167 | |
| 168 | if (immediateActions.size() > 0) { |
| 169 | instructions.add(factory().instructions().applyActions(immediateActions)); |
| 170 | } |
| 171 | if (treatment.clearedDeferred()) { |
| 172 | instructions.add(factory().instructions().clearActions()); |
| 173 | } |
| 174 | if (deferredActions.size() > 0) { |
| 175 | instructions.add(factory().instructions().writeActions(deferredActions)); |
| 176 | } |
| 177 | if (treatment.tableTransition() != null) { |
| 178 | instructions.add(buildTableGoto(treatment.tableTransition())); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 179 | } |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 180 | if (treatment.writeMetadata() != null) { |
| 181 | instructions.add(buildMetadata(treatment.writeMetadata())); |
| 182 | } |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 183 | if (treatment.metered() != null) { |
| 184 | instructions.add(buildMeter(treatment.metered())); |
| 185 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 186 | |
| 187 | long cookie = flowRule().id().value(); |
| 188 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 189 | OFFlowMod fm = factory().buildFlowModify() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 190 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 191 | .setCookie(U64.of(cookie)) |
| 192 | .setBufferId(OFBufferId.NO_BUFFER) |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 193 | .setInstructions(instructions) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 194 | .setMatch(match) |
| 195 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 196 | .setPriority(flowRule().priority()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 197 | .setTableId(TableId.of(flowRule().tableId())) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 198 | .build(); |
| 199 | |
| 200 | return fm; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public OFFlowDelete buildFlowDel() { |
| 205 | Match match = buildMatch(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 206 | |
| 207 | long cookie = flowRule().id().value(); |
| 208 | |
| 209 | OFFlowDelete fm = factory().buildFlowDelete() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 210 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 211 | .setCookie(U64.of(cookie)) |
| 212 | .setBufferId(OFBufferId.NO_BUFFER) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 213 | .setMatch(match) |
| 214 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 215 | .setPriority(flowRule().priority()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 216 | .setTableId(TableId.of(flowRule().tableId())) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 217 | .build(); |
| 218 | |
| 219 | return fm; |
| 220 | } |
| 221 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 222 | private List<OFAction> buildActions(List<Instruction> treatments) { |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 223 | if (treatment == null) { |
Sho SHIMIZU | 6601dd7 | 2015-05-11 19:00:29 -0700 | [diff] [blame] | 224 | return Collections.emptyList(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 225 | } |
Sho SHIMIZU | 6601dd7 | 2015-05-11 19:00:29 -0700 | [diff] [blame] | 226 | |
| 227 | boolean tableFound = false; |
| 228 | List<OFAction> actions = new LinkedList<>(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 229 | for (Instruction i : treatments) { |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 230 | switch (i.type()) { |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 231 | case DROP: |
Charles Chan | 7efabeb | 2015-09-28 15:12:19 -0700 | [diff] [blame] | 232 | case NOACTION: |
Sho SHIMIZU | b35ed36 | 2015-04-28 11:05:19 -0700 | [diff] [blame] | 233 | return Collections.emptyList(); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 234 | case L0MODIFICATION: |
| 235 | actions.add(buildL0Modification(i)); |
| 236 | break; |
| 237 | case L2MODIFICATION: |
| 238 | actions.add(buildL2Modification(i)); |
| 239 | break; |
| 240 | case L3MODIFICATION: |
| 241 | actions.add(buildL3Modification(i)); |
| 242 | break; |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 243 | case L4MODIFICATION: |
| 244 | actions.add(buildL4Modification(i)); |
| 245 | break; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 246 | case OUTPUT: |
| 247 | OutputInstruction out = (OutputInstruction) i; |
| 248 | OFActionOutput.Builder action = factory().actions().buildOutput() |
| 249 | .setPort(OFPort.of((int) out.port().toLong())); |
| 250 | if (out.port().equals(PortNumber.CONTROLLER)) { |
| 251 | action.setMaxLen(OFPCML_NO_BUFFER); |
| 252 | } |
| 253 | actions.add(action.build()); |
| 254 | break; |
| 255 | case GROUP: |
| 256 | GroupInstruction group = (GroupInstruction) i; |
| 257 | OFActionGroup.Builder groupBuilder = factory().actions().buildGroup() |
| 258 | .setGroup(OFGroup.of(group.groupId().id())); |
| 259 | actions.add(groupBuilder.build()); |
| 260 | break; |
Steffen Gebert | bbfdaaa | 2015-09-29 11:01:46 +0200 | [diff] [blame] | 261 | case QUEUE: |
| 262 | SetQueueInstruction queue = (SetQueueInstruction) i; |
| 263 | OFActionSetQueue.Builder queueBuilder = factory().actions().buildSetQueue() |
| 264 | .setQueueId(queue.queueId()); |
| 265 | actions.add(queueBuilder.build()); |
| 266 | break; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 267 | case TABLE: |
| 268 | //FIXME: should not occur here. |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 269 | tableFound = true; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 270 | break; |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 271 | case EXTENSION: |
| 272 | actions.add(buildExtensionAction(((Instructions.ExtensionInstructionWrapper) i) |
| 273 | .extensionInstruction())); |
| 274 | break; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 275 | default: |
| 276 | log.warn("Instruction type {} not yet implemented.", i.type()); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 279 | if (tableFound && actions.isEmpty()) { |
| 280 | // handles the case where there are no actions, but there is |
| 281 | // a goto instruction for the next table |
Sho SHIMIZU | 6601dd7 | 2015-05-11 19:00:29 -0700 | [diff] [blame] | 282 | return Collections.emptyList(); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 283 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 284 | return actions; |
| 285 | } |
| 286 | |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 287 | private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) { |
| 288 | OFInstruction instruction = factory().instructions().gotoTable( |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 289 | TableId.of(i.tableId())); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 290 | return instruction; |
| 291 | } |
| 292 | |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame] | 293 | private OFInstruction buildMetadata(Instructions.MetadataInstruction m) { |
| 294 | OFInstruction instruction = factory().instructions().writeMetadata( |
| 295 | U64.of(m.metadata()), U64.of(m.metadataMask())); |
| 296 | return instruction; |
| 297 | } |
| 298 | |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 299 | private OFInstruction buildMeter(Instructions.MeterInstruction metered) { |
| 300 | return factory().instructions().meter(metered.meterId().id()); |
| 301 | } |
| 302 | |
| 303 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 304 | private OFAction buildL0Modification(Instruction i) { |
| 305 | L0ModificationInstruction l0m = (L0ModificationInstruction) i; |
| 306 | switch (l0m.subtype()) { |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 307 | case LAMBDA: |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 308 | return buildModLambdaInstruction((ModLambdaInstruction) i); |
| 309 | case OCH: |
| 310 | try { |
| 311 | return buildModOchSignalInstruction((ModOchSignalInstruction) i); |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 312 | } catch (NoMappingFoundException e) { |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 313 | log.warn(e.getMessage()); |
| 314 | break; |
| 315 | } |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 316 | default: |
| 317 | log.warn("Unimplemented action type {}.", l0m.subtype()); |
| 318 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 319 | } |
| 320 | return null; |
| 321 | } |
| 322 | |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 323 | private OFAction buildModLambdaInstruction(ModLambdaInstruction instruction) { |
Marc De Leenheer | c2999f3 | 2015-11-20 15:52:16 -0800 | [diff] [blame] | 324 | return factory().actions().circuit(factory().oxms().expOchSigId( |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 325 | new CircuitSignalID((byte) 1, (byte) 2, instruction.lambda(), (short) 1))); |
| 326 | } |
| 327 | |
| 328 | private OFAction buildModOchSignalInstruction(ModOchSignalInstruction instruction) { |
| 329 | OchSignal signal = instruction.lambda(); |
Sho SHIMIZU | c17042d | 2015-05-28 12:07:23 -0700 | [diff] [blame] | 330 | byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType()); |
| 331 | byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing()); |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 332 | |
Marc De Leenheer | c2999f3 | 2015-11-20 15:52:16 -0800 | [diff] [blame] | 333 | return factory().actions().circuit(factory().oxms().expOchSigId( |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 334 | new CircuitSignalID(gridType, channelSpacing, |
| 335 | (short) signal.spacingMultiplier(), (short) signal.slotGranularity()) |
| 336 | )); |
| 337 | } |
| 338 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 339 | private OFAction buildL2Modification(Instruction i) { |
| 340 | L2ModificationInstruction l2m = (L2ModificationInstruction) i; |
| 341 | ModEtherInstruction eth; |
| 342 | OFOxm<?> oxm = null; |
| 343 | switch (l2m.subtype()) { |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 344 | case ETH_DST: |
| 345 | eth = (ModEtherInstruction) l2m; |
| 346 | oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong())); |
| 347 | break; |
| 348 | case ETH_SRC: |
| 349 | eth = (ModEtherInstruction) l2m; |
| 350 | oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong())); |
| 351 | break; |
| 352 | case VLAN_ID: |
| 353 | ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m; |
| 354 | oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort())); |
| 355 | break; |
| 356 | case VLAN_PCP: |
| 357 | ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m; |
| 358 | oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp())); |
| 359 | break; |
| 360 | case MPLS_PUSH: |
| 361 | PushHeaderInstructions pushHeaderInstructions = |
| 362 | (PushHeaderInstructions) l2m; |
| 363 | return factory().actions().pushMpls(EthType.of(pushHeaderInstructions |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 364 | .ethernetType().toShort())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 365 | case MPLS_POP: |
Jonathan Hart | 67fc097 | 2015-03-19 15:21:20 -0700 | [diff] [blame] | 366 | PushHeaderInstructions popHeaderInstructions = |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 367 | (PushHeaderInstructions) l2m; |
| 368 | return factory().actions().popMpls(EthType.of(popHeaderInstructions |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 369 | .ethernetType().toShort())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 370 | case MPLS_LABEL: |
| 371 | ModMplsLabelInstruction mplsLabel = |
| 372 | (ModMplsLabelInstruction) l2m; |
HIGUCHI Yuta | 04b49fc | 2015-08-28 09:58:58 -0700 | [diff] [blame] | 373 | oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.mplsLabel().toInt())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 374 | break; |
Saurav Das | 73a7dd4 | 2015-08-19 22:20:31 -0700 | [diff] [blame] | 375 | case MPLS_BOS: |
| 376 | ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m; |
| 377 | oxm = factory().oxms() |
| 378 | .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE |
| 379 | : OFBooleanValue.FALSE); |
| 380 | break; |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 381 | case DEC_MPLS_TTL: |
| 382 | return factory().actions().decMplsTtl(); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 383 | case VLAN_POP: |
| 384 | return factory().actions().popVlan(); |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 385 | case VLAN_PUSH: |
| 386 | PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m; |
| 387 | return factory().actions().pushVlan( |
alshabib | 7b808c5 | 2015-06-26 14:22:24 -0700 | [diff] [blame] | 388 | EthType.of(pushVlanInstruction.ethernetType().toShort())); |
Hyunsun Moon | a08c5d0 | 2015-07-14 17:53:00 -0700 | [diff] [blame] | 389 | case TUNNEL_ID: |
| 390 | ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m; |
| 391 | oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId())); |
| 392 | break; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 393 | default: |
| 394 | log.warn("Unimplemented action type {}.", l2m.subtype()); |
| 395 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | if (oxm != null) { |
| 399 | return factory().actions().buildSetField().setField(oxm).build(); |
| 400 | } |
| 401 | return null; |
| 402 | } |
| 403 | |
| 404 | private OFAction buildL3Modification(Instruction i) { |
| 405 | L3ModificationInstruction l3m = (L3ModificationInstruction) i; |
| 406 | ModIPInstruction ip; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 407 | Ip4Address ip4; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 408 | Ip6Address ip6; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 409 | OFOxm<?> oxm = null; |
| 410 | switch (l3m.subtype()) { |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 411 | case IPV4_SRC: |
| 412 | ip = (ModIPInstruction) i; |
| 413 | ip4 = ip.ip().getIp4Address(); |
| 414 | oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt())); |
| 415 | break; |
| 416 | case IPV4_DST: |
| 417 | ip = (ModIPInstruction) i; |
| 418 | ip4 = ip.ip().getIp4Address(); |
| 419 | oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt())); |
| 420 | break; |
| 421 | case IPV6_SRC: |
| 422 | ip = (ModIPInstruction) i; |
| 423 | ip6 = ip.ip().getIp6Address(); |
| 424 | oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets())); |
| 425 | break; |
| 426 | case IPV6_DST: |
| 427 | ip = (ModIPInstruction) i; |
| 428 | ip6 = ip.ip().getIp6Address(); |
| 429 | oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets())); |
| 430 | break; |
| 431 | case IPV6_FLABEL: |
| 432 | ModIPv6FlowLabelInstruction flowLabelInstruction = |
| 433 | (ModIPv6FlowLabelInstruction) i; |
| 434 | int flowLabel = flowLabelInstruction.flowLabel(); |
| 435 | oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel)); |
| 436 | break; |
lishuai | 3cce60b | 2015-12-01 19:35:16 +0800 | [diff] [blame^] | 437 | case ARP_SPA: |
| 438 | ModArpIPInstruction aip = (ModArpIPInstruction) i; |
| 439 | ip4 = aip.ip().getIp4Address(); |
| 440 | oxm = factory().oxms().arpSpa(IPv4Address.of(ip4.toInt())); |
| 441 | break; |
| 442 | case ARP_SHA: |
| 443 | ModArpEthInstruction ei = (ModArpEthInstruction) i; |
| 444 | oxm = factory().oxms().arpSha(MacAddress.of(ei.mac().toLong())); |
| 445 | break; |
| 446 | case ARP_OP: |
| 447 | ModArpOpInstruction oi = (ModArpOpInstruction) i; |
| 448 | oxm = factory().oxms().arpOp(ArpOpcode.of((int) oi.op())); |
| 449 | break; |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 450 | case DEC_TTL: |
| 451 | return factory().actions().decNwTtl(); |
| 452 | case TTL_IN: |
| 453 | return factory().actions().copyTtlIn(); |
| 454 | case TTL_OUT: |
| 455 | return factory().actions().copyTtlOut(); |
| 456 | default: |
| 457 | log.warn("Unimplemented action type {}.", l3m.subtype()); |
| 458 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | if (oxm != null) { |
| 462 | return factory().actions().buildSetField().setField(oxm).build(); |
| 463 | } |
| 464 | return null; |
| 465 | } |
| 466 | |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 467 | private OFAction buildL4Modification(Instruction i) { |
| 468 | L4ModificationInstruction l4m = (L4ModificationInstruction) i; |
| 469 | ModTransportPortInstruction tp; |
| 470 | OFOxm<?> oxm = null; |
| 471 | switch (l4m.subtype()) { |
| 472 | case TCP_SRC: |
| 473 | tp = (ModTransportPortInstruction) l4m; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 474 | oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt())); |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 475 | break; |
| 476 | case TCP_DST: |
| 477 | tp = (ModTransportPortInstruction) l4m; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 478 | oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt())); |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 479 | break; |
| 480 | case UDP_SRC: |
| 481 | tp = (ModTransportPortInstruction) l4m; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 482 | oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt())); |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 483 | break; |
| 484 | case UDP_DST: |
| 485 | tp = (ModTransportPortInstruction) l4m; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 486 | oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt())); |
Hyunsun Moon | c8bd97c | 2015-07-18 22:47:33 -0700 | [diff] [blame] | 487 | break; |
| 488 | default: |
| 489 | log.warn("Unimplemented action type {}.", l4m.subtype()); |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | if (oxm != null) { |
| 494 | return factory().actions().buildSetField().setField(oxm).build(); |
| 495 | } |
| 496 | return null; |
| 497 | } |
| 498 | |
alshabib | 880b644 | 2015-11-23 22:13:04 -0800 | [diff] [blame] | 499 | private OFAction buildExtensionAction(ExtensionTreatment i) { |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 500 | if (!driverService.isPresent()) { |
| 501 | log.error("No driver service present"); |
| 502 | return null; |
| 503 | } |
| 504 | Driver driver = driverService.get().getDriver(deviceId); |
alshabib | 880b644 | 2015-11-23 22:13:04 -0800 | [diff] [blame] | 505 | if (driver.hasBehaviour(ExtensionTreatmentInterpreter.class)) { |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 506 | DefaultDriverHandler handler = |
| 507 | new DefaultDriverHandler(new DefaultDriverData(driver, deviceId)); |
alshabib | 880b644 | 2015-11-23 22:13:04 -0800 | [diff] [blame] | 508 | ExtensionTreatmentInterpreter interpreter = handler.behaviour(ExtensionTreatmentInterpreter.class); |
Jonathan Hart | 3c25916 | 2015-10-21 21:31:19 -0700 | [diff] [blame] | 509 | return interpreter.mapInstruction(factory(), i); |
| 510 | } |
| 511 | |
| 512 | return null; |
| 513 | } |
| 514 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 515 | } |