2 * net/tipc/msg.c: TIPC message header routines
4 * Copyright (c) 2000-2006, Ericsson AB
5 * Copyright (c) 2005, 2010-2011, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
40 u32
tipc_msg_tot_importance(struct tipc_msg
*m
)
42 if (likely(msg_isdata(m
))) {
43 if (likely(msg_orignode(m
) == tipc_own_addr
))
44 return msg_importance(m
);
45 return msg_importance(m
) + 4;
47 if ((msg_user(m
) == MSG_FRAGMENTER
) &&
48 (msg_type(m
) == FIRST_FRAGMENT
))
49 return msg_importance(msg_get_wrapped(m
));
50 return msg_importance(m
);
54 void tipc_msg_init(struct tipc_msg
*m
, u32 user
, u32 type
,
55 u32 hsize
, u32 destnode
)
59 msg_set_user(m
, user
);
60 msg_set_hdr_sz(m
, hsize
);
61 msg_set_size(m
, hsize
);
62 msg_set_prevnode(m
, tipc_own_addr
);
63 msg_set_type(m
, type
);
65 msg_set_orignode(m
, tipc_own_addr
);
66 msg_set_destnode(m
, destnode
);
71 * tipc_msg_calc_data_size - determine total data size for message
74 int tipc_msg_calc_data_size(struct iovec
const *msg_sect
, u32 num_sect
)
79 for (i
= 0; i
< num_sect
; i
++)
80 dsz
+= msg_sect
[i
].iov_len
;
85 * tipc_msg_build - create message using specified header and data
87 * Note: Caller must not hold any locks in case copy_from_user() is interrupted!
89 * Returns message data size or errno
92 int tipc_msg_build(struct tipc_msg
*hdr
,
93 struct iovec
const *msg_sect
, u32 num_sect
,
94 int max_size
, int usrmem
, struct sk_buff
**buf
)
96 int dsz
, sz
, hsz
, pos
, res
, cnt
;
98 dsz
= tipc_msg_calc_data_size(msg_sect
, num_sect
);
99 if (unlikely(dsz
> TIPC_MAX_USER_MSG_SIZE
)) {
104 pos
= hsz
= msg_hdr_sz(hdr
);
106 msg_set_size(hdr
, sz
);
107 if (unlikely(sz
> max_size
)) {
112 *buf
= tipc_buf_acquire(sz
);
115 skb_copy_to_linear_data(*buf
, hdr
, hsz
);
116 for (res
= 1, cnt
= 0; res
&& (cnt
< num_sect
); cnt
++) {
118 res
= !copy_from_user((*buf
)->data
+ pos
,
119 msg_sect
[cnt
].iov_base
,
120 msg_sect
[cnt
].iov_len
);
122 skb_copy_to_linear_data_offset(*buf
, pos
,
123 msg_sect
[cnt
].iov_base
,
124 msg_sect
[cnt
].iov_len
);
125 pos
+= msg_sect
[cnt
].iov_len
;
135 #ifdef CONFIG_TIPC_DEBUG
137 void tipc_msg_dbg(struct print_buf
*buf
, struct tipc_msg
*msg
, const char *str
)
139 u32 usr
= msg_user(msg
);
140 tipc_printf(buf
, KERN_DEBUG
);
141 tipc_printf(buf
, str
);
145 tipc_printf(buf
, "BNDL::");
146 tipc_printf(buf
, "MSGS(%u):", msg_msgcnt(msg
));
149 tipc_printf(buf
, "BCASTP::");
152 tipc_printf(buf
, "FRAGM::");
153 switch (msg_type(msg
)) {
155 tipc_printf(buf
, "FIRST:");
158 tipc_printf(buf
, "BODY:");
161 tipc_printf(buf
, "LAST:");
164 tipc_printf(buf
, "UNKNOWN:%x", msg_type(msg
));
167 tipc_printf(buf
, "NO(%u/%u):", msg_long_msgno(msg
),
170 case TIPC_LOW_IMPORTANCE
:
171 case TIPC_MEDIUM_IMPORTANCE
:
172 case TIPC_HIGH_IMPORTANCE
:
173 case TIPC_CRITICAL_IMPORTANCE
:
174 tipc_printf(buf
, "DAT%u:", msg_user(msg
));
175 if (msg_short(msg
)) {
176 tipc_printf(buf
, "CON:");
179 switch (msg_type(msg
)) {
181 tipc_printf(buf
, "CON:");
184 tipc_printf(buf
, "MCST:");
187 tipc_printf(buf
, "NAM:");
189 case TIPC_DIRECT_MSG
:
190 tipc_printf(buf
, "DIR:");
193 tipc_printf(buf
, "UNKNOWN TYPE %u", msg_type(msg
));
195 if (msg_reroute_cnt(msg
))
196 tipc_printf(buf
, "REROUTED(%u):",
197 msg_reroute_cnt(msg
));
199 case NAME_DISTRIBUTOR
:
200 tipc_printf(buf
, "NMD::");
201 switch (msg_type(msg
)) {
203 tipc_printf(buf
, "PUBL(%u):", (msg_size(msg
) - msg_hdr_sz(msg
)) / 20); /* Items */
206 tipc_printf(buf
, "WDRW:");
209 tipc_printf(buf
, "UNKNOWN:%x", msg_type(msg
));
211 if (msg_reroute_cnt(msg
))
212 tipc_printf(buf
, "REROUTED(%u):",
213 msg_reroute_cnt(msg
));
216 tipc_printf(buf
, "CONN_MNG:");
217 switch (msg_type(msg
)) {
219 tipc_printf(buf
, "PROBE:");
221 case CONN_PROBE_REPLY
:
222 tipc_printf(buf
, "PROBE_REPLY:");
225 tipc_printf(buf
, "CONN_ACK:");
226 tipc_printf(buf
, "ACK(%u):", msg_msgcnt(msg
));
229 tipc_printf(buf
, "UNKNOWN TYPE:%x", msg_type(msg
));
231 if (msg_reroute_cnt(msg
))
232 tipc_printf(buf
, "REROUTED(%u):", msg_reroute_cnt(msg
));
235 switch (msg_type(msg
)) {
237 tipc_printf(buf
, "STATE:");
238 tipc_printf(buf
, "%s:", msg_probe(msg
) ? "PRB" : "");
239 tipc_printf(buf
, "NXS(%u):", msg_next_sent(msg
));
240 tipc_printf(buf
, "GAP(%u):", msg_seq_gap(msg
));
241 tipc_printf(buf
, "LSTBC(%u):", msg_last_bcast(msg
));
244 tipc_printf(buf
, "RESET:");
245 if (msg_size(msg
) != msg_hdr_sz(msg
))
246 tipc_printf(buf
, "BEAR:%s:", msg_data(msg
));
249 tipc_printf(buf
, "ACTIVATE:");
252 tipc_printf(buf
, "UNKNOWN TYPE:%x", msg_type(msg
));
254 tipc_printf(buf
, "PLANE(%c):", msg_net_plane(msg
));
255 tipc_printf(buf
, "SESS(%u):", msg_session(msg
));
257 case CHANGEOVER_PROTOCOL
:
258 tipc_printf(buf
, "TUNL:");
259 switch (msg_type(msg
)) {
261 tipc_printf(buf
, "DUPL:");
264 tipc_printf(buf
, "ORIG:");
265 tipc_printf(buf
, "EXP(%u)", msg_msgcnt(msg
));
268 tipc_printf(buf
, "UNKNOWN TYPE:%x", msg_type(msg
));
272 tipc_printf(buf
, "CFG:");
273 switch (msg_type(msg
)) {
275 tipc_printf(buf
, "DSC_REQ:");
278 tipc_printf(buf
, "DSC_RESP:");
281 tipc_printf(buf
, "UNKNOWN TYPE:%x:", msg_type(msg
));
286 tipc_printf(buf
, "UNKNOWN USER:");
291 case TIPC_LOW_IMPORTANCE
:
292 case TIPC_MEDIUM_IMPORTANCE
:
293 case TIPC_HIGH_IMPORTANCE
:
294 case TIPC_CRITICAL_IMPORTANCE
:
295 switch (msg_errcode(msg
)) {
298 case TIPC_ERR_NO_NAME
:
299 tipc_printf(buf
, "NO_NAME:");
301 case TIPC_ERR_NO_PORT
:
302 tipc_printf(buf
, "NO_PORT:");
304 case TIPC_ERR_NO_NODE
:
305 tipc_printf(buf
, "NO_PROC:");
307 case TIPC_ERR_OVERLOAD
:
308 tipc_printf(buf
, "OVERLOAD:");
310 case TIPC_CONN_SHUTDOWN
:
311 tipc_printf(buf
, "SHUTDOWN:");
314 tipc_printf(buf
, "UNKNOWN ERROR(%x):",
321 tipc_printf(buf
, "HZ(%u):", msg_hdr_sz(msg
));
322 tipc_printf(buf
, "SZ(%u):", msg_size(msg
));
323 tipc_printf(buf
, "SQNO(%u):", msg_seqno(msg
));
325 if (msg_non_seq(msg
))
326 tipc_printf(buf
, "NOSEQ:");
328 tipc_printf(buf
, "ACK(%u):", msg_ack(msg
));
329 tipc_printf(buf
, "BACK(%u):", msg_bcast_ack(msg
));
330 tipc_printf(buf
, "PRND(%x)", msg_prevnode(msg
));
332 if (msg_isdata(msg
)) {
333 if (msg_named(msg
)) {
334 tipc_printf(buf
, "NTYP(%u):", msg_nametype(msg
));
335 tipc_printf(buf
, "NINST(%u)", msg_nameinst(msg
));
339 if ((usr
!= LINK_PROTOCOL
) && (usr
!= LINK_CONFIG
) &&
340 (usr
!= MSG_BUNDLER
)) {
341 if (!msg_short(msg
)) {
342 tipc_printf(buf
, ":ORIG(%x:%u):",
343 msg_orignode(msg
), msg_origport(msg
));
344 tipc_printf(buf
, ":DEST(%x:%u):",
345 msg_destnode(msg
), msg_destport(msg
));
347 tipc_printf(buf
, ":OPRT(%u):", msg_origport(msg
));
348 tipc_printf(buf
, ":DPRT(%u):", msg_destport(msg
));
351 if (msg_user(msg
) == NAME_DISTRIBUTOR
) {
352 tipc_printf(buf
, ":ONOD(%x):", msg_orignode(msg
));
353 tipc_printf(buf
, ":DNOD(%x):", msg_destnode(msg
));
356 if (msg_user(msg
) == LINK_CONFIG
) {
357 u32
*raw
= (u32
*)msg
;
358 struct tipc_media_addr
*orig
= (struct tipc_media_addr
*)&raw
[5];
359 tipc_printf(buf
, ":DDOM(%x):", msg_dest_domain(msg
));
360 tipc_printf(buf
, ":NETID(%u):", msg_bc_netid(msg
));
361 tipc_media_addr_printf(buf
, orig
);
363 if (msg_user(msg
) == BCAST_PROTOCOL
) {
364 tipc_printf(buf
, "BCNACK:AFTER(%u):", msg_bcgap_after(msg
));
365 tipc_printf(buf
, "TO(%u):", msg_bcgap_to(msg
));
367 tipc_printf(buf
, "\n");
368 if ((usr
== CHANGEOVER_PROTOCOL
) && (msg_msgcnt(msg
)))
369 tipc_msg_dbg(buf
, msg_get_wrapped(msg
), " /");
370 if ((usr
== MSG_FRAGMENTER
) && (msg_type(msg
) == FIRST_FRAGMENT
))
371 tipc_msg_dbg(buf
, msg_get_wrapped(msg
), " /");