blob: f2ad9595c84c0704ee74e2ea69a199e1ee7b2d2d [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 */
Jonathan Hart86e59352014-10-22 10:42:16 -070016package org.onlab.onos.provider.of.flow.impl;
17
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
23import org.onlab.onos.net.flow.FlowRule;
24import org.onlab.onos.net.flow.TrafficTreatment;
25import org.onlab.onos.net.flow.instructions.Instruction;
26import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction;
27import org.onlab.onos.net.flow.instructions.L0ModificationInstruction;
28import org.onlab.onos.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
29import org.onlab.onos.net.flow.instructions.L2ModificationInstruction;
30import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
31import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
32import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080033import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
34import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions;
Jonathan Hart86e59352014-10-22 10:42:16 -070035import org.onlab.onos.net.flow.instructions.L3ModificationInstruction;
36import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -080037import org.onlab.packet.Ip4Address;
Jonathan Hart86e59352014-10-22 10:42:16 -070038import org.projectfloodlight.openflow.protocol.OFFactory;
39import org.projectfloodlight.openflow.protocol.OFFlowAdd;
40import org.projectfloodlight.openflow.protocol.OFFlowDelete;
41import org.projectfloodlight.openflow.protocol.OFFlowMod;
42import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
43import org.projectfloodlight.openflow.protocol.action.OFAction;
Jonathan Hart86e59352014-10-22 10:42:16 -070044import org.projectfloodlight.openflow.protocol.match.Match;
45import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
46import org.projectfloodlight.openflow.types.CircuitSignalID;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080047import org.projectfloodlight.openflow.types.EthType;
Jonathan Hart86e59352014-10-22 10:42:16 -070048import org.projectfloodlight.openflow.types.IPv4Address;
49import org.projectfloodlight.openflow.types.MacAddress;
50import org.projectfloodlight.openflow.types.OFBufferId;
51import org.projectfloodlight.openflow.types.OFPort;
52import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080053import org.projectfloodlight.openflow.types.U32;
Jonathan Hart86e59352014-10-22 10:42:16 -070054import org.projectfloodlight.openflow.types.U64;
55import org.projectfloodlight.openflow.types.VlanPcp;
56import org.slf4j.Logger;
57import org.slf4j.LoggerFactory;
58
59/**
60 * Flow mod builder for OpenFlow 1.3+.
61 */
62public class FlowModBuilderVer13 extends FlowModBuilder {
63
64 private static final Logger log = LoggerFactory.getLogger(FlowModBuilderVer10.class);
65
66 private final TrafficTreatment treatment;
67
68 /**
69 * Constructor for a flow mod builder for OpenFlow 1.3.
70 *
71 * @param flowRule the flow rule to transform into a flow mod
72 * @param factory the OpenFlow factory to use to build the flow mod
73 */
Brian O'Connor427a1762014-11-19 18:40:32 -080074 protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid) {
75 super(flowRule, factory, xid);
Jonathan Hart86e59352014-10-22 10:42:16 -070076
77 this.treatment = flowRule.treatment();
78 }
79
80 @Override
81 public OFFlowAdd buildFlowAdd() {
82 Match match = buildMatch();
Jonathan Hartd4a8bba2014-10-28 12:44:20 -070083 List<OFAction> actions = buildActions();
84
85 // FIXME had to revert back to using apply-actions instead of
86 // write-actions because LINC-OE apparently doesn't support
87 // write-actions. I would prefer to change this back in the future
88 // because apply-actions is an optional instruction in OF 1.3.
89
90 //OFInstruction writeActions =
91 //factory().instructions().writeActions(actions);
Jonathan Hart86e59352014-10-22 10:42:16 -070092
93 long cookie = flowRule().id().value();
94
95 //TODO: what to do without bufferid? do we assume that there will be a pktout as well?
96 OFFlowAdd fm = factory().buildFlowAdd()
Brian O'Connor427a1762014-11-19 18:40:32 -080097 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -070098 .setCookie(U64.of(cookie))
99 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700100 .setActions(actions)
101 //.setInstructions(Collections.singletonList(writeActions))
Jonathan Hart86e59352014-10-22 10:42:16 -0700102 .setMatch(match)
103 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
104 .setPriority(flowRule().priority())
105 .build();
106
107 return fm;
108 }
109
110 @Override
111 public OFFlowMod buildFlowMod() {
112 Match match = buildMatch();
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700113 List<OFAction> actions = buildActions();
114 //OFInstruction writeActions =
115 //factory().instructions().writeActions(actions);
Jonathan Hart86e59352014-10-22 10:42:16 -0700116
117 long cookie = flowRule().id().value();
118
119 //TODO: what to do without bufferid? do we assume that there will be a pktout as well?
120 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800121 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700122 .setCookie(U64.of(cookie))
123 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700124 .setActions(actions)
125 //.setInstructions(Collections.singletonList(writeActions))
Jonathan Hart86e59352014-10-22 10:42:16 -0700126 .setMatch(match)
127 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
128 .setPriority(flowRule().priority())
129 .build();
130
131 return fm;
132 }
133
134 @Override
135 public OFFlowDelete buildFlowDel() {
136 Match match = buildMatch();
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700137 List<OFAction> actions = buildActions();
138 //OFInstruction writeActions =
139 //factory().instructions().writeActions(actions);
Jonathan Hart86e59352014-10-22 10:42:16 -0700140
141 long cookie = flowRule().id().value();
142
143 OFFlowDelete fm = factory().buildFlowDelete()
Brian O'Connor427a1762014-11-19 18:40:32 -0800144 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700145 .setCookie(U64.of(cookie))
146 .setBufferId(OFBufferId.NO_BUFFER)
Brian O'Connore4adf242014-11-07 00:16:34 -0800147 //.setActions(actions) //FIXME do we want to send actions in flowdel?
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700148 //.setInstructions(Collections.singletonList(writeActions))
Jonathan Hart86e59352014-10-22 10:42:16 -0700149 .setMatch(match)
150 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
151 .setPriority(flowRule().priority())
152 .build();
153
154 return fm;
155 }
156
157 private List<OFAction> buildActions() {
158 List<OFAction> actions = new LinkedList<>();
159 if (treatment == null) {
160 return actions;
161 }
162 for (Instruction i : treatment.instructions()) {
163 switch (i.type()) {
164 case DROP:
165 log.warn("Saw drop action; assigning drop action");
166 return new LinkedList<>();
167 case L0MODIFICATION:
168 actions.add(buildL0Modification(i));
169 break;
170 case L2MODIFICATION:
171 actions.add(buildL2Modification(i));
172 break;
173 case L3MODIFICATION:
174 actions.add(buildL3Modification(i));
175 break;
176 case OUTPUT:
177 OutputInstruction out = (OutputInstruction) i;
178 actions.add(factory().actions().buildOutput().setPort(
179 OFPort.of((int) out.port().toLong())).build());
180 break;
181 case GROUP:
182 default:
183 log.warn("Instruction type {} not yet implemented.", i.type());
184 }
185 }
186
187 return actions;
188 }
189
190 private OFAction buildL0Modification(Instruction i) {
191 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
192 switch (l0m.subtype()) {
193 case LAMBDA:
194 ModLambdaInstruction ml = (ModLambdaInstruction) i;
195 return factory().actions().circuit(factory().oxms().ochSigidBasic(
196 new CircuitSignalID((byte) 1, (byte) 2, ml.lambda(), (short) 1)));
197 default:
198 log.warn("Unimplemented action type {}.", l0m.subtype());
199 break;
200 }
201 return null;
202 }
203
204 private OFAction buildL2Modification(Instruction i) {
205 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
206 ModEtherInstruction eth;
207 OFOxm<?> oxm = null;
208 switch (l2m.subtype()) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800209 case ETH_DST:
210 eth = (ModEtherInstruction) l2m;
211 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
212 break;
213 case ETH_SRC:
214 eth = (ModEtherInstruction) l2m;
215 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
216 break;
217 case VLAN_ID:
218 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
219 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
220 break;
221 case VLAN_PCP:
222 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
223 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
224 break;
225 case MPLS_PUSH:
226 PushHeaderInstructions pushHeaderInstructions =
227 (PushHeaderInstructions) l2m;
228 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
229 .ethernetType().getEtherType()));
230 case MPLS_POP:
231 PushHeaderInstructions popHeaderInstructions =
232 (PushHeaderInstructions) l2m;
233 return factory().actions().popMpls(EthType.of(popHeaderInstructions
234 .ethernetType().getEtherType()));
235 case MPLS_LABEL:
236 ModMplsLabelInstruction mplsLabel =
237 (ModMplsLabelInstruction) l2m;
238 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.label()
239 .longValue()));
240
241 break;
242 default:
243 log.warn("Unimplemented action type {}.", l2m.subtype());
244 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700245 }
246
247 if (oxm != null) {
248 return factory().actions().buildSetField().setField(oxm).build();
249 }
250 return null;
251 }
252
253 private OFAction buildL3Modification(Instruction i) {
254 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
255 ModIPInstruction ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800256 Ip4Address ip4;
Jonathan Hart86e59352014-10-22 10:42:16 -0700257 OFOxm<?> oxm = null;
258 switch (l3m.subtype()) {
259 case IP_DST:
260 ip = (ModIPInstruction) i;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800261 ip4 = ip.ip().getIp4Address();
262 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
Ray Milkey241b96a2014-11-17 13:08:20 -0800263 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700264 case IP_SRC:
265 ip = (ModIPInstruction) i;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800266 ip4 = ip.ip().getIp4Address();
267 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
Ray Milkey241b96a2014-11-17 13:08:20 -0800268 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700269 default:
270 log.warn("Unimplemented action type {}.", l3m.subtype());
271 break;
272 }
273
274 if (oxm != null) {
275 return factory().actions().buildSetField().setField(oxm).build();
276 }
277 return null;
278 }
279
280}