Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -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.pim.impl; |
| 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.osgi.service.component.annotations.Activate; |
| 19 | import org.osgi.service.component.annotations.Component; |
| 20 | import org.osgi.service.component.annotations.Deactivate; |
| 21 | import org.osgi.service.component.annotations.Reference; |
| 22 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 23 | import org.onlab.packet.Ethernet; |
| 24 | import org.onlab.packet.IPv4; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 27 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 28 | import org.onosproject.net.flow.TrafficSelector; |
| 29 | import org.onosproject.net.mcast.MulticastRouteService; |
| 30 | import org.onosproject.net.packet.InboundPacket; |
| 31 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 32 | import org.onosproject.net.packet.PacketPriority; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 33 | import org.onosproject.net.packet.PacketProcessor; |
| 34 | import org.onosproject.net.packet.PacketService; |
| 35 | import org.slf4j.Logger; |
| 36 | |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 37 | import java.util.Optional; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 38 | |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
| 42 | * The main PIM controller class. |
| 43 | */ |
| 44 | @Component(immediate = true) |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 45 | public class PimApplication { |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 46 | private final Logger log = getLogger(getClass()); |
| 47 | |
| 48 | // Used to get the appId |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 50 | protected CoreService coreService; |
| 51 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 52 | // Register to receive PIM packets, used to send packets as well |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 54 | protected PacketService packetService; |
| 55 | |
| 56 | // Use the MulticastRouteService to manage incoming PIM Join/Prune state as well as |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 58 | protected MulticastRouteService ms; |
| 59 | |
| 60 | // Create an instance of the PIM packet handler |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 61 | protected PimPacketHandler pimPacketHandler; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 62 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 63 | // Provide interfaces to the pimInterface manager as a result of Netconfig updates. |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 65 | protected PimInterfaceService pimInterfaceManager; |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 66 | |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 67 | private final PimPacketProcessor processor = new PimPacketProcessor(); |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 68 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 69 | /** |
| 70 | * Activate the PIM component. |
| 71 | */ |
| 72 | @Activate |
| 73 | public void activate() { |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 74 | // Get our application ID |
Ray Milkey | fe6afd8 | 2018-11-26 14:03:20 -0800 | [diff] [blame] | 75 | ApplicationId appId = coreService.registerApplication("org.onosproject.pim"); |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 76 | |
| 77 | // Build the traffic selector for PIM packets |
| 78 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 79 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 80 | selector.matchIPProtocol(IPv4.PROTOCOL_PIM); |
| 81 | |
| 82 | // Use the traffic selector to tell the packet service which packets we want. |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 83 | packetService.addProcessor(processor, PacketProcessor.director(5)); |
| 84 | |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 85 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, |
| 86 | appId, Optional.empty()); |
| 87 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 88 | // Get a copy of the PIM Packet Handler |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 89 | pimPacketHandler = new PimPacketHandler(); |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 90 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 91 | log.info("Started"); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Deactivate the PIM component. |
| 96 | */ |
| 97 | @Deactivate |
| 98 | public void deactivate() { |
Jonathan Hart | 36fd31e | 2016-01-28 15:55:31 -0800 | [diff] [blame] | 99 | packetService.removeProcessor(processor); |
| 100 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 101 | log.info("Stopped"); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * The class that will receive PIM packets, sanitize them, determine the PIMInterface |
| 106 | * they arrived on, then forward them on to be processed by the appropriate entity. |
| 107 | */ |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 108 | public class PimPacketProcessor implements PacketProcessor { |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 109 | |
| 110 | @Override |
| 111 | public void process(PacketContext context) { |
| 112 | |
| 113 | // return if this packet has already been handled |
| 114 | if (context.isHandled()) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | // get the inbound packet |
| 119 | InboundPacket pkt = context.inPacket(); |
| 120 | if (pkt == null) { |
| 121 | // problem getting the inbound pkt. Log it debug to avoid spamming log file |
| 122 | log.debug("Could not retrieve packet from context"); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | // Get the ethernet header |
| 127 | Ethernet eth = pkt.parsed(); |
| 128 | if (eth == null) { |
| 129 | // problem getting the ethernet pkt. Log it debug to avoid spamming log file |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 130 | log.debug("Could not retrieve ethernet packet from the parsed packet"); |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | |
| 134 | // Get the PIM Interface the packet was received on. |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 135 | PimInterface pimi = pimInterfaceManager.getPimInterface(pkt.receivedFrom()); |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 136 | if (pimi == null) { |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Pass the packet processing off to the PIMInterface for processing. |
| 142 | * |
| 143 | * TODO: Is it possible that PIM interface processing should move to the |
| 144 | * PIMInterfaceManager directly? |
| 145 | */ |
Jonathan Hart | 7f4bc52 | 2016-02-20 11:32:43 -0800 | [diff] [blame] | 146 | pimPacketHandler.processPacket(eth, pimi); |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Rusty Eddy | 4ae5aa8 | 2015-12-15 12:58:27 -0800 | [diff] [blame] | 150 | } |