2 * Copyright (c) 2014, 2015 NEC Corporation. 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
9 package org.opendaylight.vtn.manager.internal.packet.cache;
11 import org.opendaylight.vtn.manager.VTNException;
13 import org.opendaylight.vtn.manager.internal.util.MiscUtils;
14 import org.opendaylight.vtn.manager.internal.util.flow.match.FlowMatchType;
15 import org.opendaylight.vtn.manager.internal.util.flow.match.VTNPortRange;
16 import org.opendaylight.vtn.manager.internal.util.flow.match.VTNUdpMatch;
17 import org.opendaylight.vtn.manager.internal.util.packet.UdpHeader;
19 import org.opendaylight.controller.sal.packet.UDP;
22 * {@code UdpPacket} class implements a cache for a {@link UDP} instance.
24 public final class UdpPacket extends PortProtoPacket<UDP>
25 implements UdpHeader {
27 * Byte offset to the checksum field in UDP header.
29 private static final int UDP_OFF_CHECKSUM = 6;
32 * Checksum value which indicates the checksum is disabled.
34 private static final short UDP_CKSUM_DISABLED = 0;
37 * A {@link UDP} packet.
42 * Construct a new instance.
44 * @param udp A {@link UDP} instance.
46 public UdpPacket(UDP udp) {
51 * Derive the source port number from the packet.
53 * @return A short integer value which represents the source port number.
56 protected short getRawSourcePort() {
57 return packet.getSourcePort();
61 * Derive the destination port number from the packet.
63 * @return A short integer value which represents the destination port
67 protected short getRawDestinationPort() {
68 return packet.getDestinationPort();
72 * Set the source port number to the given packet.
74 * @param pkt A {@link UDP} instance.
75 * @param port A short integer value which indicates the source port.
78 protected void setRawSourcePort(UDP pkt, short port) {
79 pkt.setSourcePort(port);
83 * Set the destination port number to the given packet.
85 * @param pkt A {@link UDP} instance.
86 * @param port A short integer value which indicates the destination port.
89 protected void setRawDestinationPort(UDP pkt, short port) {
90 pkt.setDestinationPort(port);
94 * Return a {@link UDP} instance to set modified values.
96 * @param doCopy {@code true} is passed if the packet configured in this
97 * instance needs to be copied.
98 * @return A {@link UDP} instance.
99 * @throws VTNException
100 * Failed to copy the packet.
103 protected UDP getPacketForWrite(boolean doCopy) throws VTNException {
106 pkt = MiscUtils.copy(packet, new UDP());
116 * Return the name of the protocol.
118 * @return {@code "UDP"}.
121 protected String getProtocolName() {
126 * Construct UDP flow match fields.
128 * @param src A {@link VTNPortRange} instance which specifies the
130 * @param dst A {@link VTNPortRange} instance which specifies the
132 * @return A {@link VTNUdpMatch} instance.
135 protected VTNUdpMatch createMatch(VTNPortRange src, VTNPortRange dst) {
136 return new VTNUdpMatch(src, dst);
140 * Return a flow match type corresponding to the source port.
142 * @return {@link FlowMatchType#UDP_SRC}.
145 public FlowMatchType getSourceMatchType() {
146 return FlowMatchType.UDP_SRC;
150 * Return a flow match type corresponding to the destination port.
152 * @return {@link FlowMatchType#UDP_DST}.
155 public FlowMatchType getDestinationMatchType() {
156 return FlowMatchType.UDP_DST;
165 public boolean updateChecksum(Inet4Packet ipv4) throws VTNException {
166 // Update checksum only if the UDP packet contains a valid checksum.
168 short sum = packet.getChecksum();
170 UDP pkt = getPacketForWrite();
171 short newSum = computeChecksum(ipv4, pkt, UDP_OFF_CHECKSUM);
172 if (newSum == UDP_CKSUM_DISABLED) {
173 // Set all bits in the checksum field instead of zero.
174 newSum = (short)~newSum;
177 pkt.setChecksum(newSum);
188 * Return a {@link UDP} instance configured in this instance.
190 * @return A {@link UDP} instance.
193 public UDP getPacket() {