blob: 08e1ee2536dac8724ab9ed46e4de82b88712c681 [file] [log] [blame]
Jonathan Hart3930f632015-10-19 12:12:51 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hart3930f632015-10-19 12:12:51 -07003 *
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.routing.fpm.protocol;
18
19import com.google.common.base.MoreObjects;
20import org.onlab.packet.DeserializationException;
21
22import java.nio.ByteBuffer;
23
24import static org.onlab.packet.PacketUtils.checkInput;
25
26/**
27 * Netlink header.
28 * <p>
29 * Taken from struct nlmsghdr in linux/netlink.h
30 * </p>
31 */
32public final class Netlink {
33
34 public static final int NETLINK_HEADER_LENGTH = 16;
35
36 private final long length;
37 private final NetlinkMessageType type;
38 private final int flags;
39 private final long sequence;
40 private final long processPortId;
41
42 private final RtNetlink rtNetlink;
43
44 /**
45 * Class constructor.
46 *
47 * @param length message length
48 * @param type type
49 * @param flags flags
50 * @param sequence sequence number
51 * @param processPortId port ID
52 * @param rtNetlink netlink routing message
53 */
54 private Netlink(long length, NetlinkMessageType type, int flags, long sequence,
55 long processPortId, RtNetlink rtNetlink) {
56 this.length = length;
57 this.type = type;
58 this.flags = flags;
59 this.sequence = sequence;
60 this.processPortId = processPortId;
61 this.rtNetlink = rtNetlink;
62 }
63
64 /**
65 * Returns the message length.
66 *
67 * @return length
68 */
69 public long length() {
70 return length;
71 }
72
73 /**
74 * Returns the message type.
75 *
76 * @return message type
77 */
78 public NetlinkMessageType type() {
79 return type;
80 }
81
82 /**
83 * Returns the flags.
84 *
85 * @return flags
86 */
87 public int flags() {
88 return flags;
89 }
90
91 /**
92 * Returns the sequence number.
93 *
94 * @return sequence number
95 */
96 public long sequence() {
97 return sequence;
98 }
99
100 /**
101 * Returns the port ID.
102 *
103 * @return port ID
104 */
105 public long processPortId() {
106 return processPortId;
107 }
108
109 /**
110 * Returns the netlink routing message.
111 *
112 * @return netlink routing message
113 */
114 public RtNetlink rtNetlink() {
115 return rtNetlink;
116 }
117
118 @Override
119 public String toString() {
120 return MoreObjects.toStringHelper(getClass())
121 .add("length", length)
122 .add("type", type)
123 .add("flags", flags)
124 .add("sequence", sequence)
125 .add("processPortId", processPortId)
126 .add("rtNetlink", rtNetlink)
127 .toString();
128 }
129
130 /**
131 * Decodes a netlink header from an input buffer.
132 *
133 * @param buffer input buffer
134 * @param start starting position the netlink header
135 * @param length length of the message
136 * @return netlink header
137 * @throws DeserializationException if a netlink header could not be
138 * decoded from the input buffer
139 */
140 public static Netlink decode(byte[] buffer, int start, int length) throws
141 DeserializationException {
142 checkInput(buffer, start, length, NETLINK_HEADER_LENGTH);
143
144 ByteBuffer bb = ByteBuffer.wrap(buffer, start, length);
145
146 long messageLength = Integer.reverseBytes(bb.getInt());
147 int type = Short.reverseBytes(bb.getShort());
148 int flags = Short.reverseBytes(bb.getShort());
149 long sequence = Integer.reverseBytes(bb.getInt());
150 long processPortId = Integer.reverseBytes(bb.getInt());
151
152 NetlinkMessageType messageType = NetlinkMessageType.get(type);
153 if (messageType == null) {
154 throw new DeserializationException(
155 "Unsupported Netlink message type: " + type);
156 }
157
158 // Netlink messages from Quagga's FPM protocol are always in the
159 // netlink route family (family 0).
160 RtNetlink rtNetlink = RtNetlink.decode(buffer, bb.position(),
161 bb.limit() - bb.position());
162
163 return new Netlink(messageLength,
164 messageType,
165 flags,
166 sequence,
167 processPortId,
168 rtNetlink);
169 }
170
171}