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 | |
Thomas Vachuska | f4df005 | 2015-01-06 12:30:11 -0800 | [diff] [blame] | 18 | import org.onlab.packet.Ip4Address; |
| 19 | import org.onosproject.net.PortNumber; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.flow.FlowRule; |
| 21 | import org.onosproject.net.flow.TrafficTreatment; |
| 22 | import org.onosproject.net.flow.instructions.Instruction; |
| 23 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 24 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 25 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
| 26 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
| 27 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction; |
| 28 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 29 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 30 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 31 | import org.projectfloodlight.openflow.protocol.OFFlowAdd; |
| 32 | import org.projectfloodlight.openflow.protocol.OFFlowDelete; |
| 33 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 34 | import org.projectfloodlight.openflow.protocol.OFFlowModFlags; |
| 35 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
Thomas Vachuska | f4df005 | 2015-01-06 12:30:11 -0800 | [diff] [blame] | 36 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 37 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 38 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 39 | import org.projectfloodlight.openflow.types.MacAddress; |
| 40 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 41 | import org.projectfloodlight.openflow.types.OFPort; |
| 42 | import org.projectfloodlight.openflow.types.U64; |
| 43 | import org.projectfloodlight.openflow.types.VlanPcp; |
| 44 | import org.projectfloodlight.openflow.types.VlanVid; |
| 45 | import org.slf4j.Logger; |
| 46 | import org.slf4j.LoggerFactory; |
| 47 | |
Thomas Vachuska | f4df005 | 2015-01-06 12:30:11 -0800 | [diff] [blame] | 48 | import java.util.Collections; |
| 49 | import java.util.LinkedList; |
| 50 | import java.util.List; |
| 51 | import java.util.Optional; |
| 52 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Flow mod builder for OpenFlow 1.0. |
| 55 | */ |
| 56 | public class FlowModBuilderVer10 extends FlowModBuilder { |
| 57 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 58 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Thomas Vachuska | f4df005 | 2015-01-06 12:30:11 -0800 | [diff] [blame] | 59 | private static final int OFPCML_NO_BUFFER = 0xffff; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 60 | |
| 61 | private final TrafficTreatment treatment; |
| 62 | |
| 63 | /** |
| 64 | * Constructor for a flow mod builder for OpenFlow 1.0. |
| 65 | * |
| 66 | * @param flowRule the flow rule to transform into a flow mod |
| 67 | * @param factory the OpenFlow factory to use to build the flow mod |
Pavlin Radoslavov | 119fd5c | 2014-11-25 19:08:19 -0800 | [diff] [blame] | 68 | * @param xid the transaction ID |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 69 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 70 | protected FlowModBuilderVer10(FlowRule flowRule, |
| 71 | OFFactory factory, Optional<Long> xid) { |
| 72 | super(flowRule, factory, xid); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 73 | |
| 74 | this.treatment = flowRule.treatment(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public OFFlowAdd buildFlowAdd() { |
| 79 | Match match = buildMatch(); |
| 80 | List<OFAction> actions = buildActions(); |
| 81 | |
| 82 | long cookie = flowRule().id().value(); |
| 83 | |
alshabib | 4785eec | 2014-12-04 16:45:45 -0800 | [diff] [blame] | 84 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 85 | OFFlowAdd fm = factory().buildFlowAdd() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 86 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 87 | .setCookie(U64.of(cookie)) |
| 88 | .setBufferId(OFBufferId.NO_BUFFER) |
| 89 | .setActions(actions) |
| 90 | .setMatch(match) |
| 91 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 92 | .setPriority(flowRule().priority()) |
| 93 | .build(); |
| 94 | |
| 95 | return fm; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public OFFlowMod buildFlowMod() { |
| 100 | Match match = buildMatch(); |
| 101 | List<OFAction> actions = buildActions(); |
| 102 | |
| 103 | long cookie = flowRule().id().value(); |
| 104 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 105 | OFFlowMod fm = factory().buildFlowModify() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 106 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 107 | .setCookie(U64.of(cookie)) |
| 108 | .setBufferId(OFBufferId.NO_BUFFER) |
| 109 | .setActions(actions) |
| 110 | .setMatch(match) |
| 111 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 112 | .setPriority(flowRule().priority()) |
| 113 | .build(); |
| 114 | |
| 115 | return fm; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public OFFlowDelete buildFlowDel() { |
| 120 | Match match = buildMatch(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 121 | |
| 122 | long cookie = flowRule().id().value(); |
| 123 | |
| 124 | OFFlowDelete fm = factory().buildFlowDelete() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 125 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 126 | .setCookie(U64.of(cookie)) |
| 127 | .setBufferId(OFBufferId.NO_BUFFER) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 128 | .setMatch(match) |
| 129 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 130 | .setPriority(flowRule().priority()) |
| 131 | .build(); |
| 132 | |
| 133 | return fm; |
| 134 | } |
| 135 | |
| 136 | private List<OFAction> buildActions() { |
| 137 | List<OFAction> acts = new LinkedList<>(); |
alshabib | da4abde | 2015-05-20 15:13:23 -0700 | [diff] [blame] | 138 | OFAction act; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 139 | if (treatment == null) { |
| 140 | return acts; |
| 141 | } |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 142 | for (Instruction i : treatment.immediate()) { |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 143 | switch (i.type()) { |
| 144 | case DROP: |
| 145 | log.warn("Saw drop action; assigning drop action"); |
Sho SHIMIZU | b35ed36 | 2015-04-28 11:05:19 -0700 | [diff] [blame] | 146 | return Collections.emptyList(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 147 | case L2MODIFICATION: |
alshabib | da4abde | 2015-05-20 15:13:23 -0700 | [diff] [blame] | 148 | act = buildL2Modification(i); |
| 149 | if (act != null) { |
| 150 | acts.add(buildL2Modification(i)); |
| 151 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 152 | break; |
| 153 | case L3MODIFICATION: |
alshabib | da4abde | 2015-05-20 15:13:23 -0700 | [diff] [blame] | 154 | act = buildL3Modification(i); |
| 155 | if (act != null) { |
| 156 | acts.add(buildL3Modification(i)); |
| 157 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 158 | break; |
| 159 | case OUTPUT: |
| 160 | OutputInstruction out = (OutputInstruction) i; |
Thomas Vachuska | f4df005 | 2015-01-06 12:30:11 -0800 | [diff] [blame] | 161 | OFActionOutput.Builder action = factory().actions().buildOutput() |
| 162 | .setPort(OFPort.of((int) out.port().toLong())); |
| 163 | if (out.port().equals(PortNumber.CONTROLLER)) { |
| 164 | action.setMaxLen(OFPCML_NO_BUFFER); |
| 165 | } |
| 166 | acts.add(action.build()); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 167 | break; |
| 168 | case L0MODIFICATION: |
| 169 | case GROUP: |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame^] | 170 | case TABLE: |
| 171 | case METADATA: |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 172 | log.warn("Instruction type {} not supported with protocol version {}", |
| 173 | i.type(), factory().getVersion()); |
| 174 | break; |
| 175 | default: |
| 176 | log.warn("Instruction type {} not yet implemented.", i.type()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return acts; |
| 181 | } |
| 182 | |
| 183 | private OFAction buildL3Modification(Instruction i) { |
| 184 | L3ModificationInstruction l3m = (L3ModificationInstruction) i; |
| 185 | ModIPInstruction ip; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 186 | Ip4Address ip4; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 187 | switch (l3m.subtype()) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 188 | case IPV4_SRC: |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 189 | ip = (ModIPInstruction) i; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 190 | ip4 = ip.ip().getIp4Address(); |
| 191 | return factory().actions().setNwSrc(IPv4Address.of(ip4.toInt())); |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 192 | case IPV4_DST: |
| 193 | ip = (ModIPInstruction) i; |
| 194 | ip4 = ip.ip().getIp4Address(); |
| 195 | return factory().actions().setNwDst(IPv4Address.of(ip4.toInt())); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 196 | default: |
| 197 | log.warn("Unimplemented action type {}.", l3m.subtype()); |
| 198 | break; |
| 199 | } |
| 200 | return null; |
| 201 | } |
| 202 | |
| 203 | private OFAction buildL2Modification(Instruction i) { |
| 204 | L2ModificationInstruction l2m = (L2ModificationInstruction) i; |
| 205 | ModEtherInstruction eth; |
| 206 | switch (l2m.subtype()) { |
| 207 | case ETH_DST: |
| 208 | eth = (ModEtherInstruction) l2m; |
| 209 | return factory().actions().setDlDst(MacAddress.of(eth.mac().toLong())); |
| 210 | case ETH_SRC: |
| 211 | eth = (ModEtherInstruction) l2m; |
| 212 | return factory().actions().setDlSrc(MacAddress.of(eth.mac().toLong())); |
| 213 | case VLAN_ID: |
| 214 | ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m; |
Ray Milkey | 7808105 | 2014-11-05 10:38:12 -0800 | [diff] [blame] | 215 | return factory().actions().setVlanVid(VlanVid.ofVlan(vlanId.vlanId().toShort())); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 216 | case VLAN_PCP: |
| 217 | ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m; |
| 218 | return factory().actions().setVlanPcp(VlanPcp.of(vlanPcp.vlanPcp())); |
Jonathan Hart | 67fc097 | 2015-03-19 15:21:20 -0700 | [diff] [blame] | 219 | case VLAN_POP: |
alshabib | ab21b2d | 2015-03-04 18:35:33 -0800 | [diff] [blame] | 220 | return factory().actions().stripVlan(); |
alshabib | da4abde | 2015-05-20 15:13:23 -0700 | [diff] [blame] | 221 | case VLAN_PUSH: |
| 222 | return null; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 223 | default: |
| 224 | log.warn("Unimplemented action type {}.", l2m.subtype()); |
| 225 | break; |
| 226 | } |
| 227 | return null; |
| 228 | } |
| 229 | |
| 230 | } |