blob: 6c4ee4dcb63c6f996ff00d598a99438b5f086b7f [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080018import org.onlab.packet.Ip4Address;
19import org.onosproject.net.PortNumber;
Jonathan Hart3c259162015-10-21 21:31:19 -070020import org.onosproject.net.driver.DriverService;
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;
24import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Steffen Gebertba2d3b72015-10-22 11:14:31 +020025import org.onosproject.net.flow.instructions.Instructions.SetQueueInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.flow.instructions.L2ModificationInstruction;
27import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
28import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
29import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
30import org.onosproject.net.flow.instructions.L3ModificationInstruction;
31import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Jonathan Hart86e59352014-10-22 10:42:16 -070032import org.projectfloodlight.openflow.protocol.OFFactory;
33import org.projectfloodlight.openflow.protocol.OFFlowAdd;
34import org.projectfloodlight.openflow.protocol.OFFlowDelete;
35import org.projectfloodlight.openflow.protocol.OFFlowMod;
36import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
37import org.projectfloodlight.openflow.protocol.action.OFAction;
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080038import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
Steffen Gebertba2d3b72015-10-22 11:14:31 +020039import org.projectfloodlight.openflow.protocol.action.OFActionEnqueue;
Jonathan Hart86e59352014-10-22 10:42:16 -070040import org.projectfloodlight.openflow.protocol.match.Match;
41import org.projectfloodlight.openflow.types.IPv4Address;
42import org.projectfloodlight.openflow.types.MacAddress;
43import org.projectfloodlight.openflow.types.OFBufferId;
44import org.projectfloodlight.openflow.types.OFPort;
45import org.projectfloodlight.openflow.types.U64;
46import org.projectfloodlight.openflow.types.VlanPcp;
47import org.projectfloodlight.openflow.types.VlanVid;
48import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080051import java.util.Collections;
52import java.util.LinkedList;
53import java.util.List;
54import java.util.Optional;
55
Jonathan Hart86e59352014-10-22 10:42:16 -070056/**
57 * Flow mod builder for OpenFlow 1.0.
58 */
59public class FlowModBuilderVer10 extends FlowModBuilder {
60
alshabib10580802015-02-18 18:30:33 -080061 private final Logger log = LoggerFactory.getLogger(getClass());
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080062 private static final int OFPCML_NO_BUFFER = 0xffff;
Jonathan Hart86e59352014-10-22 10:42:16 -070063
64 private final TrafficTreatment treatment;
65
66 /**
67 * Constructor for a flow mod builder for OpenFlow 1.0.
68 *
69 * @param flowRule the flow rule to transform into a flow mod
70 * @param factory the OpenFlow factory to use to build the flow mod
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -080071 * @param xid the transaction ID
Jonathan Hart86e59352014-10-22 10:42:16 -070072 */
Brian O'Connor427a1762014-11-19 18:40:32 -080073 protected FlowModBuilderVer10(FlowRule flowRule,
Jonathan Hart3c259162015-10-21 21:31:19 -070074 OFFactory factory, Optional<Long> xid,
75 Optional<DriverService> driverService) {
76 super(flowRule, factory, xid, driverService);
Jonathan Hart86e59352014-10-22 10:42:16 -070077
78 this.treatment = flowRule.treatment();
79 }
80
81 @Override
82 public OFFlowAdd buildFlowAdd() {
83 Match match = buildMatch();
84 List<OFAction> actions = buildActions();
85
86 long cookie = flowRule().id().value();
87
alshabib4785eec2014-12-04 16:45:45 -080088
Jonathan Hart86e59352014-10-22 10:42:16 -070089 OFFlowAdd fm = factory().buildFlowAdd()
Brian O'Connor427a1762014-11-19 18:40:32 -080090 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -070091 .setCookie(U64.of(cookie))
92 .setBufferId(OFBufferId.NO_BUFFER)
93 .setActions(actions)
94 .setMatch(match)
95 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
96 .setPriority(flowRule().priority())
97 .build();
98
99 return fm;
100 }
101
102 @Override
103 public OFFlowMod buildFlowMod() {
104 Match match = buildMatch();
105 List<OFAction> actions = buildActions();
106
107 long cookie = flowRule().id().value();
108
Jonathan Hart86e59352014-10-22 10:42:16 -0700109 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800110 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700111 .setCookie(U64.of(cookie))
112 .setBufferId(OFBufferId.NO_BUFFER)
113 .setActions(actions)
114 .setMatch(match)
115 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
116 .setPriority(flowRule().priority())
117 .build();
118
119 return fm;
120 }
121
122 @Override
123 public OFFlowDelete buildFlowDel() {
124 Match match = buildMatch();
Jonathan Hart86e59352014-10-22 10:42:16 -0700125
126 long cookie = flowRule().id().value();
127
128 OFFlowDelete fm = factory().buildFlowDelete()
Brian O'Connor427a1762014-11-19 18:40:32 -0800129 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700130 .setCookie(U64.of(cookie))
131 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hart86e59352014-10-22 10:42:16 -0700132 .setMatch(match)
133 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
134 .setPriority(flowRule().priority())
135 .build();
136
137 return fm;
138 }
139
140 private List<OFAction> buildActions() {
141 List<OFAction> acts = new LinkedList<>();
alshabibda4abde2015-05-20 15:13:23 -0700142 OFAction act;
Jonathan Hart86e59352014-10-22 10:42:16 -0700143 if (treatment == null) {
144 return acts;
145 }
Ray Milkey42507352015-03-20 15:16:10 -0700146 for (Instruction i : treatment.immediate()) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700147 switch (i.type()) {
148 case DROP:
Charles Chan7efabeb2015-09-28 15:12:19 -0700149 case NOACTION:
Sho SHIMIZUb35ed362015-04-28 11:05:19 -0700150 return Collections.emptyList();
Jonathan Hart86e59352014-10-22 10:42:16 -0700151 case L2MODIFICATION:
alshabibda4abde2015-05-20 15:13:23 -0700152 act = buildL2Modification(i);
153 if (act != null) {
154 acts.add(buildL2Modification(i));
155 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700156 break;
157 case L3MODIFICATION:
alshabibda4abde2015-05-20 15:13:23 -0700158 act = buildL3Modification(i);
159 if (act != null) {
160 acts.add(buildL3Modification(i));
161 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700162 break;
163 case OUTPUT:
164 OutputInstruction out = (OutputInstruction) i;
Thomas Vachuskaf4df0052015-01-06 12:30:11 -0800165 OFActionOutput.Builder action = factory().actions().buildOutput()
166 .setPort(OFPort.of((int) out.port().toLong()));
167 if (out.port().equals(PortNumber.CONTROLLER)) {
168 action.setMaxLen(OFPCML_NO_BUFFER);
169 }
170 acts.add(action.build());
Jonathan Hart86e59352014-10-22 10:42:16 -0700171 break;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200172 case QUEUE:
173 SetQueueInstruction queue = (SetQueueInstruction) i;
174 if (queue.port() == null) {
175 log.warn("Required argument 'port' undefined for OFActionEnqueue");
176 }
177 OFActionEnqueue.Builder queueBuilder = factory().actions().buildEnqueue()
178 .setQueueId(queue.queueId())
179 .setPort(OFPort.ofInt((int) queue.port().toLong()));
180 acts.add(queueBuilder.build());
181 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700182 case L0MODIFICATION:
183 case GROUP:
Saurav Das86af8f12015-05-25 23:55:33 -0700184 case TABLE:
185 case METADATA:
Jonathan Hart86e59352014-10-22 10:42:16 -0700186 log.warn("Instruction type {} not supported with protocol version {}",
187 i.type(), factory().getVersion());
188 break;
189 default:
190 log.warn("Instruction type {} not yet implemented.", i.type());
191 }
192 }
193
194 return acts;
195 }
196
197 private OFAction buildL3Modification(Instruction i) {
198 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
199 ModIPInstruction ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800200 Ip4Address ip4;
Jonathan Hart86e59352014-10-22 10:42:16 -0700201 switch (l3m.subtype()) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800202 case IPV4_SRC:
Jonathan Hart86e59352014-10-22 10:42:16 -0700203 ip = (ModIPInstruction) i;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800204 ip4 = ip.ip().getIp4Address();
205 return factory().actions().setNwSrc(IPv4Address.of(ip4.toInt()));
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800206 case IPV4_DST:
207 ip = (ModIPInstruction) i;
208 ip4 = ip.ip().getIp4Address();
209 return factory().actions().setNwDst(IPv4Address.of(ip4.toInt()));
Jonathan Hart86e59352014-10-22 10:42:16 -0700210 default:
211 log.warn("Unimplemented action type {}.", l3m.subtype());
212 break;
213 }
214 return null;
215 }
216
217 private OFAction buildL2Modification(Instruction i) {
218 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
219 ModEtherInstruction eth;
220 switch (l2m.subtype()) {
221 case ETH_DST:
222 eth = (ModEtherInstruction) l2m;
223 return factory().actions().setDlDst(MacAddress.of(eth.mac().toLong()));
224 case ETH_SRC:
225 eth = (ModEtherInstruction) l2m;
226 return factory().actions().setDlSrc(MacAddress.of(eth.mac().toLong()));
227 case VLAN_ID:
228 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
Ray Milkey78081052014-11-05 10:38:12 -0800229 return factory().actions().setVlanVid(VlanVid.ofVlan(vlanId.vlanId().toShort()));
Jonathan Hart86e59352014-10-22 10:42:16 -0700230 case VLAN_PCP:
231 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
232 return factory().actions().setVlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
Jonathan Hart67fc0972015-03-19 15:21:20 -0700233 case VLAN_POP:
alshabibab21b2d2015-03-04 18:35:33 -0800234 return factory().actions().stripVlan();
alshabibda4abde2015-05-20 15:13:23 -0700235 case VLAN_PUSH:
236 return null;
Jonathan Hart86e59352014-10-22 10:42:16 -0700237 default:
238 log.warn("Unimplemented action type {}.", l2m.subtype());
239 break;
240 }
241 return null;
242 }
243
244}