lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 3 | * |
| 4 | * 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 |
| 7 | * |
| 8 | * 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. |
| 15 | */ |
| 16 | package org.onosproject.vtn.table.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import org.onlab.osgi.DefaultServiceDirectory; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
lishuai | aca6026 | 2015-11-26 19:48:01 +0800 | [diff] [blame] | 23 | import org.onlab.packet.EthType.EtherType; |
lishuai | 9e4e43a | 2015-11-26 19:30:55 +0800 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
lishuai | aca6026 | 2015-11-26 19:48:01 +0800 | [diff] [blame] | 25 | import org.onlab.packet.Ip4Address; |
lishuai | 7547db4 | 2015-11-20 14:56:11 +0800 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
lishuai | 9e4e43a | 2015-11-26 19:30:55 +0800 | [diff] [blame] | 27 | import org.onlab.packet.IpPrefix; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.net.DeviceId; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 31 | import org.onosproject.net.Port; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 34 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 35 | import org.onosproject.net.flow.TrafficSelector; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
| 37 | import org.onosproject.net.flow.criteria.Criteria; |
| 38 | import org.onosproject.net.flow.instructions.Instructions; |
| 39 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 40 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 41 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 42 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
| 43 | import org.onosproject.net.flowobjective.Objective; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 44 | import org.onosproject.net.flowobjective.Objective.Operation; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 45 | import org.onosproject.vtn.table.ClassifierService; |
| 46 | import org.onosproject.vtnrsc.SegmentationId; |
| 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | import com.google.common.collect.Sets; |
| 50 | |
| 51 | /** |
| 52 | * Provides implementation of ClassifierService. |
| 53 | */ |
| 54 | public class ClassifierServiceImpl implements ClassifierService { |
| 55 | private final Logger log = getLogger(getClass()); |
| 56 | |
lishuai | aca6026 | 2015-11-26 19:48:01 +0800 | [diff] [blame] | 57 | private static final EtherType ETH_TYPE = EtherType.ARP; |
Phanendra Manda | dbe60d8 | 2015-11-27 13:10:11 +0530 | [diff] [blame] | 58 | private static final int ARP_CLASSIFIER_PRIORITY = 60000; |
| 59 | private static final int L3_CLASSIFIER_PRIORITY = 0xffff; |
| 60 | private static final int L2_CLASSIFIER_PRIORITY = 50000; |
Wu wenbin | acc10ea | 2016-05-06 16:48:56 +0800 | [diff] [blame] | 61 | private static final int USERDATA_CLASSIFIER_PRIORITY = 65535; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 62 | private final FlowObjectiveService flowObjectiveService; |
| 63 | private final ApplicationId appId; |
| 64 | |
| 65 | /** |
| 66 | * Constructor. |
| 67 | * |
| 68 | * @param appId the application id of vtn |
| 69 | */ |
| 70 | public ClassifierServiceImpl(ApplicationId appId) { |
| 71 | this.appId = checkNotNull(appId, "ApplicationId can not be null"); |
| 72 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 73 | this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void programLocalIn(DeviceId deviceId, |
| 78 | SegmentationId segmentationId, PortNumber inPort, |
| 79 | MacAddress srcMac, ApplicationId appid, |
| 80 | Objective.Operation type) { |
| 81 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 82 | .matchInPort(inPort).matchEthSrc(srcMac).build(); |
| 83 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 84 | treatment.add(Instructions |
| 85 | .modTunnelId(Long.parseLong(segmentationId.toString()))); |
| 86 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 87 | .builder().withTreatment(treatment.build()) |
| 88 | .withSelector(selector).fromApp(appId).makePermanent() |
Phanendra Manda | dbe60d8 | 2015-11-27 13:10:11 +0530 | [diff] [blame] | 89 | .withFlag(Flag.SPECIFIC).withPriority(L2_CLASSIFIER_PRIORITY); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 90 | if (type.equals(Objective.Operation.ADD)) { |
| 91 | log.debug("programLocalIn-->ADD"); |
| 92 | flowObjectiveService.forward(deviceId, objective.add()); |
| 93 | } else { |
| 94 | log.debug("programLocalIn-->REMOVE"); |
| 95 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void programTunnelIn(DeviceId deviceId, |
| 101 | SegmentationId segmentationId, |
| 102 | Iterable<PortNumber> localTunnelPorts, |
| 103 | Objective.Operation type) { |
| 104 | if (localTunnelPorts == null) { |
| 105 | log.info("No tunnel port in device"); |
| 106 | return; |
| 107 | } |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 108 | Sets.newHashSet(localTunnelPorts).forEach(tp -> { |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 109 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 110 | .matchInPort(tp).add(Criteria.matchTunnelId(Long |
| 111 | .parseLong(segmentationId.toString()))) |
| 112 | .build(); |
| 113 | |
| 114 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 115 | .build(); |
| 116 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 117 | .builder().withTreatment(treatment).withSelector(selector) |
| 118 | .fromApp(appId).makePermanent().withFlag(Flag.SPECIFIC) |
Phanendra Manda | dbe60d8 | 2015-11-27 13:10:11 +0530 | [diff] [blame] | 119 | .withPriority(L2_CLASSIFIER_PRIORITY); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 120 | if (type.equals(Objective.Operation.ADD)) { |
| 121 | log.debug("programTunnelIn-->ADD"); |
| 122 | flowObjectiveService.forward(deviceId, objective.add()); |
| 123 | } else { |
| 124 | log.debug("programTunnelIn-->REMOVE"); |
| 125 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 126 | } |
| 127 | }); |
| 128 | } |
| 129 | |
lishuai | 7547db4 | 2015-11-20 14:56:11 +0800 | [diff] [blame] | 130 | @Override |
lishuai | 9e4e43a | 2015-11-26 19:30:55 +0800 | [diff] [blame] | 131 | public void programL3ExPortClassifierRules(DeviceId deviceId, PortNumber inPort, |
| 132 | IpAddress dstIp, |
| 133 | Objective.Operation type) { |
| 134 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 135 | .matchEthType(Ethernet.TYPE_IPV4).matchInPort(inPort) |
| 136 | .matchIPDst(IpPrefix.valueOf(dstIp, 32)).build(); |
| 137 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 138 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 139 | .builder().withTreatment(treatment).withSelector(selector) |
| 140 | .fromApp(appId).withFlag(Flag.SPECIFIC) |
Phanendra Manda | dbe60d8 | 2015-11-27 13:10:11 +0530 | [diff] [blame] | 141 | .withPriority(L3_CLASSIFIER_PRIORITY); |
lishuai | 9e4e43a | 2015-11-26 19:30:55 +0800 | [diff] [blame] | 142 | if (type.equals(Objective.Operation.ADD)) { |
| 143 | log.debug("L3ExToInClassifierRules-->ADD"); |
| 144 | flowObjectiveService.forward(deviceId, objective.add()); |
| 145 | } else { |
| 146 | log.debug("L3ExToInClassifierRules-->REMOVE"); |
| 147 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 148 | } |
lishuai | 7547db4 | 2015-11-20 14:56:11 +0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
lishuai | 478d02e | 2015-11-26 19:33:41 +0800 | [diff] [blame] | 152 | public void programL3InPortClassifierRules(DeviceId deviceId, PortNumber inPort, |
| 153 | MacAddress srcMac, MacAddress dstMac, |
| 154 | SegmentationId actionVni, |
| 155 | Objective.Operation type) { |
| 156 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 157 | .matchInPort(inPort).matchEthSrc(srcMac).matchEthDst(dstMac) |
| 158 | .build(); |
| 159 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 160 | .setTunnelId(Long.parseLong(actionVni.segmentationId())).build(); |
| 161 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 162 | .builder().withTreatment(treatment).withSelector(selector) |
| 163 | .fromApp(appId).withFlag(Flag.SPECIFIC) |
Phanendra Manda | dbe60d8 | 2015-11-27 13:10:11 +0530 | [diff] [blame] | 164 | .withPriority(L3_CLASSIFIER_PRIORITY); |
lishuai | 478d02e | 2015-11-26 19:33:41 +0800 | [diff] [blame] | 165 | if (type.equals(Objective.Operation.ADD)) { |
| 166 | log.debug("L3InternalClassifierRules-->ADD"); |
| 167 | flowObjectiveService.forward(deviceId, objective.add()); |
| 168 | } else { |
| 169 | log.debug("L3InternalClassifierRules-->REMOVE"); |
| 170 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 171 | } |
lishuai | 7547db4 | 2015-11-20 14:56:11 +0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
lishuai | aca6026 | 2015-11-26 19:48:01 +0800 | [diff] [blame] | 175 | public void programArpClassifierRules(DeviceId deviceId, IpAddress dstIp, |
| 176 | SegmentationId actionVni, |
| 177 | Objective.Operation type) { |
| 178 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 179 | .matchEthType(ETH_TYPE.ethType().toShort()) |
| 180 | .matchArpTpa(Ip4Address.valueOf(dstIp.toString())) |
| 181 | .build(); |
| 182 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 183 | .setTunnelId(Long.parseLong(actionVni.segmentationId())) |
| 184 | .build(); |
| 185 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 186 | .builder().withTreatment(treatment).withSelector(selector) |
| 187 | .fromApp(appId).withFlag(Flag.SPECIFIC) |
Phanendra Manda | dbe60d8 | 2015-11-27 13:10:11 +0530 | [diff] [blame] | 188 | .withPriority(ARP_CLASSIFIER_PRIORITY); |
lishuai | aca6026 | 2015-11-26 19:48:01 +0800 | [diff] [blame] | 189 | if (type.equals(Objective.Operation.ADD)) { |
| 190 | log.debug("ArpClassifierRules-->ADD"); |
| 191 | flowObjectiveService.forward(deviceId, objective.add()); |
| 192 | } else { |
| 193 | log.debug("ArpClassifierRules-->REMOVE"); |
| 194 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 195 | } |
lishuai | 7547db4 | 2015-11-20 14:56:11 +0800 | [diff] [blame] | 196 | } |
| 197 | |
Wu wenbin | acc10ea | 2016-05-06 16:48:56 +0800 | [diff] [blame] | 198 | @Override |
lishuai | cfd96d2 | 2016-05-09 16:00:01 +0800 | [diff] [blame] | 199 | public void programArpClassifierRules(DeviceId deviceId, PortNumber inPort, |
| 200 | IpAddress dstIp, |
| 201 | SegmentationId actionVni, |
| 202 | Objective.Operation type) { |
| 203 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 204 | .matchInPort(inPort).matchEthType(ETH_TYPE.ethType().toShort()) |
| 205 | .matchArpTpa(Ip4Address.valueOf(dstIp.toString())).build(); |
| 206 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 207 | .setTunnelId(Long.parseLong(actionVni.segmentationId())) |
| 208 | .build(); |
| 209 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 210 | .builder().withTreatment(treatment).withSelector(selector) |
| 211 | .fromApp(appId).withFlag(Flag.SPECIFIC) |
| 212 | .withPriority(ARP_CLASSIFIER_PRIORITY); |
| 213 | if (type.equals(Objective.Operation.ADD)) { |
| 214 | log.debug("ArpClassifierRules-->ADD"); |
| 215 | flowObjectiveService.forward(deviceId, objective.add()); |
| 216 | } else { |
| 217 | log.debug("ArpClassifierRules-->REMOVE"); |
| 218 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | @Override |
Wu wenbin | acc10ea | 2016-05-06 16:48:56 +0800 | [diff] [blame] | 223 | public void programUserdataClassifierRules(DeviceId deviceId, |
| 224 | IpPrefix ipPrefix, |
| 225 | IpAddress dstIp, |
| 226 | MacAddress dstmac, |
| 227 | SegmentationId actionVni, |
| 228 | Objective.Operation type) { |
| 229 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 230 | .matchEthType(Ethernet.TYPE_IPV4).matchIPSrc(ipPrefix) |
| 231 | .matchIPDst(IpPrefix.valueOf(dstIp, 32)).build(); |
| 232 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 233 | .setTunnelId(Long.parseLong(actionVni.segmentationId())) |
| 234 | .setEthDst(dstmac).build(); |
| 235 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 236 | .builder().withTreatment(treatment).withSelector(selector) |
| 237 | .fromApp(appId).withFlag(Flag.SPECIFIC) |
| 238 | .withPriority(USERDATA_CLASSIFIER_PRIORITY); |
| 239 | if (type.equals(Objective.Operation.ADD)) { |
| 240 | log.debug("UserdataClassifierRules-->ADD"); |
| 241 | flowObjectiveService.forward(deviceId, objective.add()); |
| 242 | } else { |
| 243 | log.debug("UserdataClassifierRules-->REMOVE"); |
| 244 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 245 | } |
| 246 | } |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 247 | |
| 248 | @Override |
| 249 | public void programExportPortArpClassifierRules(Port exportPort, |
| 250 | DeviceId deviceId, |
| 251 | Operation type) { |
| 252 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 253 | .matchEthType(EtherType.ARP.ethType().toShort()) |
| 254 | .matchInPort(exportPort.number()).build(); |
| 255 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 256 | treatment.add(Instructions.createOutput(PortNumber.CONTROLLER)); |
| 257 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 258 | .builder().withTreatment(treatment.build()) |
| 259 | .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) |
| 260 | .withPriority(L3_CLASSIFIER_PRIORITY); |
| 261 | if (type.equals(Objective.Operation.ADD)) { |
| 262 | flowObjectiveService.forward(deviceId, objective.add()); |
| 263 | } else { |
| 264 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 265 | } |
| 266 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 267 | } |