blob: a6c0bd61fcc609c030bc4b257bbed6e522a56bed [file] [log] [blame]
lishuai404b0fc2015-12-04 09:59:59 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
lishuai404b0fc2015-12-04 09:59:59 +08003 *
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 */
16package org.onosproject.vtn.table.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import org.onlab.osgi.DefaultServiceDirectory;
22import org.onlab.osgi.ServiceDirectory;
23import org.onlab.packet.EthType.EtherType;
24import org.onlab.packet.Ip4Address;
25import org.onlab.packet.IpAddress;
26import org.onlab.packet.MacAddress;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
31import org.onosproject.net.driver.DriverHandler;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.TrafficSelector;
35import org.onosproject.net.flow.TrafficTreatment;
36import org.onosproject.net.flow.instructions.ExtensionTreatment;
37import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
38import org.onosproject.net.flowobjective.DefaultForwardingObjective;
39import org.onosproject.net.flowobjective.FlowObjectiveService;
40import org.onosproject.net.flowobjective.ForwardingObjective;
41import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
42import org.onosproject.net.flowobjective.Objective;
43import org.onosproject.net.flowobjective.Objective.Operation;
44import org.onosproject.vtn.table.ArpService;
45import org.onosproject.vtnrsc.SegmentationId;
46import org.slf4j.Logger;
47
48/**
49 * ArpTable class providing the rules in ARP table.
50 */
51public 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}