2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sample.l2switch.md.packet;
10 import org.opendaylight.controller.sample.l2switch.md.addresstracker.AddressTracker;
11 import org.opendaylight.controller.sample.l2switch.md.flow.FlowWriterService;
12 import org.opendaylight.controller.sample.l2switch.md.inventory.InventoryService;
13 import org.opendaylight.controller.sample.l2switch.md.util.InstanceIdentifierUtils;
14 import org.opendaylight.controller.sal.packet.Ethernet;
15 import org.opendaylight.controller.sal.packet.LLDP;
16 import org.opendaylight.controller.sal.packet.LinkEncap;
17 import org.opendaylight.controller.sal.packet.Packet;
18 import org.opendaylight.controller.sal.packet.RawPacket;
19 import org.opendaylight.controller.sal.utils.HexEncode;
20 import org.opendaylight.controller.sal.utils.NetUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.*;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.address.tracker.rev140402.l2.addresses.L2Address;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInputBuilder;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.util.HashMap;
36 import java.util.List;
39 * PacketHandler examines Ethernet packets to find L2Addresses (mac, nodeConnector) pairings
40 * of the sender and learns them.
41 * It also forwards the data packets appropriately dependending upon whether it knows about the
44 public class PacketHandler implements PacketProcessingListener {
46 private final static Logger _logger = LoggerFactory.getLogger(PacketHandler.class);
48 private PacketProcessingService packetProcessingService;
49 private AddressTracker addressTracker;
50 private FlowWriterService flowWriterService;
51 private InventoryService inventoryService;
53 public void setAddressTracker(AddressTracker addressTracker) {
54 this.addressTracker = addressTracker;
57 public void setPacketProcessingService(PacketProcessingService packetProcessingService) {
58 this.packetProcessingService = packetProcessingService;
61 public void setFlowWriterService(FlowWriterService flowWriterService) {
62 this.flowWriterService = flowWriterService;
65 public void setInventoryService(InventoryService inventoryService) {
66 this.inventoryService = inventoryService;
70 * The handler function for all incoming packets.
71 * @param packetReceived The incoming packet.
74 public void onPacketReceived(PacketReceived packetReceived) {
76 if(packetReceived == null) return;
79 byte[] payload = packetReceived.getPayload();
80 RawPacket rawPacket = new RawPacket(payload);
81 NodeConnectorRef ingress = packetReceived.getIngress();
83 Packet packet = decodeDataPacket(rawPacket);
85 if(!(packet instanceof Ethernet)) return;
87 handleEthernetPacket(packet, ingress);
89 } catch(Throwable _e) {
95 * The handler function for Ethernet packets.
96 * @param packet The incoming Ethernet packet.
97 * @param ingress The NodeConnector where the Ethernet packet came from.
99 private void handleEthernetPacket(Packet packet, NodeConnectorRef ingress) {
100 byte[] srcMac = ((Ethernet) packet).getSourceMACAddress();
101 byte[] destMac = ((Ethernet) packet).getDestinationMACAddress();
103 if (srcMac == null || srcMac.length == 0) return;
105 Object enclosedPacket = packet.getPayload();
107 if (enclosedPacket instanceof LLDP)
108 return; // LLDP packets are handled by OpenFlowPlugin
110 // get l2address by src mac
111 // if unknown, add l2address
112 MacAddress srcMacAddress = toMacAddress(srcMac);
113 L2Address src = addressTracker.getAddress(srcMacAddress);
114 boolean isSrcKnown = (src != null);
116 addressTracker.addAddress(srcMacAddress, ingress);
119 // get host by dest mac
120 // if known set dest known to true
121 MacAddress destMacAddress = toMacAddress(destMac);
122 L2Address dest = addressTracker.getAddress(destMacAddress);
123 boolean isDestKnown = (dest != null);
125 byte[] payload = packet.getRawPayload();
126 // if (src and dest known)
127 // sendpacket to dest and add src<->dest flow
128 if(isSrcKnown & isDestKnown) {
129 flowWriterService.addMacToMacFlowsUsingShortestPath(srcMacAddress, src.getNodeConnectorRef(),
130 destMacAddress, dest.getNodeConnectorRef());
131 sendPacketOut(payload, getControllerNodeConnector(dest.getNodeConnectorRef()), dest.getNodeConnectorRef());
134 // sendpacket to external links minus ingress
135 floodExternalPorts(payload, ingress);
140 * Floods the specified payload on external ports, which are ports not connected to switches.
141 * @param payload The payload to be flooded.
142 * @param ingress The NodeConnector where the payload came from.
144 private void floodExternalPorts(byte[] payload, NodeConnectorRef ingress) {
145 List<NodeConnectorRef> externalPorts = inventoryService.getExternalNodeConnectors();
146 externalPorts.remove(ingress);
148 for (NodeConnectorRef egress : externalPorts) {
149 sendPacketOut(payload, getControllerNodeConnector(egress), egress);
154 * Sends the specified packet on the specified port.
155 * @param payload The payload to be sent.
156 * @param ingress The NodeConnector where the payload came from.
157 * @param egress The NodeConnector where the payload will go.
159 private void sendPacketOut(byte[] payload, NodeConnectorRef ingress, NodeConnectorRef egress) {
160 if (ingress == null || egress == null) return;
161 InstanceIdentifier<Node> egressNodePath = InstanceIdentifierUtils.getNodePath(egress.getValue());
162 TransmitPacketInput input = new TransmitPacketInputBuilder() //
163 .setPayload(payload) //
164 .setNode(new NodeRef(egressNodePath)) //
165 .setEgress(egress) //
166 .setIngress(ingress) //
168 packetProcessingService.transmitPacket(input);
172 * Decodes an incoming packet.
173 * @param raw The raw packet to be decoded.
174 * @return The decoded form of the raw packet.
176 private Packet decodeDataPacket(RawPacket raw) {
180 byte[] data = raw.getPacketData();
181 if(data.length <= 0) {
184 if(raw.getEncap().equals(LinkEncap.ETHERNET)) {
185 Ethernet res = new Ethernet();
187 res.deserialize(data, 0, data.length * NetUtils.NumBitsInAByte);
188 res.setRawPayload(raw.getPacketData());
189 } catch(Exception e) {
190 _logger.warn("Failed to decode packet: {}", e.getMessage());
198 * Creates a MacAddress object out of a byte array.
199 * @param dataLinkAddress The byte-array form of a MacAddress
200 * @return MacAddress of the specified dataLinkAddress.
202 private MacAddress toMacAddress(byte[] dataLinkAddress) {
203 return new MacAddress(HexEncode.bytesToHexStringFormat(dataLinkAddress));
207 * Gets the NodeConnector that connects the controller & switch for a specified switch port/node connector.
208 * @param nodeConnectorRef The nodeConnector of a switch.
209 * @return The NodeConnector that that connects the controller & switch.
211 private NodeConnectorRef getControllerNodeConnector(NodeConnectorRef nodeConnectorRef) {
212 NodeConnectorRef controllerSwitchNodeConnector = null;
213 HashMap<String, NodeConnectorRef> controllerSwitchConnectors = inventoryService.getControllerSwitchConnectors();
214 InstanceIdentifier<Node> nodePath = InstanceIdentifierUtils.getNodePath(nodeConnectorRef.getValue());
215 if (nodePath != null) {
216 NodeKey nodeKey = InstanceIdentifierUtils.getNodeKey(nodePath);
217 if (nodeKey != null) {
218 controllerSwitchNodeConnector = controllerSwitchConnectors.get(nodeKey.getId().getValue());
221 return controllerSwitchNodeConnector;