2 * net/tipc/msg.h: Include file for TIPC message header routines
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
37 #include <net/tipc/tipc_msg.h>
39 #define TIPC_VERSION 2
40 #define DATA_LOW TIPC_LOW_IMPORTANCE
41 #define DATA_MEDIUM TIPC_MEDIUM_IMPORTANCE
42 #define DATA_HIGH TIPC_HIGH_IMPORTANCE
43 #define DATA_CRITICAL TIPC_CRITICAL_IMPORTANCE
44 #define SHORT_H_SIZE 24 /* Connected,in cluster */
45 #define DIR_MSG_H_SIZE 32 /* Directly addressed messages */
46 #define CONN_MSG_H_SIZE 36 /* Routed connected msgs*/
47 #define LONG_H_SIZE 40 /* Named Messages */
48 #define MCAST_H_SIZE 44 /* Multicast messages */
49 #define MAX_H_SIZE 60 /* Inclusive full options */
50 #define MAX_MSG_SIZE (MAX_H_SIZE + TIPC_MAX_USER_MSG_SIZE)
51 #define LINK_CONFIG 13
55 TIPC user data message header format, version 2
57 - Fundamental definitions available to privileged TIPC users
58 are located in tipc_msg.h.
59 - Remaining definitions available to TIPC internal users appear below.
63 static inline void msg_set_word(struct tipc_msg *m, u32 w, u32 val)
65 m->hdr[w] = htonl(val);
68 static inline void msg_set_bits(struct tipc_msg *m, u32 w,
69 u32 pos, u32 mask, u32 val)
71 u32 word = msg_word(m,w) & ~(mask << pos);
72 msg_set_word(m, w, (word |= (val << pos)));
79 static inline u32 msg_version(struct tipc_msg *m)
81 return msg_bits(m, 0, 29, 7);
84 static inline void msg_set_version(struct tipc_msg *m)
86 msg_set_bits(m, 0, 29, 0xf, TIPC_VERSION);
89 static inline u32 msg_user(struct tipc_msg *m)
91 return msg_bits(m, 0, 25, 0xf);
94 static inline u32 msg_isdata(struct tipc_msg *m)
96 return (msg_user(m) <= DATA_CRITICAL);
99 static inline void msg_set_user(struct tipc_msg *m, u32 n)
101 msg_set_bits(m, 0, 25, 0xf, n);
104 static inline void msg_set_importance(struct tipc_msg *m, u32 i)
109 static inline void msg_set_hdr_sz(struct tipc_msg *m,u32 n)
111 msg_set_bits(m, 0, 21, 0xf, n>>2);
114 static inline int msg_non_seq(struct tipc_msg *m)
116 return msg_bits(m, 0, 20, 1);
119 static inline void msg_set_non_seq(struct tipc_msg *m)
121 msg_set_bits(m, 0, 20, 1, 1);
124 static inline int msg_dest_droppable(struct tipc_msg *m)
126 return msg_bits(m, 0, 19, 1);
129 static inline void msg_set_dest_droppable(struct tipc_msg *m, u32 d)
131 msg_set_bits(m, 0, 19, 1, d);
134 static inline int msg_src_droppable(struct tipc_msg *m)
136 return msg_bits(m, 0, 18, 1);
139 static inline void msg_set_src_droppable(struct tipc_msg *m, u32 d)
141 msg_set_bits(m, 0, 18, 1, d);
144 static inline void msg_set_size(struct tipc_msg *m, u32 sz)
146 m->hdr[0] = htonl((msg_word(m, 0) & ~0x1ffff) | sz);
154 static inline void msg_set_type(struct tipc_msg *m, u32 n)
156 msg_set_bits(m, 1, 29, 0x7, n);
159 static inline void msg_set_errcode(struct tipc_msg *m, u32 err)
161 msg_set_bits(m, 1, 25, 0xf, err);
164 static inline u32 msg_reroute_cnt(struct tipc_msg *m)
166 return msg_bits(m, 1, 21, 0xf);
169 static inline void msg_incr_reroute_cnt(struct tipc_msg *m)
171 msg_set_bits(m, 1, 21, 0xf, msg_reroute_cnt(m) + 1);
174 static inline void msg_reset_reroute_cnt(struct tipc_msg *m)
176 msg_set_bits(m, 1, 21, 0xf, 0);
179 static inline u32 msg_lookup_scope(struct tipc_msg *m)
181 return msg_bits(m, 1, 19, 0x3);
184 static inline void msg_set_lookup_scope(struct tipc_msg *m, u32 n)
186 msg_set_bits(m, 1, 19, 0x3, n);
189 static inline void msg_set_options(struct tipc_msg *m, const char *opt, u32 sz)
191 u32 hsz = msg_hdr_sz(m);
192 char *to = (char *)&m->hdr[hsz/4];
194 if ((hsz < DIR_MSG_H_SIZE) || ((hsz + sz) > MAX_H_SIZE))
196 msg_set_bits(m, 1, 16, 0x7, (hsz - 28)/4);
197 msg_set_hdr_sz(m, hsz + sz);
201 static inline u32 msg_bcast_ack(struct tipc_msg *m)
203 return msg_bits(m, 1, 0, 0xffff);
206 static inline void msg_set_bcast_ack(struct tipc_msg *m, u32 n)
208 msg_set_bits(m, 1, 0, 0xffff, n);
216 static inline u32 msg_ack(struct tipc_msg *m)
218 return msg_bits(m, 2, 16, 0xffff);
221 static inline void msg_set_ack(struct tipc_msg *m, u32 n)
223 msg_set_bits(m, 2, 16, 0xffff, n);
226 static inline u32 msg_seqno(struct tipc_msg *m)
228 return msg_bits(m, 2, 0, 0xffff);
231 static inline void msg_set_seqno(struct tipc_msg *m, u32 n)
233 msg_set_bits(m, 2, 0, 0xffff, n);
242 static inline void msg_set_prevnode(struct tipc_msg *m, u32 a)
244 msg_set_word(m, 3, a);
247 static inline void msg_set_origport(struct tipc_msg *m, u32 p)
249 msg_set_word(m, 4, p);
252 static inline void msg_set_destport(struct tipc_msg *m, u32 p)
254 msg_set_word(m, 5, p);
257 static inline void msg_set_mc_netid(struct tipc_msg *m, u32 p)
259 msg_set_word(m, 5, p);
262 static inline void msg_set_orignode(struct tipc_msg *m, u32 a)
264 msg_set_word(m, 6, a);
267 static inline void msg_set_destnode(struct tipc_msg *m, u32 a)
269 msg_set_word(m, 7, a);
272 static inline int msg_is_dest(struct tipc_msg *m, u32 d)
274 return(msg_short(m) || (msg_destnode(m) == d));
277 static inline u32 msg_routed(struct tipc_msg *m)
279 if (likely(msg_short(m)))
281 return(msg_destnode(m) ^ msg_orignode(m)) >> 11;
284 static inline void msg_set_nametype(struct tipc_msg *m, u32 n)
286 msg_set_word(m, 8, n);
289 static inline u32 msg_transp_seqno(struct tipc_msg *m)
291 return msg_word(m, 8);
294 static inline void msg_set_timestamp(struct tipc_msg *m, u32 n)
296 msg_set_word(m, 8, n);
299 static inline u32 msg_timestamp(struct tipc_msg *m)
301 return msg_word(m, 8);
304 static inline void msg_set_transp_seqno(struct tipc_msg *m, u32 n)
306 msg_set_word(m, 8, n);
309 static inline void msg_set_namelower(struct tipc_msg *m, u32 n)
311 msg_set_word(m, 9, n);
314 static inline void msg_set_nameinst(struct tipc_msg *m, u32 n)
316 msg_set_namelower(m, n);
319 static inline void msg_set_nameupper(struct tipc_msg *m, u32 n)
321 msg_set_word(m, 10, n);
324 static inline struct tipc_msg *msg_get_wrapped(struct tipc_msg *m)
326 return (struct tipc_msg *)msg_data(m);
329 static inline void msg_expand(struct tipc_msg *m, u32 destnode)
333 msg_set_hdr_sz(m, LONG_H_SIZE);
334 msg_set_orignode(m, msg_prevnode(m));
335 msg_set_destnode(m, destnode);
336 memset(&m->hdr[8], 0, 12);
342 TIPC internal message header format, version 2
344 1 0 9 8 7 6 5 4|3 2 1 0 9 8 7 6|5 4 3 2 1 0 9 8|7 6 5 4 3 2 1 0
345 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
346 w0:|vers |msg usr|hdr sz |n|resrv| packet size |
347 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
348 w1:|m typ|rsv=0| sequence gap | broadcast ack no |
349 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
350 w2:| link level ack no/bc_gap_from | seq no / bcast_gap_to |
351 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
353 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
354 w4:| next sent broadcast/fragm no | next sent pkt/ fragm msg no |
355 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
356 w5:| session no |rsv=0|r|berid|link prio|netpl|p|
357 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
358 w6:| originating node |
359 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
360 w7:| destination node |
361 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
362 w8:| transport sequence number |
363 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
364 w9:| msg count / bcast tag | link tolerance |
365 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
367 / User Specific Data /
369 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
371 NB: CONN_MANAGER use data message format. LINK_CONFIG has own format.
378 #define BCAST_PROTOCOL 5
379 #define MSG_BUNDLER 6
380 #define LINK_PROTOCOL 7
381 #define CONN_MANAGER 8
382 #define ROUTE_DISTRIBUTOR 9
383 #define CHANGEOVER_PROTOCOL 10
384 #define NAME_DISTRIBUTOR 11
385 #define MSG_FRAGMENTER 12
386 #define LINK_CONFIG 13
387 #define INT_H_SIZE 40
388 #define DSC_H_SIZE 40
391 * Connection management protocol messages
395 #define CONN_PROBE_REPLY 1
399 * Name distributor messages
402 #define PUBLICATION 0
410 static inline u32 msg_seq_gap(struct tipc_msg *m)
412 return msg_bits(m, 1, 16, 0xff);
415 static inline void msg_set_seq_gap(struct tipc_msg *m, u32 n)
417 msg_set_bits(m, 1, 16, 0xff, n);
420 static inline u32 msg_req_links(struct tipc_msg *m)
422 return msg_bits(m, 1, 16, 0xfff);
425 static inline void msg_set_req_links(struct tipc_msg *m, u32 n)
427 msg_set_bits(m, 1, 16, 0xfff, n);
435 static inline u32 msg_dest_domain(struct tipc_msg *m)
437 return msg_word(m, 2);
440 static inline void msg_set_dest_domain(struct tipc_msg *m, u32 n)
442 msg_set_word(m, 2, n);
445 static inline u32 msg_bcgap_after(struct tipc_msg *m)
447 return msg_bits(m, 2, 16, 0xffff);
450 static inline void msg_set_bcgap_after(struct tipc_msg *m, u32 n)
452 msg_set_bits(m, 2, 16, 0xffff, n);
455 static inline u32 msg_bcgap_to(struct tipc_msg *m)
457 return msg_bits(m, 2, 0, 0xffff);
460 static inline void msg_set_bcgap_to(struct tipc_msg *m, u32 n)
462 msg_set_bits(m, 2, 0, 0xffff, n);
470 static inline u32 msg_last_bcast(struct tipc_msg *m)
472 return msg_bits(m, 4, 16, 0xffff);
475 static inline void msg_set_last_bcast(struct tipc_msg *m, u32 n)
477 msg_set_bits(m, 4, 16, 0xffff, n);
481 static inline u32 msg_fragm_no(struct tipc_msg *m)
483 return msg_bits(m, 4, 16, 0xffff);
486 static inline void msg_set_fragm_no(struct tipc_msg *m, u32 n)
488 msg_set_bits(m, 4, 16, 0xffff, n);
492 static inline u32 msg_next_sent(struct tipc_msg *m)
494 return msg_bits(m, 4, 0, 0xffff);
497 static inline void msg_set_next_sent(struct tipc_msg *m, u32 n)
499 msg_set_bits(m, 4, 0, 0xffff, n);
503 static inline u32 msg_long_msgno(struct tipc_msg *m)
505 return msg_bits(m, 4, 0, 0xffff);
508 static inline void msg_set_long_msgno(struct tipc_msg *m, u32 n)
510 msg_set_bits(m, 4, 0, 0xffff, n);
513 static inline u32 msg_bc_netid(struct tipc_msg *m)
515 return msg_word(m, 4);
518 static inline void msg_set_bc_netid(struct tipc_msg *m, u32 id)
520 msg_set_word(m, 4, id);
523 static inline u32 msg_link_selector(struct tipc_msg *m)
525 return msg_bits(m, 4, 0, 1);
528 static inline void msg_set_link_selector(struct tipc_msg *m, u32 n)
530 msg_set_bits(m, 4, 0, 1, (n & 1));
537 static inline u32 msg_session(struct tipc_msg *m)
539 return msg_bits(m, 5, 16, 0xffff);
542 static inline void msg_set_session(struct tipc_msg *m, u32 n)
544 msg_set_bits(m, 5, 16, 0xffff, n);
547 static inline u32 msg_probe(struct tipc_msg *m)
549 return msg_bits(m, 5, 0, 1);
552 static inline void msg_set_probe(struct tipc_msg *m, u32 val)
554 msg_set_bits(m, 5, 0, 1, (val & 1));
557 static inline char msg_net_plane(struct tipc_msg *m)
559 return msg_bits(m, 5, 1, 7) + 'A';
562 static inline void msg_set_net_plane(struct tipc_msg *m, char n)
564 msg_set_bits(m, 5, 1, 7, (n - 'A'));
567 static inline u32 msg_linkprio(struct tipc_msg *m)
569 return msg_bits(m, 5, 4, 0x1f);
572 static inline void msg_set_linkprio(struct tipc_msg *m, u32 n)
574 msg_set_bits(m, 5, 4, 0x1f, n);
577 static inline u32 msg_bearer_id(struct tipc_msg *m)
579 return msg_bits(m, 5, 9, 0x7);
582 static inline void msg_set_bearer_id(struct tipc_msg *m, u32 n)
584 msg_set_bits(m, 5, 9, 0x7, n);
587 static inline u32 msg_redundant_link(struct tipc_msg *m)
589 return msg_bits(m, 5, 12, 0x1);
592 static inline void msg_set_redundant_link(struct tipc_msg *m)
594 msg_set_bits(m, 5, 12, 0x1, 1);
597 static inline void msg_clear_redundant_link(struct tipc_msg *m)
599 msg_set_bits(m, 5, 12, 0x1, 0);
607 static inline u32 msg_msgcnt(struct tipc_msg *m)
609 return msg_bits(m, 9, 16, 0xffff);
612 static inline void msg_set_msgcnt(struct tipc_msg *m, u32 n)
614 msg_set_bits(m, 9, 16, 0xffff, n);
617 static inline u32 msg_bcast_tag(struct tipc_msg *m)
619 return msg_bits(m, 9, 16, 0xffff);
622 static inline void msg_set_bcast_tag(struct tipc_msg *m, u32 n)
624 msg_set_bits(m, 9, 16, 0xffff, n);
627 static inline u32 msg_max_pkt(struct tipc_msg *m)
629 return (msg_bits(m, 9, 16, 0xffff) * 4);
632 static inline void msg_set_max_pkt(struct tipc_msg *m, u32 n)
634 msg_set_bits(m, 9, 16, 0xffff, (n / 4));
637 static inline u32 msg_link_tolerance(struct tipc_msg *m)
639 return msg_bits(m, 9, 0, 0xffff);
642 static inline void msg_set_link_tolerance(struct tipc_msg *m, u32 n)
644 msg_set_bits(m, 9, 0, 0xffff, n);
648 * Routing table message data
652 static inline u32 msg_remote_node(struct tipc_msg *m)
654 return msg_word(m, msg_hdr_sz(m)/4);
657 static inline void msg_set_remote_node(struct tipc_msg *m, u32 a)
659 msg_set_word(m, msg_hdr_sz(m)/4, a);
662 static inline int msg_dataoctet(struct tipc_msg *m, u32 pos)
664 return(msg_data(m)[pos + 4] != 0);
667 static inline void msg_set_dataoctet(struct tipc_msg *m, u32 pos)
669 msg_data(m)[pos + 4] = 1;
673 * Segmentation message types
676 #define FIRST_FRAGMENT 0
678 #define LAST_FRAGMENT 2
681 * Link management protocol message types
686 #define ACTIVATE_MSG 2
689 * Changeover tunnel message types
691 #define DUPLICATE_MSG 0
692 #define ORIGINAL_MSG 1
695 * Routing table message types
697 #define EXT_ROUTING_TABLE 0
698 #define LOCAL_ROUTING_TABLE 1
699 #define SLAVE_ROUTING_TABLE 2
700 #define ROUTE_ADDITION 3
701 #define ROUTE_REMOVAL 4
704 * Config protocol message types
707 #define DSC_REQ_MSG 0
708 #define DSC_RESP_MSG 1
710 static inline u32 msg_tot_importance(struct tipc_msg *m)
712 if (likely(msg_isdata(m))) {
713 if (likely(msg_orignode(m) == tipc_own_addr))
714 return msg_importance(m);
715 return msg_importance(m) + 4;
717 if ((msg_user(m) == MSG_FRAGMENTER) &&
718 (msg_type(m) == FIRST_FRAGMENT))
719 return msg_importance(msg_get_wrapped(m));
720 return msg_importance(m);
724 static inline void msg_init(struct tipc_msg *m, u32 user, u32 type,
725 u32 err, u32 hsize, u32 destnode)
729 msg_set_user(m, user);
730 msg_set_hdr_sz(m, hsize);
731 msg_set_size(m, hsize);
732 msg_set_prevnode(m, tipc_own_addr);
733 msg_set_type(m, type);
734 msg_set_errcode(m, err);
736 msg_set_orignode(m, tipc_own_addr);
737 msg_set_destnode(m, destnode);
742 * msg_calc_data_size - determine total data size for message
745 static inline int msg_calc_data_size(struct iovec const *msg_sect, u32 num_sect)
750 for (i = 0; i < num_sect; i++)
751 dsz += msg_sect[i].iov_len;
756 * msg_build - create message using specified header and data
758 * Note: Caller must not hold any locks in case copy_from_user() is interrupted!
760 * Returns message data size or errno
763 static inline int msg_build(struct tipc_msg *hdr,
764 struct iovec const *msg_sect, u32 num_sect,
765 int max_size, int usrmem, struct sk_buff** buf)
767 int dsz, sz, hsz, pos, res, cnt;
769 dsz = msg_calc_data_size(msg_sect, num_sect);
770 if (unlikely(dsz > TIPC_MAX_USER_MSG_SIZE)) {
775 pos = hsz = msg_hdr_sz(hdr);
777 msg_set_size(hdr, sz);
778 if (unlikely(sz > max_size)) {
783 *buf = buf_acquire(sz);
786 memcpy((*buf)->data, (unchar *)hdr, hsz);
787 for (res = 1, cnt = 0; res && (cnt < num_sect); cnt++) {
789 res = !copy_from_user((*buf)->data + pos,
790 msg_sect[cnt].iov_base,
791 msg_sect[cnt].iov_len);
793 memcpy((*buf)->data + pos, msg_sect[cnt].iov_base,
794 msg_sect[cnt].iov_len);
795 pos += msg_sect[cnt].iov_len;
806 struct tipc_media_addr;
808 extern void msg_set_media_addr(struct tipc_msg *m,
809 struct tipc_media_addr *a);
811 extern void msg_get_media_addr(struct tipc_msg *m,
812 struct tipc_media_addr *a);