blob: d87103d8e61318756e51e73070792e27c998339f [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
Jonathan Hart29afca32015-01-20 18:17:39 -080018import org.onlab.packet.Ip4Address;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080019import org.onlab.packet.Ip6Address;
Charles M.C. Chanfe421812015-01-12 18:20:51 +080020import org.onosproject.net.PortNumber;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.flow.FlowRule;
22import org.onosproject.net.flow.TrafficTreatment;
23import org.onosproject.net.flow.instructions.Instruction;
alshabib9af70072015-02-09 14:34:16 -080024import org.onosproject.net.flow.instructions.Instructions;
sangho8995ac52015-02-04 11:29:03 -080025import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
alshabib9af70072015-02-09 14:34:16 -080026import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.flow.instructions.L0ModificationInstruction;
28import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
29import org.onosproject.net.flow.instructions.L2ModificationInstruction;
30import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Jonathan Hart29afca32015-01-20 18:17:39 -080031import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
33import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions;
35import org.onosproject.net.flow.instructions.L3ModificationInstruction;
36import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080037import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
alshabib9af70072015-02-09 14:34:16 -080038import org.onosproject.openflow.controller.OpenFlowSwitch;
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;
sangho8995ac52015-02-04 11:29:03 -080045import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
Charles M.C. Chanfe421812015-01-12 18:20:51 +080046import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
alshabib9af70072015-02-09 14:34:16 -080047import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hart86e59352014-10-22 10:42:16 -070048import org.projectfloodlight.openflow.protocol.match.Match;
49import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
50import org.projectfloodlight.openflow.types.CircuitSignalID;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080051import org.projectfloodlight.openflow.types.EthType;
Jonathan Hart86e59352014-10-22 10:42:16 -070052import org.projectfloodlight.openflow.types.IPv4Address;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080053import org.projectfloodlight.openflow.types.IPv6Address;
54import org.projectfloodlight.openflow.types.IPv6FlowLabel;
Jonathan Hart86e59352014-10-22 10:42:16 -070055import org.projectfloodlight.openflow.types.MacAddress;
56import org.projectfloodlight.openflow.types.OFBufferId;
sangho8995ac52015-02-04 11:29:03 -080057import org.projectfloodlight.openflow.types.OFGroup;
Jonathan Hart86e59352014-10-22 10:42:16 -070058import org.projectfloodlight.openflow.types.OFPort;
59import org.projectfloodlight.openflow.types.OFVlanVidMatch;
alshabib9af70072015-02-09 14:34:16 -080060import org.projectfloodlight.openflow.types.TableId;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080061import org.projectfloodlight.openflow.types.U32;
Jonathan Hart86e59352014-10-22 10:42:16 -070062import org.projectfloodlight.openflow.types.U64;
63import org.projectfloodlight.openflow.types.VlanPcp;
64import org.slf4j.Logger;
65import org.slf4j.LoggerFactory;
66
Jonathan Hart29afca32015-01-20 18:17:39 -080067import java.util.Collections;
68import java.util.LinkedList;
69import java.util.List;
70import java.util.Optional;
71
Jonathan Hart86e59352014-10-22 10:42:16 -070072/**
73 * Flow mod builder for OpenFlow 1.3+.
74 */
75public class FlowModBuilderVer13 extends FlowModBuilder {
76
77 private static final Logger log = LoggerFactory.getLogger(FlowModBuilderVer10.class);
Charles M.C. Chanfe421812015-01-12 18:20:51 +080078 private static final int OFPCML_NO_BUFFER = 0xffff;
Jonathan Hart86e59352014-10-22 10:42:16 -070079
80 private final TrafficTreatment treatment;
81
82 /**
83 * Constructor for a flow mod builder for OpenFlow 1.3.
84 *
85 * @param flowRule the flow rule to transform into a flow mod
86 * @param factory the OpenFlow factory to use to build the flow mod
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -080087 * @param xid the transaction ID
Jonathan Hart86e59352014-10-22 10:42:16 -070088 */
Brian O'Connor427a1762014-11-19 18:40:32 -080089 protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid) {
90 super(flowRule, factory, xid);
Jonathan Hart86e59352014-10-22 10:42:16 -070091
92 this.treatment = flowRule.treatment();
93 }
94
95 @Override
96 public OFFlowAdd buildFlowAdd() {
97 Match match = buildMatch();
Jonathan Hartd4a8bba2014-10-28 12:44:20 -070098 List<OFAction> actions = buildActions();
alshabib9af70072015-02-09 14:34:16 -080099 List<OFInstruction> instructions = buildInstructions();
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700100 // FIXME had to revert back to using apply-actions instead of
101 // write-actions because LINC-OE apparently doesn't support
102 // write-actions. I would prefer to change this back in the future
103 // because apply-actions is an optional instruction in OF 1.3.
104
Jonathan Harteda33872015-02-18 10:22:32 -0800105 OFInstruction applyActions =
106 factory().instructions().applyActions(actions);
107
108 instructions.add(applyActions);
Jonathan Hart86e59352014-10-22 10:42:16 -0700109
110 long cookie = flowRule().id().value();
111
alshabib4785eec2014-12-04 16:45:45 -0800112
Jonathan Hart86e59352014-10-22 10:42:16 -0700113 OFFlowAdd fm = factory().buildFlowAdd()
Brian O'Connor427a1762014-11-19 18:40:32 -0800114 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700115 .setCookie(U64.of(cookie))
116 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800117 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700118 .setMatch(match)
119 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
120 .setPriority(flowRule().priority())
121 .build();
122
123 return fm;
124 }
125
126 @Override
127 public OFFlowMod buildFlowMod() {
128 Match match = buildMatch();
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700129 List<OFAction> actions = buildActions();
alshabib9af70072015-02-09 14:34:16 -0800130 List<OFInstruction> instructions = buildInstructions();
Jonathan Harteda33872015-02-18 10:22:32 -0800131 OFInstruction applyActions =
132 factory().instructions().applyActions(actions);
133
134 instructions.add(applyActions);
Jonathan Hart86e59352014-10-22 10:42:16 -0700135
136 long cookie = flowRule().id().value();
137
alshabib4785eec2014-12-04 16:45:45 -0800138
Jonathan Hart86e59352014-10-22 10:42:16 -0700139 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800140 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700141 .setCookie(U64.of(cookie))
142 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800143 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700144 .setMatch(match)
145 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
146 .setPriority(flowRule().priority())
147 .build();
148
149 return fm;
150 }
151
152 @Override
153 public OFFlowDelete buildFlowDel() {
154 Match match = buildMatch();
Jonathan Hart86e59352014-10-22 10:42:16 -0700155
156 long cookie = flowRule().id().value();
157
158 OFFlowDelete fm = factory().buildFlowDelete()
Brian O'Connor427a1762014-11-19 18:40:32 -0800159 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700160 .setCookie(U64.of(cookie))
161 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hart86e59352014-10-22 10:42:16 -0700162 .setMatch(match)
163 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
164 .setPriority(flowRule().priority())
165 .build();
166
167 return fm;
168 }
169
alshabib9af70072015-02-09 14:34:16 -0800170
171 private List<OFInstruction> buildInstructions() {
172 List<OFInstruction> instructions = new LinkedList<>();
173 if (treatment == null) {
174 return instructions;
175 }
176 for (Instruction i : treatment.instructions()) {
177 switch (i.type()) {
178 case TABLE:
179 instructions.add(buildTableGoto(((Instructions.TableTypeTransition) i)));
180 break;
181 default:
182 break;
183 }
184 }
185 return instructions;
186 }
187
Jonathan Hart86e59352014-10-22 10:42:16 -0700188 private List<OFAction> buildActions() {
189 List<OFAction> actions = new LinkedList<>();
190 if (treatment == null) {
191 return actions;
192 }
193 for (Instruction i : treatment.instructions()) {
194 switch (i.type()) {
alshabib9af70072015-02-09 14:34:16 -0800195 case DROP:
196 log.warn("Saw drop action; assigning drop action");
197 return new LinkedList<>();
198 case L0MODIFICATION:
199 actions.add(buildL0Modification(i));
200 break;
201 case L2MODIFICATION:
202 actions.add(buildL2Modification(i));
203 break;
204 case L3MODIFICATION:
205 actions.add(buildL3Modification(i));
206 break;
207 case OUTPUT:
208 OutputInstruction out = (OutputInstruction) i;
209 OFActionOutput.Builder action = factory().actions().buildOutput()
210 .setPort(OFPort.of((int) out.port().toLong()));
211 if (out.port().equals(PortNumber.CONTROLLER)) {
212 action.setMaxLen(OFPCML_NO_BUFFER);
213 }
214 actions.add(action.build());
215 break;
216 case GROUP:
217 GroupInstruction group = (GroupInstruction) i;
218 OFActionGroup.Builder groupBuilder = factory().actions().buildGroup()
219 .setGroup(OFGroup.of(group.groupId().id()));
220 actions.add(groupBuilder.build());
221 break;
222 default:
223 log.warn("Instruction type {} not yet implemented.", i.type());
Jonathan Hart86e59352014-10-22 10:42:16 -0700224 }
225 }
226
227 return actions;
228 }
229
alshabib9af70072015-02-09 14:34:16 -0800230 private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) {
231 OFInstruction instruction = factory().instructions().gotoTable(
232 TableId.of(getTableType(i.tableType()).ordinal()));
233 return instruction;
234 }
235
236 // FIXME: this has to go as well perhaps when we implement the SelectorService.
237 private OpenFlowSwitch.TableType getTableType(FlowRule.Type type) {
238 switch (type) {
239
240 case DEFAULT:
241 return OpenFlowSwitch.TableType.NONE;
242 case IP:
243 return OpenFlowSwitch.TableType.IP;
244 case MPLS:
245 return OpenFlowSwitch.TableType.MPLS;
246 case ACL:
247 return OpenFlowSwitch.TableType.ACL;
248 case VLAN_MPLS:
249 return OpenFlowSwitch.TableType.VLAN_MPLS;
250 case VLAN:
251 return OpenFlowSwitch.TableType.VLAN;
252 case ETHER:
253 return OpenFlowSwitch.TableType.ETHER;
254 case COS:
255 return OpenFlowSwitch.TableType.COS;
256 default:
257 return OpenFlowSwitch.TableType.NONE;
258 }
259 }
260
261
Jonathan Hart86e59352014-10-22 10:42:16 -0700262 private OFAction buildL0Modification(Instruction i) {
263 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
264 switch (l0m.subtype()) {
265 case LAMBDA:
266 ModLambdaInstruction ml = (ModLambdaInstruction) i;
267 return factory().actions().circuit(factory().oxms().ochSigidBasic(
268 new CircuitSignalID((byte) 1, (byte) 2, ml.lambda(), (short) 1)));
269 default:
270 log.warn("Unimplemented action type {}.", l0m.subtype());
271 break;
272 }
273 return null;
274 }
275
276 private OFAction buildL2Modification(Instruction i) {
277 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
278 ModEtherInstruction eth;
279 OFOxm<?> oxm = null;
280 switch (l2m.subtype()) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800281 case ETH_DST:
282 eth = (ModEtherInstruction) l2m;
283 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
284 break;
285 case ETH_SRC:
286 eth = (ModEtherInstruction) l2m;
287 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
288 break;
289 case VLAN_ID:
290 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
291 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
292 break;
293 case VLAN_PCP:
294 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
295 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
296 break;
297 case MPLS_PUSH:
298 PushHeaderInstructions pushHeaderInstructions =
299 (PushHeaderInstructions) l2m;
300 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800301 .ethernetType()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800302 case MPLS_POP:
303 PushHeaderInstructions popHeaderInstructions =
304 (PushHeaderInstructions) l2m;
305 return factory().actions().popMpls(EthType.of(popHeaderInstructions
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800306 .ethernetType()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800307 case MPLS_LABEL:
308 ModMplsLabelInstruction mplsLabel =
309 (ModMplsLabelInstruction) l2m;
310 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.label()
311 .longValue()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800312 break;
sangho3f97a17d2015-01-29 22:56:29 -0800313 case DEC_MPLS_TTL:
314 return factory().actions().decMplsTtl();
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800315 default:
316 log.warn("Unimplemented action type {}.", l2m.subtype());
317 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700318 }
319
320 if (oxm != null) {
321 return factory().actions().buildSetField().setField(oxm).build();
322 }
323 return null;
324 }
325
326 private OFAction buildL3Modification(Instruction i) {
327 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
328 ModIPInstruction ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800329 Ip4Address ip4;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800330 Ip6Address ip6;
Jonathan Hart86e59352014-10-22 10:42:16 -0700331 OFOxm<?> oxm = null;
332 switch (l3m.subtype()) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800333 case IPV4_SRC:
334 ip = (ModIPInstruction) i;
335 ip4 = ip.ip().getIp4Address();
336 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
337 break;
338 case IPV4_DST:
Jonathan Hart86e59352014-10-22 10:42:16 -0700339 ip = (ModIPInstruction) i;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800340 ip4 = ip.ip().getIp4Address();
341 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
Ray Milkey241b96a2014-11-17 13:08:20 -0800342 break;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800343 case IPV6_SRC:
Jonathan Hart86e59352014-10-22 10:42:16 -0700344 ip = (ModIPInstruction) i;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800345 ip6 = ip.ip().getIp6Address();
346 oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets()));
347 break;
348 case IPV6_DST:
349 ip = (ModIPInstruction) i;
350 ip6 = ip.ip().getIp6Address();
351 oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets()));
352 break;
353 case IPV6_FLABEL:
354 ModIPv6FlowLabelInstruction flowLabelInstruction =
355 (ModIPv6FlowLabelInstruction) i;
356 int flowLabel = flowLabelInstruction.flowLabel();
357 oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel));
Ray Milkey241b96a2014-11-17 13:08:20 -0800358 break;
sangho3f97a17d2015-01-29 22:56:29 -0800359 case DEC_TTL:
360 return factory().actions().decNwTtl();
361 case TTL_IN:
362 return factory().actions().copyTtlIn();
363 case TTL_OUT:
364 return factory().actions().copyTtlOut();
Jonathan Hart86e59352014-10-22 10:42:16 -0700365 default:
366 log.warn("Unimplemented action type {}.", l3m.subtype());
367 break;
368 }
369
370 if (oxm != null) {
371 return factory().actions().buildSetField().setField(oxm).build();
372 }
373 return null;
374 }
375
376}