blob: b2e7cf4e626d759159b7743e9f317baa5b4d4ec0 [file] [log] [blame]
alshabibeec3a062014-09-17 18:01:26 -07001package org.onlab.onos.provider.of.flow.impl;
2
3import static org.slf4j.LoggerFactory.getLogger;
4
5import java.util.Collections;
6import java.util.LinkedList;
7import java.util.List;
8
alshabib6b5cfec2014-09-18 17:42:18 -07009import org.onlab.onos.net.flow.FlowId;
alshabibeec3a062014-09-17 18:01:26 -070010import org.onlab.onos.net.flow.FlowRule;
11import org.onlab.onos.net.flow.TrafficSelector;
12import org.onlab.onos.net.flow.TrafficTreatment;
13import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion;
14import org.onlab.onos.net.flow.criteria.Criteria.EthTypeCriterion;
15import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion;
16import org.onlab.onos.net.flow.criteria.Criteria.IPProtocolCriterion;
17import org.onlab.onos.net.flow.criteria.Criteria.PortCriterion;
18import org.onlab.onos.net.flow.criteria.Criteria.VlanIdCriterion;
19import org.onlab.onos.net.flow.criteria.Criteria.VlanPcpCriterion;
20import org.onlab.onos.net.flow.criteria.Criterion;
21import org.onlab.onos.net.flow.instructions.Instruction;
22import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction;
23import org.onlab.onos.net.flow.instructions.L2ModificationInstruction;
24import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
25import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
26import org.onlab.onos.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
27import org.onlab.onos.net.flow.instructions.L3ModificationInstruction;
28import org.onlab.onos.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
29import org.projectfloodlight.openflow.protocol.OFFactory;
30import org.projectfloodlight.openflow.protocol.OFFlowMod;
31import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
32import org.projectfloodlight.openflow.protocol.action.OFAction;
33import org.projectfloodlight.openflow.protocol.match.Match;
34import org.projectfloodlight.openflow.protocol.match.MatchField;
35import org.projectfloodlight.openflow.types.EthType;
36import org.projectfloodlight.openflow.types.IPv4Address;
37import org.projectfloodlight.openflow.types.IpProtocol;
38import org.projectfloodlight.openflow.types.MacAddress;
39import org.projectfloodlight.openflow.types.Masked;
40import org.projectfloodlight.openflow.types.OFBufferId;
41import org.projectfloodlight.openflow.types.OFPort;
42import org.projectfloodlight.openflow.types.OFVlanVidMatch;
alshabib6b5cfec2014-09-18 17:42:18 -070043import org.projectfloodlight.openflow.types.U64;
alshabibeec3a062014-09-17 18:01:26 -070044import org.projectfloodlight.openflow.types.VlanPcp;
45import org.projectfloodlight.openflow.types.VlanVid;
46import org.slf4j.Logger;
47
48
49public class FlowModBuilder {
50
51 private final Logger log = getLogger(getClass());
52
53 private final OFFactory factory;
54 private final TrafficTreatment treatment;
55 private final TrafficSelector selector;
56
57 private final int priority;
58
alshabib6b5cfec2014-09-18 17:42:18 -070059 private final FlowId cookie;
60
alshabibeec3a062014-09-17 18:01:26 -070061
62
63 public FlowModBuilder(FlowRule flowRule, OFFactory factory) {
64 this.factory = factory;
65 this.treatment = flowRule.treatment();
66 this.selector = flowRule.selector();
67 this.priority = flowRule.priority();
alshabib6b5cfec2014-09-18 17:42:18 -070068 this.cookie = flowRule.id();
alshabibeec3a062014-09-17 18:01:26 -070069 }
70
71 public OFFlowMod buildFlowMod() {
72 Match match = buildMatch();
73 List<OFAction> actions = buildActions();
74
75 //TODO: what to do without bufferid? do we assume that there will be a pktout as well?
76 OFFlowMod fm = factory.buildFlowModify()
alshabib6b5cfec2014-09-18 17:42:18 -070077 .setCookie(U64.of(cookie.value()))
alshabibeec3a062014-09-17 18:01:26 -070078 .setBufferId(OFBufferId.NO_BUFFER)
79 .setActions(actions)
80 .setMatch(match)
81 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
82 .setIdleTimeout(10)
83 .setHardTimeout(10)
84 .setPriority(priority)
85 .build();
86
87 return fm;
88
89 }
90
91 private List<OFAction> buildActions() {
92 List<OFAction> acts = new LinkedList<>();
93 for (Instruction i : treatment.instructions()) {
94 switch (i.type()) {
95 case DROP:
96 log.warn("Saw drop action; assigning drop action");
97 return new LinkedList<>();
98 case L2MODIFICATION:
99 acts.add(buildL2Modification(i));
100 case L3MODIFICATION:
101 acts.add(buildL3Modification(i));
102 case OUTPUT:
103 OutputInstruction out = (OutputInstruction) i;
104 acts.add(factory.actions().buildOutput().setPort(
105 OFPort.of((int) out.port().toLong())).build());
106 break;
107 case GROUP:
108 default:
109 log.warn("Instruction type {} not yet implemented.", i.type());
110 }
111 }
112
113 return acts;
114 }
115
116 private OFAction buildL3Modification(Instruction i) {
117 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
118 ModIPInstruction ip;
119 switch (l3m.subtype()) {
120 case L3_DST:
121 ip = (ModIPInstruction) i;
122 return factory.actions().setNwDst(IPv4Address.of(ip.ip().toInt()));
123 case L3_SRC:
124 ip = (ModIPInstruction) i;
125 return factory.actions().setNwSrc(IPv4Address.of(ip.ip().toInt()));
126 default:
127 log.warn("Unimplemented action type {}.", l3m.subtype());
128 break;
129 }
130 return null;
131 }
132
133 private OFAction buildL2Modification(Instruction i) {
134 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
135 ModEtherInstruction eth;
136 switch (l2m.subtype()) {
137 case L2_DST:
138 eth = (ModEtherInstruction) l2m;
139 return factory.actions().setDlDst(MacAddress.of(eth.mac().toLong()));
140 case L2_SRC:
141 eth = (ModEtherInstruction) l2m;
142 return factory.actions().setDlSrc(MacAddress.of(eth.mac().toLong()));
143 case VLAN_ID:
144 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
145 return factory.actions().setVlanVid(VlanVid.ofVlan(vlanId.vlanId.toShort()));
146 case VLAN_PCP:
147 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
148 return factory.actions().setVlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
149 default:
150 log.warn("Unimplemented action type {}.", l2m.subtype());
151 break;
152 }
153 return null;
154 }
155
156 private Match buildMatch() {
157 Match.Builder mBuilder = factory.buildMatch();
158 EthCriterion eth;
159 IPCriterion ip;
160 for (Criterion c : selector.criteria()) {
161 switch (c.type()) {
162 case IN_PORT:
163 PortCriterion inport = (PortCriterion) c;
164 mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong()));
165 break;
166 case ETH_SRC:
167 eth = (EthCriterion) c;
168 mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong()));
169 break;
170 case ETH_DST:
171 eth = (EthCriterion) c;
172 mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong()));
173 break;
174 case ETH_TYPE:
175 EthTypeCriterion ethType = (EthTypeCriterion) c;
176 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType()));
177 break;
178 case IPV4_DST:
179 ip = (IPCriterion) c;
180 if (ip.ip().isMasked()) {
181 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
182 IPv4Address.of(ip.ip().netmask().toInt()));
183 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
184 } else {
185 mBuilder.setExact(MatchField.IPV4_DST, IPv4Address.of(ip.ip().toInt()));
186 }
187 break;
188 case IPV4_SRC:
189 ip = (IPCriterion) c;
190 if (ip.ip().isMasked()) {
191 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
192 IPv4Address.of(ip.ip().netmask().toInt()));
193 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
194 } else {
195 mBuilder.setExact(MatchField.IPV4_SRC, IPv4Address.of(ip.ip().toInt()));
196 }
197 break;
198 case IP_PROTO:
199 IPProtocolCriterion p = (IPProtocolCriterion) c;
200 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
201 break;
202 case VLAN_PCP:
203 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
204 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
205 break;
206 case VLAN_VID:
207 VlanIdCriterion vid = (VlanIdCriterion) c;
208 mBuilder.setExact(MatchField.VLAN_VID,
209 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
210 break;
211 case ARP_OP:
212 case ARP_SHA:
213 case ARP_SPA:
214 case ARP_THA:
215 case ARP_TPA:
216 case ICMPV4_CODE:
217 case ICMPV4_TYPE:
218 case ICMPV6_CODE:
219 case ICMPV6_TYPE:
220 case IN_PHY_PORT:
221 case IPV6_DST:
222 case IPV6_EXTHDR:
223 case IPV6_FLABEL:
224 case IPV6_ND_SLL:
225 case IPV6_ND_TARGET:
226 case IPV6_ND_TLL:
227 case IPV6_SRC:
228 case IP_DSCP:
229 case IP_ECN:
230 case METADATA:
231 case MPLS_BOS:
232 case MPLS_LABEL:
233 case MPLS_TC:
234 case PBB_ISID:
235 case SCTP_DST:
236 case SCTP_SRC:
237 case TCP_DST:
238 case TCP_SRC:
239 case TUNNEL_ID:
240 case UDP_DST:
241 case UDP_SRC:
242 default:
243 log.warn("Match type {} not yet implemented.", c.type());
244 }
245 }
246 return mBuilder.build();
247 }
248
249}