blob: 8ef8e7ecd1d43b91d5b63a63565273d837c10172 [file] [log] [blame]
Pier Ventreb6a7f342016-11-26 21:05:22 -08001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Pier Ventreb6a7f342016-11-26 21:05:22 -08003 *
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;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070022import org.onlab.packet.VlanId;
Ray Milkey2a31aeb2017-08-03 16:28:24 -070023import org.onosproject.net.neighbour.NeighbourMessageContext;
Pier Ventreb6b81d52016-12-02 08:16:05 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.Host;
27import org.onosproject.net.HostId;
28import org.onosproject.net.flow.DefaultTrafficTreatment;
29import org.onosproject.net.flow.TrafficTreatment;
Pier Ventreb6a7f342016-11-26 21:05:22 -080030import org.onosproject.net.host.HostService;
Pier Ventreb6b81d52016-12-02 08:16:05 -080031import org.onosproject.net.packet.DefaultOutboundPacket;
32import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
33import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventreb6a7f342016-11-26 21:05:22 -080034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Pier Ventreb6b81d52016-12-02 08:16:05 -080037import java.nio.ByteBuffer;
38
39import static com.google.common.base.Preconditions.checkNotNull;
40
Pier Ventreb6a7f342016-11-26 21:05:22 -080041/**
Pier Ventreb6b81d52016-12-02 08:16:05 -080042 * This handler provides provides useful functions to the
43 * neighbour handlers (ARP, NDP).
Pier Ventreb6a7f342016-11-26 21:05:22 -080044 */
Pier Ventreb6b81d52016-12-02 08:16:05 -080045public class SegmentRoutingNeighbourHandler {
Pier Ventreb6a7f342016-11-26 21:05:22 -080046
47 private static Logger log = LoggerFactory.getLogger(SegmentRoutingNeighbourHandler.class);
Pier Ventreb6b81d52016-12-02 08:16:05 -080048
49 protected SegmentRoutingManager srManager;
50 protected DeviceConfiguration config;
Pier Ventreb6a7f342016-11-26 21:05:22 -080051
52 /**
Pier Ventreb6b81d52016-12-02 08:16:05 -080053 * Creates an SegmentRoutingNeighbourHandler object.
Pier Ventreb6a7f342016-11-26 21:05:22 -080054 *
Pier Ventreb6b81d52016-12-02 08:16:05 -080055 * @param srManager SegmentRoutingManager object
Pier Ventreb6a7f342016-11-26 21:05:22 -080056 */
Pier Ventreb6b81d52016-12-02 08:16:05 -080057 public SegmentRoutingNeighbourHandler(SegmentRoutingManager srManager) {
58 this.srManager = srManager;
59 this.config = checkNotNull(srManager.deviceConfiguration);
Pier Ventreb6a7f342016-11-26 21:05:22 -080060 }
61
Pier Ventreb6b81d52016-12-02 08:16:05 -080062 /**
63 * Creates an SegmentRoutingNeighbourHandler object.
Pier Ventreb6a7f342016-11-26 21:05:22 -080064 */
Pier Ventreb6b81d52016-12-02 08:16:05 -080065 public SegmentRoutingNeighbourHandler() {
66 this.srManager = null;
67 this.config = null;
68 }
69
70 /**
71 * Retrieve router (device) info.
72 *
73 * @param mac where to copy the mac
74 * @param ip where to copy the ip
75 * @param deviceId the device id
76 * @param targetAddress the target address
Pier Luigi6a83c4a2017-01-29 12:38:48 -080077 * @return true if it was possible to get the necessary info.
78 * False for errors
Pier Ventreb6b81d52016-12-02 08:16:05 -080079 */
Pier Luigi6a83c4a2017-01-29 12:38:48 -080080 protected boolean getSenderInfo(byte[] mac,
Pier Ventreb6b81d52016-12-02 08:16:05 -080081 byte[] ip,
82 DeviceId deviceId,
83 IpAddress targetAddress) {
84 byte[] senderMacAddress;
85 byte[] senderIpAddress;
Pier Luigi6a83c4a2017-01-29 12:38:48 -080086 IpAddress sender;
Pier Ventreb6b81d52016-12-02 08:16:05 -080087 try {
88 senderMacAddress = config.getDeviceMac(deviceId).toBytes();
Pier Ventre6b2c1b32016-12-09 17:26:04 -080089 if (targetAddress.isIp4()) {
Pier Luigi6a83c4a2017-01-29 12:38:48 -080090 sender = config.getRouterIpAddressForASubnetHost(targetAddress.getIp4Address());
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 /**
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700110 * Reads the boolean configuration for responding to unknown hosts.
111 *
112 * @return respondToUnknownHosts boolean.
113 */
114 protected boolean respondToUnknownHosts() {
115 return srManager.respondToUnknownHosts;
116 }
117
118 /**
Pier Ventreb6b81d52016-12-02 08:16:05 -0800119 * Utility to send a ND reply using the supplied information.
120 *
121 * @param pkt the request
122 * @param targetMac the target mac
123 * @param hostService the host service
Charles Chan1e544ca2018-06-29 14:28:39 -0700124 * @param isRouter true if this reply is sent on behalf of a router
Pier Ventreb6b81d52016-12-02 08:16:05 -0800125 */
Charles Chan1e544ca2018-06-29 14:28:39 -0700126 protected void sendResponse(NeighbourMessageContext pkt, MacAddress targetMac, HostService hostService,
127 boolean isRouter) {
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700128 // if this is false, check if host exists in the store
129 if (!respondToUnknownHosts()) {
130 short vlanId = pkt.packet().getQinQVID();
131 HostId dstId = HostId.hostId(pkt.srcMac(), vlanId == Ethernet.VLAN_UNTAGGED
132 ? pkt.vlan() : VlanId.vlanId(vlanId));
133 Host dst = hostService.getHost(dstId);
134 if (dst == null) {
135 log.warn("Cannot send {} response to host {} - does not exist in the store",
136 pkt.protocol(), dstId);
137 return;
138 }
Pier Ventreb6b81d52016-12-02 08:16:05 -0800139 }
Charles Chan1e544ca2018-06-29 14:28:39 -0700140 pkt.setIsRouter(isRouter);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800141 pkt.reply(targetMac);
142 }
143
144 /**
145 * Flood to all ports in the same subnet.
146 *
147 * @param packet packet to be flooded
148 * @param inPort where the packet comes from
149 * @param targetAddress the target address
150 */
151 protected void flood(Ethernet packet, ConnectPoint inPort, IpAddress targetAddress) {
152 try {
153 srManager.deviceConfiguration
154 .getSubnetPortsMap(inPort.deviceId()).forEach((subnet, ports) -> {
155 if (subnet.contains(targetAddress)) {
156 ports.stream()
157 .filter(port -> port != inPort.port())
158 .forEach(port -> {
159 forward(packet, new ConnectPoint(inPort.deviceId(), port));
160 });
Pier Ventreb6a7f342016-11-26 21:05:22 -0800161 }
Pier Ventreb6b81d52016-12-02 08:16:05 -0800162 });
163 } catch (DeviceConfigNotFoundException e) {
164 log.warn(e.getMessage()
165 + " Cannot flood in subnet as device config not available"
166 + " for device: " + inPort.deviceId());
Pier Ventreb6a7f342016-11-26 21:05:22 -0800167 }
168 }
169
Pier Ventreb6b81d52016-12-02 08:16:05 -0800170 /*
171 * Floods only on the port which have been configured with the subnet
172 * of the target address. The in port is excluded.
173 *
174 * @param pkt the ndp/arp packet and context information
175 */
176 protected void flood(NeighbourMessageContext pkt) {
177 try {
178 srManager.deviceConfiguration
179 .getSubnetPortsMap(pkt.inPort().deviceId()).forEach((subnet, ports) -> {
180 if (subnet.contains(pkt.target())) {
181 ports.stream()
182 .filter(port -> port != pkt.inPort().port())
183 .forEach(port -> {
184 ConnectPoint outPoint = new ConnectPoint(
185 pkt.inPort().deviceId(),
186 port
187 );
188 pkt.forward(outPoint);
189 });
190 }
191 });
192 } catch (DeviceConfigNotFoundException e) {
193 log.warn(e.getMessage()
194 + " Cannot flood in subnet as device config not available"
195 + " for device: " + pkt.inPort().deviceId());
196 }
197 }
198
199 /**
200 * Packet out to given port.
201 *
202 * Note: In current implementation, we expect all communication with
203 * end hosts within a subnet to be untagged.
204 * <p>
205 * For those pipelines that internally assigns a VLAN, the VLAN tag will be
206 * removed before egress.
207 * <p>
208 * For those pipelines that do not assign internal VLAN, the packet remains
209 * untagged.
210 *
211 * @param packet packet to be forwarded
212 * @param outPort where the packet should be forwarded
213 */
214 private void forward(Ethernet packet, ConnectPoint outPort) {
215 ByteBuffer buf = ByteBuffer.wrap(packet.serialize());
216
217 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder();
218 tbuilder.setOutput(outPort.port());
219 srManager.packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
220 tbuilder.build(), buf));
221 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800222
223}