blob: c9de4500efd2222be543920a287ea7895f31bdf6 [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;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.flow.FlowRule;
21import org.onosproject.net.flow.TrafficTreatment;
22import org.onosproject.net.flow.instructions.Instruction;
23import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
24import org.onosproject.net.flow.instructions.L2ModificationInstruction;
25import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
26import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
27import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
28import org.onosproject.net.flow.instructions.L3ModificationInstruction;
29import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Jonathan Hart86e59352014-10-22 10:42:16 -070030import org.projectfloodlight.openflow.protocol.OFFactory;
31import org.projectfloodlight.openflow.protocol.OFFlowAdd;
32import org.projectfloodlight.openflow.protocol.OFFlowDelete;
33import org.projectfloodlight.openflow.protocol.OFFlowMod;
34import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
35import org.projectfloodlight.openflow.protocol.action.OFAction;
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080036import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
Jonathan Hart86e59352014-10-22 10:42:16 -070037import org.projectfloodlight.openflow.protocol.match.Match;
38import org.projectfloodlight.openflow.types.IPv4Address;
39import org.projectfloodlight.openflow.types.MacAddress;
40import org.projectfloodlight.openflow.types.OFBufferId;
41import org.projectfloodlight.openflow.types.OFPort;
42import org.projectfloodlight.openflow.types.U64;
43import org.projectfloodlight.openflow.types.VlanPcp;
44import org.projectfloodlight.openflow.types.VlanVid;
45import org.slf4j.Logger;
46import org.slf4j.LoggerFactory;
47
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080048import java.util.Collections;
49import java.util.LinkedList;
50import java.util.List;
51import java.util.Optional;
52
Jonathan Hart86e59352014-10-22 10:42:16 -070053/**
54 * Flow mod builder for OpenFlow 1.0.
55 */
56public class FlowModBuilderVer10 extends FlowModBuilder {
57
alshabib10580802015-02-18 18:30:33 -080058 private final Logger log = LoggerFactory.getLogger(getClass());
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080059 private static final int OFPCML_NO_BUFFER = 0xffff;
Jonathan Hart86e59352014-10-22 10:42:16 -070060
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 Radoslavov119fd5c2014-11-25 19:08:19 -080068 * @param xid the transaction ID
Jonathan Hart86e59352014-10-22 10:42:16 -070069 */
Brian O'Connor427a1762014-11-19 18:40:32 -080070 protected FlowModBuilderVer10(FlowRule flowRule,
71 OFFactory factory, Optional<Long> xid) {
72 super(flowRule, factory, xid);
Jonathan Hart86e59352014-10-22 10:42:16 -070073
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
alshabib4785eec2014-12-04 16:45:45 -080084
Jonathan Hart86e59352014-10-22 10:42:16 -070085 OFFlowAdd fm = factory().buildFlowAdd()
Brian O'Connor427a1762014-11-19 18:40:32 -080086 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -070087 .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 Hart86e59352014-10-22 10:42:16 -0700105 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800106 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700107 .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 Hart86e59352014-10-22 10:42:16 -0700121
122 long cookie = flowRule().id().value();
123
124 OFFlowDelete fm = factory().buildFlowDelete()
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 Hart86e59352014-10-22 10:42:16 -0700128 .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<>();
alshabibda4abde2015-05-20 15:13:23 -0700138 OFAction act;
Jonathan Hart86e59352014-10-22 10:42:16 -0700139 if (treatment == null) {
140 return acts;
141 }
Ray Milkey42507352015-03-20 15:16:10 -0700142 for (Instruction i : treatment.immediate()) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700143 switch (i.type()) {
144 case DROP:
145 log.warn("Saw drop action; assigning drop action");
Sho SHIMIZUb35ed362015-04-28 11:05:19 -0700146 return Collections.emptyList();
Jonathan Hart86e59352014-10-22 10:42:16 -0700147 case L2MODIFICATION:
alshabibda4abde2015-05-20 15:13:23 -0700148 act = buildL2Modification(i);
149 if (act != null) {
150 acts.add(buildL2Modification(i));
151 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700152 break;
153 case L3MODIFICATION:
alshabibda4abde2015-05-20 15:13:23 -0700154 act = buildL3Modification(i);
155 if (act != null) {
156 acts.add(buildL3Modification(i));
157 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700158 break;
159 case OUTPUT:
160 OutputInstruction out = (OutputInstruction) i;
Thomas Vachuskaf4df0052015-01-06 12:30:11 -0800161 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 Hart86e59352014-10-22 10:42:16 -0700167 break;
168 case L0MODIFICATION:
169 case GROUP:
Saurav Das86af8f12015-05-25 23:55:33 -0700170 case TABLE:
171 case METADATA:
Jonathan Hart86e59352014-10-22 10:42:16 -0700172 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 Radoslavov23e398d2014-11-05 15:17:57 -0800186 Ip4Address ip4;
Jonathan Hart86e59352014-10-22 10:42:16 -0700187 switch (l3m.subtype()) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800188 case IPV4_SRC:
Jonathan Hart86e59352014-10-22 10:42:16 -0700189 ip = (ModIPInstruction) i;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800190 ip4 = ip.ip().getIp4Address();
191 return factory().actions().setNwSrc(IPv4Address.of(ip4.toInt()));
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800192 case IPV4_DST:
193 ip = (ModIPInstruction) i;
194 ip4 = ip.ip().getIp4Address();
195 return factory().actions().setNwDst(IPv4Address.of(ip4.toInt()));
Jonathan Hart86e59352014-10-22 10:42:16 -0700196 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 Milkey78081052014-11-05 10:38:12 -0800215 return factory().actions().setVlanVid(VlanVid.ofVlan(vlanId.vlanId().toShort()));
Jonathan Hart86e59352014-10-22 10:42:16 -0700216 case VLAN_PCP:
217 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
218 return factory().actions().setVlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
Jonathan Hart67fc0972015-03-19 15:21:20 -0700219 case VLAN_POP:
alshabibab21b2d2015-03-04 18:35:33 -0800220 return factory().actions().stripVlan();
alshabibda4abde2015-05-20 15:13:23 -0700221 case VLAN_PUSH:
222 return null;
Jonathan Hart86e59352014-10-22 10:42:16 -0700223 default:
224 log.warn("Unimplemented action type {}.", l2m.subtype());
225 break;
226 }
227 return null;
228 }
229
230}