blob: 0fa077b5aad254ddee9538c2fb9e3bf09e39c4fa [file] [log] [blame]
Pier Ventreb6a7f342016-11-26 21:05:22 -08001/*
2 * Copyright 2016-present 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
17package org.onosproject.segmentrouting;
18
Pier Ventreb6b81d52016-12-02 08:16:05 -080019import org.onlab.packet.Ethernet;
20import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
Pier Ventreb6a7f342016-11-26 21:05:22 -080022import org.onosproject.incubator.net.neighbour.NeighbourMessageContext;
Pier Ventreb6b81d52016-12-02 08:16:05 -080023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Host;
26import org.onosproject.net.HostId;
27import org.onosproject.net.flow.DefaultTrafficTreatment;
28import org.onosproject.net.flow.TrafficTreatment;
Pier Ventreb6a7f342016-11-26 21:05:22 -080029import org.onosproject.net.host.HostService;
Pier Ventreb6b81d52016-12-02 08:16:05 -080030import org.onosproject.net.packet.DefaultOutboundPacket;
31import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
32import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventreb6a7f342016-11-26 21:05:22 -080033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
Pier Ventreb6b81d52016-12-02 08:16:05 -080036import java.nio.ByteBuffer;
37
38import static com.google.common.base.Preconditions.checkNotNull;
39
Pier Ventreb6a7f342016-11-26 21:05:22 -080040/**
Pier Ventreb6b81d52016-12-02 08:16:05 -080041 * This handler provides provides useful functions to the
42 * neighbour handlers (ARP, NDP).
Pier Ventreb6a7f342016-11-26 21:05:22 -080043 */
Pier Ventreb6b81d52016-12-02 08:16:05 -080044public class SegmentRoutingNeighbourHandler {
Pier Ventreb6a7f342016-11-26 21:05:22 -080045
46 private static Logger log = LoggerFactory.getLogger(SegmentRoutingNeighbourHandler.class);
Pier Ventreb6b81d52016-12-02 08:16:05 -080047
48 protected SegmentRoutingManager srManager;
49 protected DeviceConfiguration config;
Pier Ventreb6a7f342016-11-26 21:05:22 -080050
51 /**
Pier Ventreb6b81d52016-12-02 08:16:05 -080052 * Creates an SegmentRoutingNeighbourHandler object.
Pier Ventreb6a7f342016-11-26 21:05:22 -080053 *
Pier Ventreb6b81d52016-12-02 08:16:05 -080054 * @param srManager SegmentRoutingManager object
Pier Ventreb6a7f342016-11-26 21:05:22 -080055 */
Pier Ventreb6b81d52016-12-02 08:16:05 -080056 public SegmentRoutingNeighbourHandler(SegmentRoutingManager srManager) {
57 this.srManager = srManager;
58 this.config = checkNotNull(srManager.deviceConfiguration);
Pier Ventreb6a7f342016-11-26 21:05:22 -080059 }
60
Pier Ventreb6b81d52016-12-02 08:16:05 -080061 /**
62 * Creates an SegmentRoutingNeighbourHandler object.
Pier Ventreb6a7f342016-11-26 21:05:22 -080063 */
Pier Ventreb6b81d52016-12-02 08:16:05 -080064 public SegmentRoutingNeighbourHandler() {
65 this.srManager = null;
66 this.config = null;
67 }
68
69 /**
70 * Retrieve router (device) info.
71 *
72 * @param mac where to copy the mac
73 * @param ip where to copy the ip
74 * @param deviceId the device id
75 * @param targetAddress the target address
Pier Luigi6a83c4a2017-01-29 12:38:48 -080076 * @return true if it was possible to get the necessary info.
77 * False for errors
Pier Ventreb6b81d52016-12-02 08:16:05 -080078 */
Pier Luigi6a83c4a2017-01-29 12:38:48 -080079 protected boolean getSenderInfo(byte[] mac,
Pier Ventreb6b81d52016-12-02 08:16:05 -080080 byte[] ip,
81 DeviceId deviceId,
82 IpAddress targetAddress) {
83 byte[] senderMacAddress;
84 byte[] senderIpAddress;
Pier Luigi6a83c4a2017-01-29 12:38:48 -080085 IpAddress sender;
Pier Ventreb6b81d52016-12-02 08:16:05 -080086 try {
87 senderMacAddress = config.getDeviceMac(deviceId).toBytes();
Pier Ventre6b2c1b32016-12-09 17:26:04 -080088 if (targetAddress.isIp4()) {
Pier Luigi6a83c4a2017-01-29 12:38:48 -080089 sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp4Address());
90
Pier Ventre6b2c1b32016-12-09 17:26:04 -080091 } else {
Pier Luigi6a83c4a2017-01-29 12:38:48 -080092 sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp6Address());
Pier Ventre6b2c1b32016-12-09 17:26:04 -080093 }
Pier Luigi6a83c4a2017-01-29 12:38:48 -080094 // If sender is null we abort.
95 if (sender == null) {
96 log.warn("Sender ip is null. Aborting getSenderInfo");
97 return false;
98 }
99 senderIpAddress = sender.toOctets();
Pier Ventreb6b81d52016-12-02 08:16:05 -0800100 } catch (DeviceConfigNotFoundException e) {
Pier Luigi6a83c4a2017-01-29 12:38:48 -0800101 log.warn(e.getMessage() + " Aborting getSenderInfo");
102 return false;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800103 }
Pier Ventreb6b81d52016-12-02 08:16:05 -0800104 System.arraycopy(senderMacAddress, 0, mac, 0, senderMacAddress.length);
105 System.arraycopy(senderIpAddress, 0, ip, 0, senderIpAddress.length);
Pier Luigi6a83c4a2017-01-29 12:38:48 -0800106 return true;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800107 }
108
109 /**
110 * Utility to send a ND reply using the supplied information.
111 *
112 * @param pkt the request
113 * @param targetMac the target mac
114 * @param hostService the host service
115 */
116 protected void sendResponse(NeighbourMessageContext pkt, MacAddress targetMac, HostService hostService) {
117 HostId dstId = HostId.hostId(pkt.srcMac(), pkt.vlan());
118 Host dst = hostService.getHost(dstId);
119 if (dst == null) {
120 log.warn("Cannot send {} response to host {} - does not exist in the store",
121 pkt.protocol(), dstId);
122 return;
123 }
124 pkt.reply(targetMac);
125 }
126
127 /**
128 * Flood to all ports in the same subnet.
129 *
130 * @param packet packet to be flooded
131 * @param inPort where the packet comes from
132 * @param targetAddress the target address
133 */
134 protected void flood(Ethernet packet, ConnectPoint inPort, IpAddress targetAddress) {
135 try {
136 srManager.deviceConfiguration
137 .getSubnetPortsMap(inPort.deviceId()).forEach((subnet, ports) -> {
138 if (subnet.contains(targetAddress)) {
139 ports.stream()
140 .filter(port -> port != inPort.port())
141 .forEach(port -> {
142 forward(packet, new ConnectPoint(inPort.deviceId(), port));
143 });
Pier Ventreb6a7f342016-11-26 21:05:22 -0800144 }
Pier Ventreb6b81d52016-12-02 08:16:05 -0800145 });
146 } catch (DeviceConfigNotFoundException e) {
147 log.warn(e.getMessage()
148 + " Cannot flood in subnet as device config not available"
149 + " for device: " + inPort.deviceId());
Pier Ventreb6a7f342016-11-26 21:05:22 -0800150 }
151 }
152
Pier Ventreb6b81d52016-12-02 08:16:05 -0800153 /*
154 * Floods only on the port which have been configured with the subnet
155 * of the target address. The in port is excluded.
156 *
157 * @param pkt the ndp/arp packet and context information
158 */
159 protected void flood(NeighbourMessageContext pkt) {
160 try {
161 srManager.deviceConfiguration
162 .getSubnetPortsMap(pkt.inPort().deviceId()).forEach((subnet, ports) -> {
163 if (subnet.contains(pkt.target())) {
164 ports.stream()
165 .filter(port -> port != pkt.inPort().port())
166 .forEach(port -> {
167 ConnectPoint outPoint = new ConnectPoint(
168 pkt.inPort().deviceId(),
169 port
170 );
171 pkt.forward(outPoint);
172 });
173 }
174 });
175 } catch (DeviceConfigNotFoundException e) {
176 log.warn(e.getMessage()
177 + " Cannot flood in subnet as device config not available"
178 + " for device: " + pkt.inPort().deviceId());
179 }
180 }
181
182 /**
183 * Packet out to given port.
184 *
185 * Note: In current implementation, we expect all communication with
186 * end hosts within a subnet to be untagged.
187 * <p>
188 * For those pipelines that internally assigns a VLAN, the VLAN tag will be
189 * removed before egress.
190 * <p>
191 * For those pipelines that do not assign internal VLAN, the packet remains
192 * untagged.
193 *
194 * @param packet packet to be forwarded
195 * @param outPort where the packet should be forwarded
196 */
197 private void forward(Ethernet packet, ConnectPoint outPort) {
198 ByteBuffer buf = ByteBuffer.wrap(packet.serialize());
199
200 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
201 tbuilder.setOutput(outPort.port());
202 srManager.packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
203 tbuilder.build(), buf));
204 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800205
206}