blob: c351e1b6e42e5b0b4545123990fa55dea79c6881 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.of.flow.impl;
Jonathan Hart86e59352014-10-22 10:42:16 -070017
18import java.util.Collections;
19import java.util.LinkedList;
20import java.util.List;
Brian O'Connor427a1762014-11-19 18:40:32 -080021import java.util.Optional;
Jonathan Hart86e59352014-10-22 10:42:16 -070022
Charles M.C. Chanfe421812015-01-12 18:20:51 +080023import org.onosproject.net.PortNumber;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.flow.FlowRule;
25import org.onosproject.net.flow.TrafficTreatment;
26import org.onosproject.net.flow.instructions.Instruction;
27import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
28import org.onosproject.net.flow.instructions.L0ModificationInstruction;
29import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
30import org.onosproject.net.flow.instructions.L2ModificationInstruction;
31import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
32import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
33import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
34import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
35import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions;
36import org.onosproject.net.flow.instructions.L3ModificationInstruction;
37import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -080038import org.onlab.packet.Ip4Address;
Jonathan Hart86e59352014-10-22 10:42:16 -070039import org.projectfloodlight.openflow.protocol.OFFactory;
40import org.projectfloodlight.openflow.protocol.OFFlowAdd;
41import org.projectfloodlight.openflow.protocol.OFFlowDelete;
42import org.projectfloodlight.openflow.protocol.OFFlowMod;
43import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
44import org.projectfloodlight.openflow.protocol.action.OFAction;
Charles M.C. Chanfe421812015-01-12 18:20:51 +080045import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
Jonathan Hart86e59352014-10-22 10:42:16 -070046import org.projectfloodlight.openflow.protocol.match.Match;
47import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
48import org.projectfloodlight.openflow.types.CircuitSignalID;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080049import org.projectfloodlight.openflow.types.EthType;
Jonathan Hart86e59352014-10-22 10:42:16 -070050import org.projectfloodlight.openflow.types.IPv4Address;
51import org.projectfloodlight.openflow.types.MacAddress;
52import org.projectfloodlight.openflow.types.OFBufferId;
53import org.projectfloodlight.openflow.types.OFPort;
54import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080055import org.projectfloodlight.openflow.types.U32;
Jonathan Hart86e59352014-10-22 10:42:16 -070056import org.projectfloodlight.openflow.types.U64;
57import org.projectfloodlight.openflow.types.VlanPcp;
58import org.slf4j.Logger;
59import org.slf4j.LoggerFactory;
60
61/**
62 * Flow mod builder for OpenFlow 1.3+.
63 */
64public class FlowModBuilderVer13 extends FlowModBuilder {
65
66 private static final Logger log = LoggerFactory.getLogger(FlowModBuilderVer10.class);
Charles M.C. Chanfe421812015-01-12 18:20:51 +080067 private static final int OFPCML_NO_BUFFER = 0xffff;
Jonathan Hart86e59352014-10-22 10:42:16 -070068
69 private final TrafficTreatment treatment;
70
71 /**
72 * Constructor for a flow mod builder for OpenFlow 1.3.
73 *
74 * @param flowRule the flow rule to transform into a flow mod
75 * @param factory the OpenFlow factory to use to build the flow mod
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -080076 * @param xid the transaction ID
Jonathan Hart86e59352014-10-22 10:42:16 -070077 */
Brian O'Connor427a1762014-11-19 18:40:32 -080078 protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid) {
79 super(flowRule, factory, xid);
Jonathan Hart86e59352014-10-22 10:42:16 -070080
81 this.treatment = flowRule.treatment();
82 }
83
84 @Override
85 public OFFlowAdd buildFlowAdd() {
86 Match match = buildMatch();
Jonathan Hartd4a8bba2014-10-28 12:44:20 -070087 List<OFAction> actions = buildActions();
88
89 // FIXME had to revert back to using apply-actions instead of
90 // write-actions because LINC-OE apparently doesn't support
91 // write-actions. I would prefer to change this back in the future
92 // because apply-actions is an optional instruction in OF 1.3.
93
94 //OFInstruction writeActions =
95 //factory().instructions().writeActions(actions);
Jonathan Hart86e59352014-10-22 10:42:16 -070096
97 long cookie = flowRule().id().value();
98
alshabib4785eec2014-12-04 16:45:45 -080099
Jonathan Hart86e59352014-10-22 10:42:16 -0700100 OFFlowAdd fm = factory().buildFlowAdd()
Brian O'Connor427a1762014-11-19 18:40:32 -0800101 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700102 .setCookie(U64.of(cookie))
103 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700104 .setActions(actions)
105 //.setInstructions(Collections.singletonList(writeActions))
Jonathan Hart86e59352014-10-22 10:42:16 -0700106 .setMatch(match)
107 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
108 .setPriority(flowRule().priority())
109 .build();
110
111 return fm;
112 }
113
114 @Override
115 public OFFlowMod buildFlowMod() {
116 Match match = buildMatch();
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700117 List<OFAction> actions = buildActions();
118 //OFInstruction writeActions =
119 //factory().instructions().writeActions(actions);
Jonathan Hart86e59352014-10-22 10:42:16 -0700120
121 long cookie = flowRule().id().value();
122
alshabib4785eec2014-12-04 16:45:45 -0800123
Jonathan Hart86e59352014-10-22 10:42:16 -0700124 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800125 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700126 .setCookie(U64.of(cookie))
127 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700128 .setActions(actions)
129 //.setInstructions(Collections.singletonList(writeActions))
Jonathan Hart86e59352014-10-22 10:42:16 -0700130 .setMatch(match)
131 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
132 .setPriority(flowRule().priority())
133 .build();
134
135 return fm;
136 }
137
138 @Override
139 public OFFlowDelete buildFlowDel() {
140 Match match = buildMatch();
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700141 List<OFAction> actions = buildActions();
142 //OFInstruction writeActions =
143 //factory().instructions().writeActions(actions);
Jonathan Hart86e59352014-10-22 10:42:16 -0700144
145 long cookie = flowRule().id().value();
146
147 OFFlowDelete fm = factory().buildFlowDelete()
Brian O'Connor427a1762014-11-19 18:40:32 -0800148 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700149 .setCookie(U64.of(cookie))
150 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hart86e59352014-10-22 10:42:16 -0700151 .setMatch(match)
152 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
153 .setPriority(flowRule().priority())
154 .build();
155
156 return fm;
157 }
158
159 private List<OFAction> buildActions() {
160 List<OFAction> actions = new LinkedList<>();
161 if (treatment == null) {
162 return actions;
163 }
164 for (Instruction i : treatment.instructions()) {
165 switch (i.type()) {
166 case DROP:
167 log.warn("Saw drop action; assigning drop action");
168 return new LinkedList<>();
169 case L0MODIFICATION:
170 actions.add(buildL0Modification(i));
171 break;
172 case L2MODIFICATION:
173 actions.add(buildL2Modification(i));
174 break;
175 case L3MODIFICATION:
176 actions.add(buildL3Modification(i));
177 break;
178 case OUTPUT:
179 OutputInstruction out = (OutputInstruction) i;
Charles M.C. Chanfe421812015-01-12 18:20:51 +0800180 OFActionOutput.Builder action = factory().actions().buildOutput()
181 .setPort(OFPort.of((int) out.port().toLong()));
182 if (out.port().equals(PortNumber.CONTROLLER)) {
183 action.setMaxLen(OFPCML_NO_BUFFER);
184 }
185 actions.add(action.build());
Jonathan Hart86e59352014-10-22 10:42:16 -0700186 break;
187 case GROUP:
188 default:
189 log.warn("Instruction type {} not yet implemented.", i.type());
190 }
191 }
192
193 return actions;
194 }
195
196 private OFAction buildL0Modification(Instruction i) {
197 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
198 switch (l0m.subtype()) {
199 case LAMBDA:
200 ModLambdaInstruction ml = (ModLambdaInstruction) i;
201 return factory().actions().circuit(factory().oxms().ochSigidBasic(
202 new CircuitSignalID((byte) 1, (byte) 2, ml.lambda(), (short) 1)));
203 default:
204 log.warn("Unimplemented action type {}.", l0m.subtype());
205 break;
206 }
207 return null;
208 }
209
210 private OFAction buildL2Modification(Instruction i) {
211 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
212 ModEtherInstruction eth;
213 OFOxm<?> oxm = null;
214 switch (l2m.subtype()) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800215 case ETH_DST:
216 eth = (ModEtherInstruction) l2m;
217 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
218 break;
219 case ETH_SRC:
220 eth = (ModEtherInstruction) l2m;
221 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
222 break;
223 case VLAN_ID:
224 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
225 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
226 break;
227 case VLAN_PCP:
228 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
229 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
230 break;
231 case MPLS_PUSH:
232 PushHeaderInstructions pushHeaderInstructions =
233 (PushHeaderInstructions) l2m;
234 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
235 .ethernetType().getEtherType()));
236 case MPLS_POP:
237 PushHeaderInstructions popHeaderInstructions =
238 (PushHeaderInstructions) l2m;
239 return factory().actions().popMpls(EthType.of(popHeaderInstructions
240 .ethernetType().getEtherType()));
241 case MPLS_LABEL:
242 ModMplsLabelInstruction mplsLabel =
243 (ModMplsLabelInstruction) l2m;
244 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.label()
245 .longValue()));
246
247 break;
248 default:
249 log.warn("Unimplemented action type {}.", l2m.subtype());
250 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700251 }
252
253 if (oxm != null) {
254 return factory().actions().buildSetField().setField(oxm).build();
255 }
256 return null;
257 }
258
259 private OFAction buildL3Modification(Instruction i) {
260 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
261 ModIPInstruction ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800262 Ip4Address ip4;
Jonathan Hart86e59352014-10-22 10:42:16 -0700263 OFOxm<?> oxm = null;
264 switch (l3m.subtype()) {
265 case IP_DST:
266 ip = (ModIPInstruction) i;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800267 ip4 = ip.ip().getIp4Address();
268 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
Ray Milkey241b96a2014-11-17 13:08:20 -0800269 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700270 case IP_SRC:
271 ip = (ModIPInstruction) i;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800272 ip4 = ip.ip().getIp4Address();
273 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
Ray Milkey241b96a2014-11-17 13:08:20 -0800274 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700275 default:
276 log.warn("Unimplemented action type {}.", l3m.subtype());
277 break;
278 }
279
280 if (oxm != null) {
281 return factory().actions().buildSetField().setField(oxm).build();
282 }
283 return null;
284 }
285
286}