blob: 8c467bff8993d3344acb3aa0faa6ac1b552a6c3b [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.proxyarp;
alshabibc274c902014-10-03 14:58:27 -070017
alshabibc274c902014-10-03 14:58:27 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080021import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
alshabibc274c902014-10-03 14:58:27 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080025import org.onlab.packet.Ethernet;
Kunihiro Ishiguro93224452015-02-13 00:40:08 +090026import org.onlab.packet.ICMP6;
alshabib78baaf22015-02-18 18:55:45 -080027import org.onlab.packet.IPv6;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070028import org.onosproject.cfg.ComponentConfigService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080031import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.TrafficSelector;
Ayaka Koshibebfbe1d72015-03-16 16:51:41 -070033import org.onosproject.net.packet.InboundPacket;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.packet.PacketContext;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080035import org.onosproject.net.packet.PacketPriority;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.packet.PacketProcessor;
37import org.onosproject.net.packet.PacketService;
38import org.onosproject.net.proxyarp.ProxyArpService;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080039import org.osgi.service.component.ComponentContext;
alshabibc274c902014-10-03 14:58:27 -070040import org.slf4j.Logger;
41
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070042import java.util.Dictionary;
43
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080044import static com.google.common.base.Strings.isNullOrEmpty;
alshabib78baaf22015-02-18 18:55:45 -080045import static org.slf4j.LoggerFactory.getLogger;
46
alshabibc274c902014-10-03 14:58:27 -070047/**
48 * Sample reactive proxy arp application.
49 */
50@Component(immediate = true)
51public class ProxyArp {
52
alshabibc274c902014-10-03 14:58:27 -070053 private final Logger log = getLogger(getClass());
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected PacketService packetService;
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected ProxyArpService proxyArpService;
60
61 private ProxyArpProcessor processor = new ProxyArpProcessor();
62
alshabib92c65ad2014-10-08 21:56:05 -070063 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected CoreService coreService;
65
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected ComponentConfigService cfgService;
68
alshabibc274c902014-10-03 14:58:27 -070069 private ApplicationId appId;
70
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080071 @Property(name = "ipv6NeighborDiscovery", boolValue = false,
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070072 label = "Enable IPv6 Neighbor Discovery; default is false")
Ayaka Koshibebfbe1d72015-03-16 16:51:41 -070073 protected boolean ipv6NeighborDiscovery = false;
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080074
alshabibc274c902014-10-03 14:58:27 -070075 @Activate
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080076 public void activate(ComponentContext context) {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070077 cfgService.registerProperties(getClass());
Brian O'Connorabafb502014-12-02 22:26:20 -080078 appId = coreService.registerApplication("org.onosproject.proxyarp");
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080079 readComponentConfiguration(context);
80
alshabibc274c902014-10-03 14:58:27 -070081 packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1);
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080082
83 TrafficSelector.Builder selectorBuilder =
84 DefaultTrafficSelector.builder();
85 selectorBuilder.matchEthType(Ethernet.TYPE_ARP);
86 packetService.requestPackets(selectorBuilder.build(),
87 PacketPriority.CONTROL, appId);
88
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080089 if (ipv6NeighborDiscovery) {
90 // IPv6 Neighbor Solicitation packet.
91 selectorBuilder = DefaultTrafficSelector.builder();
92 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
93 selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
94 selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION);
95 packetService.requestPackets(selectorBuilder.build(),
96 PacketPriority.CONTROL, appId);
Kunihiro Ishiguro93224452015-02-13 00:40:08 +090097
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -080098 // IPv6 Neighbor Advertisement packet.
99 selectorBuilder = DefaultTrafficSelector.builder();
100 selectorBuilder.matchEthType(Ethernet.TYPE_IPV6);
101 selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6);
102 selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT);
103 packetService.requestPackets(selectorBuilder.build(),
104 PacketPriority.CONTROL, appId);
105 }
Kunihiro Ishiguro93224452015-02-13 00:40:08 +0900106
alshabibc274c902014-10-03 14:58:27 -0700107 log.info("Started with Application ID {}", appId.id());
108 }
109
110 @Deactivate
111 public void deactivate() {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700112 cfgService.unregisterProperties(getClass(), false);
alshabibc274c902014-10-03 14:58:27 -0700113 packetService.removeProcessor(processor);
114 processor = null;
115 log.info("Stopped");
116 }
117
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800118 @Modified
119 public void modified(ComponentContext context) {
120 readComponentConfiguration(context);
121 }
122
123 /**
124 * Extracts properties from the component configuration context.
125 *
126 * @param context the component context
127 */
128 private void readComponentConfiguration(ComponentContext context) {
129 Dictionary<?, ?> properties = context.getProperties();
130 Boolean flag;
131
132 flag = isPropertyEnabled(properties, "ipv6NeighborDiscovery");
133 if (flag == null) {
134 log.info("IPv6 Neighbor Discovery is not configured, " +
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700135 "using current value of {}", ipv6NeighborDiscovery);
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800136 } else {
137 ipv6NeighborDiscovery = flag;
138 log.info("Configured. IPv6 Neighbor Discovery is {}",
139 ipv6NeighborDiscovery ? "enabled" : "disabled");
140 }
141 }
142
143 /**
144 * Check property name is defined and set to true.
145 *
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700146 * @param properties properties to be looked up
Pavlin Radoslavov93b606b2015-02-25 17:28:39 -0800147 * @param propertyName the name of the property to look up
148 * @return value when the propertyName is defined or return null
149 */
150 private static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
151 String propertyName) {
152 Boolean value = null;
153 try {
154 String s = (String) properties.get(propertyName);
155 value = isNullOrEmpty(s) ? null : s.trim().equals("true");
156 } catch (ClassCastException e) {
157 // No propertyName defined.
158 value = null;
159 }
160 return value;
161 }
alshabibc274c902014-10-03 14:58:27 -0700162
163 /**
164 * Packet processor responsible for forwarding packets along their paths.
165 */
166 private class ProxyArpProcessor implements PacketProcessor {
167
168 @Override
169 public void process(PacketContext context) {
170 // Stop processing if the packet has been handled, since we
171 // can't do any more to it.
172 if (context.isHandled()) {
173 return;
174 }
Ayaka Koshibebfbe1d72015-03-16 16:51:41 -0700175 // If IPv6 NDP is disabled, don't handle IPv6 frames.
176 InboundPacket pkt = context.inPacket();
177 Ethernet ethPkt = pkt.parsed();
178 if (ethPkt == null) {
179 return;
180 }
181 if (!ipv6NeighborDiscovery && (ethPkt.getEtherType() == Ethernet.TYPE_IPV6)) {
182 return;
183 }
alshabibc274c902014-10-03 14:58:27 -0700184 //handle the arp packet.
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800185 proxyArpService.handlePacket(context);
alshabibc274c902014-10-03 14:58:27 -0700186 }
187 }
188}
189
190