lishuai | 404b0fc | 2015-12-04 09:59:59 +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 | 404b0fc | 2015-12-04 09:59:59 +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; |
| 23 | import org.onlab.packet.EthType.EtherType; |
| 24 | import org.onlab.packet.Ip4Address; |
| 25 | import org.onlab.packet.IpAddress; |
| 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.behaviour.ExtensionTreatmentResolver; |
| 31 | import org.onosproject.net.driver.DriverHandler; |
| 32 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 34 | import org.onosproject.net.flow.TrafficSelector; |
| 35 | import org.onosproject.net.flow.TrafficTreatment; |
| 36 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 37 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 38 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 39 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 40 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 41 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
| 42 | import org.onosproject.net.flowobjective.Objective; |
| 43 | import org.onosproject.net.flowobjective.Objective.Operation; |
| 44 | import org.onosproject.vtn.table.ArpService; |
| 45 | import org.onosproject.vtnrsc.SegmentationId; |
| 46 | import org.slf4j.Logger; |
| 47 | |
| 48 | /** |
| 49 | * ArpTable class providing the rules in ARP table. |
| 50 | */ |
| 51 | public class ArpServiceImpl implements ArpService { |
| 52 | private final Logger log = getLogger(getClass()); |
| 53 | |
| 54 | private static final int ARP_PRIORITY = 0xffff; |
| 55 | private static final short ARP_RESPONSE = 0x2; |
| 56 | private static final EtherType ARP_TYPE = EtherType.ARP; |
| 57 | |
| 58 | private final FlowObjectiveService flowObjectiveService; |
| 59 | private final ApplicationId appId; |
| 60 | |
| 61 | /** |
| 62 | * Construct a ArpServiceImpl object. |
| 63 | * |
| 64 | * @param appId the application id of vtn |
| 65 | */ |
| 66 | public ArpServiceImpl(ApplicationId appId) { |
| 67 | this.appId = checkNotNull(appId, "ApplicationId can not be null"); |
| 68 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 69 | this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void programArpRules(DriverHandler hander, DeviceId deviceId, |
| 74 | IpAddress dstIP, SegmentationId srcVni, |
| 75 | MacAddress dstMac, Operation type) { |
| 76 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 77 | .matchEthType(ARP_TYPE.ethType().toShort()) |
| 78 | .matchArpTpa(Ip4Address.valueOf(dstIP.toString())) |
| 79 | .matchTunnelId(Long.parseLong(srcVni.segmentationId())).build(); |
| 80 | |
| 81 | ExtensionTreatmentResolver resolver = hander |
| 82 | .behaviour(ExtensionTreatmentResolver.class); |
| 83 | ExtensionTreatment ethSrcToDst = resolver |
| 84 | .getExtensionInstruction(ExtensionTreatmentType.ExtensionTreatmentTypes |
| 85 | .NICIRA_MOV_ETH_SRC_TO_DST.type()); |
| 86 | ExtensionTreatment arpShaToTha = resolver |
| 87 | .getExtensionInstruction(ExtensionTreatmentType.ExtensionTreatmentTypes |
| 88 | .NICIRA_MOV_ARP_SHA_TO_THA.type()); |
| 89 | ExtensionTreatment arpSpaToTpa = resolver |
| 90 | .getExtensionInstruction(ExtensionTreatmentType.ExtensionTreatmentTypes |
| 91 | .NICIRA_MOV_ARP_SPA_TO_TPA.type()); |
| 92 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 93 | .extension(ethSrcToDst, deviceId) |
| 94 | .setEthSrc(dstMac).setArpOp(ARP_RESPONSE) |
| 95 | .extension(arpShaToTha, deviceId) |
| 96 | .extension(arpSpaToTpa, deviceId) |
| 97 | .setArpSha(dstMac).setArpSpa(dstIP) |
| 98 | .setOutput(PortNumber.IN_PORT).build(); |
| 99 | |
| 100 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 101 | .builder().withTreatment(treatment).withSelector(selector) |
| 102 | .fromApp(appId).withFlag(Flag.SPECIFIC) |
| 103 | .withPriority(ARP_PRIORITY); |
| 104 | |
| 105 | if (type.equals(Objective.Operation.ADD)) { |
| 106 | log.debug("PrivateArpRules-->ADD"); |
| 107 | flowObjectiveService.forward(deviceId, objective.add()); |
| 108 | } else { |
| 109 | log.debug("PrivateArpRules-->REMOVE"); |
| 110 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 111 | } |
| 112 | } |
| 113 | } |