Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.proxyarp; |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 17 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 18 | import java.util.Dictionary; |
| 19 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 23 | import org.apache.felix.scr.annotations.Modified; |
| 24 | import org.apache.felix.scr.annotations.Property; |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 27 | import org.onlab.packet.Ethernet; |
Kunihiro Ishiguro | 9322445 | 2015-02-13 00:40:08 +0900 | [diff] [blame] | 28 | import org.onlab.packet.ICMP6; |
alshabib | 78baaf2 | 2015-02-18 18:55:45 -0800 | [diff] [blame] | 29 | import org.onlab.packet.IPv6; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 33 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 35 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.packet.PacketProcessor; |
| 37 | import org.onosproject.net.packet.PacketService; |
| 38 | import org.onosproject.net.proxyarp.ProxyArpService; |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 39 | import org.osgi.service.component.ComponentContext; |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 42 | import static com.google.common.base.Strings.isNullOrEmpty; |
alshabib | 78baaf2 | 2015-02-18 18:55:45 -0800 | [diff] [blame] | 43 | import static org.slf4j.LoggerFactory.getLogger; |
| 44 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Sample reactive proxy arp application. |
| 47 | */ |
| 48 | @Component(immediate = true) |
| 49 | public class ProxyArp { |
| 50 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 51 | private final Logger log = getLogger(getClass()); |
| 52 | |
| 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected PacketService packetService; |
| 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected ProxyArpService proxyArpService; |
| 58 | |
| 59 | private ProxyArpProcessor processor = new ProxyArpProcessor(); |
| 60 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected CoreService coreService; |
| 63 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 64 | private ApplicationId appId; |
| 65 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 66 | @Property(name = "ipv6NeighborDiscovery", boolValue = false, |
| 67 | label = "Enable IPv6 Neighbor Discovery; default is false") |
| 68 | private boolean ipv6NeighborDiscovery = false; |
| 69 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 70 | @Activate |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 71 | public void activate(ComponentContext context) { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 72 | appId = coreService.registerApplication("org.onosproject.proxyarp"); |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 73 | readComponentConfiguration(context); |
| 74 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 75 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1); |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 76 | |
| 77 | TrafficSelector.Builder selectorBuilder = |
| 78 | DefaultTrafficSelector.builder(); |
| 79 | selectorBuilder.matchEthType(Ethernet.TYPE_ARP); |
| 80 | packetService.requestPackets(selectorBuilder.build(), |
| 81 | PacketPriority.CONTROL, appId); |
| 82 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 83 | if (ipv6NeighborDiscovery) { |
| 84 | // IPv6 Neighbor Solicitation packet. |
| 85 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 86 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 87 | selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 88 | selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION); |
| 89 | packetService.requestPackets(selectorBuilder.build(), |
| 90 | PacketPriority.CONTROL, appId); |
Kunihiro Ishiguro | 9322445 | 2015-02-13 00:40:08 +0900 | [diff] [blame] | 91 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 92 | // IPv6 Neighbor Advertisement packet. |
| 93 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 94 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 95 | selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 96 | selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 97 | packetService.requestPackets(selectorBuilder.build(), |
| 98 | PacketPriority.CONTROL, appId); |
| 99 | } |
Kunihiro Ishiguro | 9322445 | 2015-02-13 00:40:08 +0900 | [diff] [blame] | 100 | |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 101 | log.info("Started with Application ID {}", appId.id()); |
| 102 | } |
| 103 | |
| 104 | @Deactivate |
| 105 | public void deactivate() { |
| 106 | packetService.removeProcessor(processor); |
| 107 | processor = null; |
| 108 | log.info("Stopped"); |
| 109 | } |
| 110 | |
Pavlin Radoslavov | 93b606b | 2015-02-25 17:28:39 -0800 | [diff] [blame^] | 111 | @Modified |
| 112 | public void modified(ComponentContext context) { |
| 113 | readComponentConfiguration(context); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Extracts properties from the component configuration context. |
| 118 | * |
| 119 | * @param context the component context |
| 120 | */ |
| 121 | private void readComponentConfiguration(ComponentContext context) { |
| 122 | Dictionary<?, ?> properties = context.getProperties(); |
| 123 | Boolean flag; |
| 124 | |
| 125 | flag = isPropertyEnabled(properties, "ipv6NeighborDiscovery"); |
| 126 | if (flag == null) { |
| 127 | log.info("IPv6 Neighbor Discovery is not configured, " + |
| 128 | "using current value of {}", ipv6NeighborDiscovery); |
| 129 | } else { |
| 130 | ipv6NeighborDiscovery = flag; |
| 131 | log.info("Configured. IPv6 Neighbor Discovery is {}", |
| 132 | ipv6NeighborDiscovery ? "enabled" : "disabled"); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Check property name is defined and set to true. |
| 138 | * |
| 139 | * @param properties properties to be looked up |
| 140 | * @param propertyName the name of the property to look up |
| 141 | * @return value when the propertyName is defined or return null |
| 142 | */ |
| 143 | private static Boolean isPropertyEnabled(Dictionary<?, ?> properties, |
| 144 | String propertyName) { |
| 145 | Boolean value = null; |
| 146 | try { |
| 147 | String s = (String) properties.get(propertyName); |
| 148 | value = isNullOrEmpty(s) ? null : s.trim().equals("true"); |
| 149 | } catch (ClassCastException e) { |
| 150 | // No propertyName defined. |
| 151 | value = null; |
| 152 | } |
| 153 | return value; |
| 154 | } |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * Packet processor responsible for forwarding packets along their paths. |
| 158 | */ |
| 159 | private class ProxyArpProcessor implements PacketProcessor { |
| 160 | |
| 161 | @Override |
| 162 | public void process(PacketContext context) { |
| 163 | // Stop processing if the packet has been handled, since we |
| 164 | // can't do any more to it. |
| 165 | if (context.isHandled()) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | //handle the arp packet. |
Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 170 | proxyArpService.handlePacket(context); |
alshabib | c274c90 | 2014-10-03 14:58:27 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |