Kunihiro Ishiguro | f1bff50 | 2015-01-30 15:47:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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.proxyndp; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onlab.packet.Ethernet; |
| 26 | import org.onlab.packet.IPv6; |
| 27 | import org.onlab.packet.ICMP6; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 31 | import org.onosproject.net.flow.TrafficSelector; |
| 32 | import org.onosproject.net.packet.PacketContext; |
| 33 | import org.onosproject.net.packet.PacketPriority; |
| 34 | import org.onosproject.net.packet.PacketProcessor; |
| 35 | import org.onosproject.net.packet.PacketService; |
| 36 | import org.onosproject.net.proxyarp.ProxyArpService; |
| 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | /** |
| 40 | * Sample reactive proxy ndp application. |
| 41 | */ |
| 42 | @Component(immediate = true) |
| 43 | public class ProxyNdp { |
| 44 | |
| 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected PacketService packetService; |
| 49 | |
| 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected ProxyArpService proxyArpService; |
| 52 | |
| 53 | private ProxyNdpProcessor processor = new ProxyNdpProcessor(); |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected CoreService coreService; |
| 57 | |
| 58 | private ApplicationId appId; |
| 59 | |
| 60 | @Activate |
| 61 | public void activate() { |
| 62 | appId = coreService.registerApplication("org.onosproject.proxyndp"); |
| 63 | packetService.addProcessor(processor, PacketProcessor.ADVISOR_MAX + 1); |
| 64 | |
| 65 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 66 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 67 | selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 68 | selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_SOLICITATION); |
| 69 | packetService.requestPackets(selectorBuilder.build(), |
| 70 | PacketPriority.CONTROL, appId); |
| 71 | |
| 72 | selectorBuilder = DefaultTrafficSelector.builder(); |
| 73 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
| 74 | selectorBuilder.matchIPProtocol(IPv6.PROTOCOL_ICMP6); |
| 75 | selectorBuilder.matchIcmpv6Type(ICMP6.NEIGHBOR_ADVERTISEMENT); |
| 76 | packetService.requestPackets(selectorBuilder.build(), |
| 77 | PacketPriority.CONTROL, appId); |
| 78 | |
| 79 | log.info("Started with Application ID {}", appId.id()); |
| 80 | } |
| 81 | |
| 82 | @Deactivate |
| 83 | public void deactivate() { |
| 84 | packetService.removeProcessor(processor); |
| 85 | processor = null; |
| 86 | log.info("Stopped"); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Packet processor responsible for forwarding packets along their paths. |
| 92 | */ |
| 93 | private class ProxyNdpProcessor implements PacketProcessor { |
| 94 | |
| 95 | @Override |
| 96 | public void process(PacketContext context) { |
| 97 | // Stop processing if the packet has been handled, since we |
| 98 | // can't do any more to it. |
| 99 | if (context.isHandled()) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | // Handle the neighbor discovery packet. |
| 104 | proxyArpService.handlePacket(context); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |