lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | 9e4e43a | 2015-11-26 19:30:55 +0800 | [diff] [blame] | 23 | import org.onlab.packet.Ethernet; |
lishuai | 7547db4 | 2015-11-20 14:56:11 +0800 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
lishuai | 9e4e43a | 2015-11-26 19:30:55 +0800 | [diff] [blame] | 25 | import org.onlab.packet.IpPrefix; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.PortNumber; |
| 30 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 31 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
| 34 | import org.onosproject.net.flow.criteria.Criteria; |
| 35 | import org.onosproject.net.flow.instructions.Instructions; |
| 36 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 37 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 38 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 39 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
| 40 | import org.onosproject.net.flowobjective.Objective; |
lishuai | 7547db4 | 2015-11-20 14:56:11 +0800 | [diff] [blame] | 41 | import org.onosproject.net.flowobjective.Objective.Operation; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 42 | import org.onosproject.vtn.table.ClassifierService; |
| 43 | import org.onosproject.vtnrsc.SegmentationId; |
| 44 | import org.slf4j.Logger; |
| 45 | |
| 46 | import com.google.common.collect.Sets; |
| 47 | |
| 48 | /** |
| 49 | * Provides implementation of ClassifierService. |
| 50 | */ |
| 51 | public class ClassifierServiceImpl implements ClassifierService { |
| 52 | private final Logger log = getLogger(getClass()); |
| 53 | |
lishuai | 9e4e43a | 2015-11-26 19:30:55 +0800 | [diff] [blame] | 54 | private static final int L3_CLAFFIFIER_PRIORITY = 0xffff; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 55 | private static final int L2_CLAFFIFIER_PRIORITY = 50000; |
| 56 | |
| 57 | private final FlowObjectiveService flowObjectiveService; |
| 58 | private final ApplicationId appId; |
| 59 | |
| 60 | /** |
| 61 | * Constructor. |
| 62 | * |
| 63 | * @param appId the application id of vtn |
| 64 | */ |
| 65 | public ClassifierServiceImpl(ApplicationId appId) { |
| 66 | this.appId = checkNotNull(appId, "ApplicationId can not be null"); |
| 67 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 68 | this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void programLocalIn(DeviceId deviceId, |
| 73 | SegmentationId segmentationId, PortNumber inPort, |
| 74 | MacAddress srcMac, ApplicationId appid, |
| 75 | Objective.Operation type) { |
| 76 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 77 | .matchInPort(inPort).matchEthSrc(srcMac).build(); |
| 78 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 79 | treatment.add(Instructions |
| 80 | .modTunnelId(Long.parseLong(segmentationId.toString()))); |
| 81 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 82 | .builder().withTreatment(treatment.build()) |
| 83 | .withSelector(selector).fromApp(appId).makePermanent() |
| 84 | .withFlag(Flag.SPECIFIC).withPriority(L2_CLAFFIFIER_PRIORITY); |
| 85 | if (type.equals(Objective.Operation.ADD)) { |
| 86 | log.debug("programLocalIn-->ADD"); |
| 87 | flowObjectiveService.forward(deviceId, objective.add()); |
| 88 | } else { |
| 89 | log.debug("programLocalIn-->REMOVE"); |
| 90 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void programTunnelIn(DeviceId deviceId, |
| 96 | SegmentationId segmentationId, |
| 97 | Iterable<PortNumber> localTunnelPorts, |
| 98 | Objective.Operation type) { |
| 99 | if (localTunnelPorts == null) { |
| 100 | log.info("No tunnel port in device"); |
| 101 | return; |
| 102 | } |
| 103 | Sets.newHashSet(localTunnelPorts).stream().forEach(tp -> { |
| 104 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 105 | .matchInPort(tp).add(Criteria.matchTunnelId(Long |
| 106 | .parseLong(segmentationId.toString()))) |
| 107 | .build(); |
| 108 | |
| 109 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 110 | .build(); |
| 111 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 112 | .builder().withTreatment(treatment).withSelector(selector) |
| 113 | .fromApp(appId).makePermanent().withFlag(Flag.SPECIFIC) |
| 114 | .withPriority(L2_CLAFFIFIER_PRIORITY); |
| 115 | if (type.equals(Objective.Operation.ADD)) { |
| 116 | log.debug("programTunnelIn-->ADD"); |
| 117 | flowObjectiveService.forward(deviceId, objective.add()); |
| 118 | } else { |
| 119 | log.debug("programTunnelIn-->REMOVE"); |
| 120 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 121 | } |
| 122 | }); |
| 123 | } |
| 124 | |
lishuai | 7547db4 | 2015-11-20 14:56:11 +0800 | [diff] [blame] | 125 | @Override |
lishuai | 9e4e43a | 2015-11-26 19:30:55 +0800 | [diff] [blame] | 126 | public void programL3ExPortClassifierRules(DeviceId deviceId, PortNumber inPort, |
| 127 | IpAddress dstIp, |
| 128 | Objective.Operation type) { |
| 129 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 130 | .matchEthType(Ethernet.TYPE_IPV4).matchInPort(inPort) |
| 131 | .matchIPDst(IpPrefix.valueOf(dstIp, 32)).build(); |
| 132 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 133 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 134 | .builder().withTreatment(treatment).withSelector(selector) |
| 135 | .fromApp(appId).withFlag(Flag.SPECIFIC) |
| 136 | .withPriority(L3_CLAFFIFIER_PRIORITY); |
| 137 | if (type.equals(Objective.Operation.ADD)) { |
| 138 | log.debug("L3ExToInClassifierRules-->ADD"); |
| 139 | flowObjectiveService.forward(deviceId, objective.add()); |
| 140 | } else { |
| 141 | log.debug("L3ExToInClassifierRules-->REMOVE"); |
| 142 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 143 | } |
lishuai | 7547db4 | 2015-11-20 14:56:11 +0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
lishuai | 478d02e | 2015-11-26 19:33:41 +0800 | [diff] [blame] | 147 | public void programL3InPortClassifierRules(DeviceId deviceId, PortNumber inPort, |
| 148 | MacAddress srcMac, MacAddress dstMac, |
| 149 | SegmentationId actionVni, |
| 150 | Objective.Operation type) { |
| 151 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 152 | .matchInPort(inPort).matchEthSrc(srcMac).matchEthDst(dstMac) |
| 153 | .build(); |
| 154 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 155 | .setTunnelId(Long.parseLong(actionVni.segmentationId())).build(); |
| 156 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 157 | .builder().withTreatment(treatment).withSelector(selector) |
| 158 | .fromApp(appId).withFlag(Flag.SPECIFIC) |
| 159 | .withPriority(L3_CLAFFIFIER_PRIORITY); |
| 160 | if (type.equals(Objective.Operation.ADD)) { |
| 161 | log.debug("L3InternalClassifierRules-->ADD"); |
| 162 | flowObjectiveService.forward(deviceId, objective.add()); |
| 163 | } else { |
| 164 | log.debug("L3InternalClassifierRules-->REMOVE"); |
| 165 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 166 | } |
lishuai | 7547db4 | 2015-11-20 14:56:11 +0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void programArpClassifierRules(DeviceId deviceId, IpAddress srcGwIp, |
| 171 | SegmentationId srcVni, Operation type) { |
| 172 | // TODO Auto-generated method stub |
| 173 | } |
| 174 | |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 175 | } |