lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 19 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
| 22 | import org.onlab.osgi.DefaultServiceDirectory; |
| 23 | import org.onlab.osgi.ServiceDirectory; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 24 | import org.onlab.packet.Ip4Address; |
| 25 | import org.onlab.packet.IpAddress; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onosproject.core.ApplicationId; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 28 | import org.onosproject.core.DefaultGroupId; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.PortNumber; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 31 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
| 32 | import org.onosproject.net.driver.DriverHandler; |
| 33 | import org.onosproject.net.driver.DriverService; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.TrafficTreatment.Builder; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 39 | import org.onosproject.net.flow.criteria.Criteria; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 40 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 41 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 42 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 43 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 44 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
| 45 | import org.onosproject.net.flowobjective.Objective; |
| 46 | import org.onosproject.vtn.table.L2ForwardService; |
| 47 | import org.onosproject.vtnrsc.SegmentationId; |
| 48 | import org.slf4j.Logger; |
| 49 | |
| 50 | import com.google.common.collect.Sets; |
| 51 | |
| 52 | /** |
| 53 | * Provides implementation of L2ForwardService. |
| 54 | */ |
| 55 | public final class L2ForwardServiceImpl implements L2ForwardService { |
| 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
| 58 | private static final int MAC_PRIORITY = 0xffff; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 59 | public static final Integer GROUP_ID = 1; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 60 | private final FlowObjectiveService flowObjectiveService; |
| 61 | private final ApplicationId appId; |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 62 | private final DriverService driverService; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 63 | /** |
| 64 | * Constructor. |
| 65 | * |
| 66 | * @param appId the application id of vtn |
| 67 | */ |
| 68 | public L2ForwardServiceImpl(ApplicationId appId) { |
| 69 | this.appId = checkNotNull(appId, "ApplicationId can not be null"); |
| 70 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 71 | this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class); |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 72 | this.driverService = serviceDirectory.get(DriverService.class); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void programLocalBcastRules(DeviceId deviceId, |
| 77 | SegmentationId segmentationId, |
| 78 | PortNumber inPort, |
| 79 | Iterable<PortNumber> localVmPorts, |
| 80 | Iterable<PortNumber> localTunnelPorts, |
| 81 | Objective.Operation type) { |
| 82 | if (localVmPorts == null || localTunnelPorts == null) { |
| 83 | log.info("No other host port and tunnel in the device"); |
| 84 | return; |
| 85 | } |
| 86 | Sets.newHashSet(localVmPorts).stream().forEach(lp -> { |
| 87 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 88 | .matchInPort(lp).matchEthDst(MacAddress.BROADCAST) |
| 89 | .add(Criteria.matchTunnelId(Long |
| 90 | .parseLong(segmentationId.toString()))) |
| 91 | .build(); |
| 92 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 93 | .builder(); |
| 94 | boolean flag = false; |
| 95 | for (PortNumber outPort : localVmPorts) { |
| 96 | flag = true; |
| 97 | if (outPort != lp) { |
| 98 | treatment.setOutput(outPort); |
| 99 | } |
| 100 | } |
lishuai | 590d93a | 2015-12-11 13:05:14 +0800 | [diff] [blame] | 101 | if (type == Objective.Operation.REMOVE && inPort.equals(lp)) { |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 102 | flag = false; |
| 103 | } |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 104 | treatment.group(new DefaultGroupId(GROUP_ID)); |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 105 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 106 | .builder().withTreatment(treatment.build()) |
| 107 | .withSelector(selector).fromApp(appId).makePermanent() |
| 108 | .withFlag(Flag.SPECIFIC).withPriority(MAC_PRIORITY); |
| 109 | if (flag) { |
| 110 | flowObjectiveService.forward(deviceId, objective.add()); |
| 111 | } else { |
| 112 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 113 | } |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void programTunnelBcastRules(DeviceId deviceId, |
| 119 | SegmentationId segmentationId, |
| 120 | Iterable<PortNumber> localVmPorts, |
| 121 | Iterable<PortNumber> localTunnelPorts, |
| 122 | Objective.Operation type) { |
| 123 | if (localVmPorts == null || localTunnelPorts == null) { |
| 124 | log.info("No other host port or tunnel ports in the device"); |
| 125 | return; |
| 126 | } |
| 127 | Sets.newHashSet(localTunnelPorts).stream().forEach(tp -> { |
| 128 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 129 | .matchInPort(tp) |
| 130 | .add(Criteria.matchTunnelId(Long |
| 131 | .parseLong(segmentationId.toString()))) |
| 132 | .matchEthDst(MacAddress.BROADCAST).build(); |
| 133 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 134 | .builder(); |
| 135 | |
| 136 | for (PortNumber outPort : localVmPorts) { |
| 137 | treatment.setOutput(outPort); |
| 138 | } |
| 139 | |
| 140 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 141 | .builder().withTreatment(treatment.build()) |
| 142 | .withSelector(selector).fromApp(appId).makePermanent() |
| 143 | .withFlag(Flag.SPECIFIC).withPriority(MAC_PRIORITY); |
| 144 | if (type.equals(Objective.Operation.ADD)) { |
| 145 | if (Sets.newHashSet(localVmPorts).size() == 0) { |
| 146 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 147 | } else { |
| 148 | flowObjectiveService.forward(deviceId, objective.add()); |
| 149 | } |
| 150 | } else { |
| 151 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 152 | } |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void programLocalOut(DeviceId deviceId, |
| 158 | SegmentationId segmentationId, |
| 159 | PortNumber outPort, MacAddress sourceMac, |
| 160 | Objective.Operation type) { |
| 161 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 162 | .matchTunnelId(Long.parseLong(segmentationId.toString())) |
| 163 | .matchEthDst(sourceMac).build(); |
| 164 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 165 | .setOutput(outPort).build(); |
| 166 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 167 | .builder().withTreatment(treatment).withSelector(selector) |
| 168 | .fromApp(appId).withFlag(Flag.SPECIFIC) |
| 169 | .withPriority(MAC_PRIORITY); |
| 170 | if (type.equals(Objective.Operation.ADD)) { |
| 171 | flowObjectiveService.forward(deviceId, objective.add()); |
| 172 | } else { |
| 173 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void programTunnelOut(DeviceId deviceId, |
| 180 | SegmentationId segmentationId, |
| 181 | PortNumber tunnelOutPort, MacAddress dstMac, |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 182 | Objective.Operation type, IpAddress ipAddress) { |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 183 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 184 | .matchEthDst(dstMac).add(Criteria.matchTunnelId(Long |
| 185 | .parseLong(segmentationId.toString()))) |
| 186 | .build(); |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 187 | |
| 188 | DriverHandler handler = driverService.createHandler(deviceId); |
| 189 | ExtensionTreatmentResolver resolver = handler.behaviour(ExtensionTreatmentResolver.class); |
| 190 | ExtensionTreatment treatment = resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type()); |
| 191 | try { |
| 192 | treatment.setPropertyValue("tunnelDst", Ip4Address.valueOf(ipAddress.toString())); |
| 193 | } catch (Exception e) { |
| 194 | log.error("Failed to get extension instruction to set tunnel dst {}", deviceId); |
| 195 | } |
| 196 | |
| 197 | Builder builder = DefaultTrafficTreatment.builder(); |
| 198 | builder.extension(treatment, deviceId) |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 199 | .setOutput(tunnelOutPort).build(); |
| 200 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
jiangrui | c69a7fd | 2015-11-19 15:40:01 +0800 | [diff] [blame] | 201 | .builder().withTreatment(builder.build()).withSelector(selector) |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 202 | .fromApp(appId).withFlag(Flag.SPECIFIC) |
| 203 | .withPriority(MAC_PRIORITY); |
| 204 | if (type.equals(Objective.Operation.ADD)) { |
| 205 | flowObjectiveService.forward(deviceId, objective.add()); |
| 206 | } else { |
| 207 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 208 | } |
| 209 | |
| 210 | } |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 211 | } |