msg.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * net/tipc/msg.c: TIPC message header routines
  3. *
  4. * Copyright (c) 2000-2006, Ericsson AB
  5. * Copyright (c) 2005, 2010-2011, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  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.
  19. *
  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.
  23. *
  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.
  35. */
  36. #include "core.h"
  37. #include "msg.h"
  38. u32 tipc_msg_tot_importance(struct tipc_msg *m)
  39. {
  40. if (likely(msg_isdata(m))) {
  41. if (likely(msg_orignode(m) == tipc_own_addr))
  42. return msg_importance(m);
  43. return msg_importance(m) + 4;
  44. }
  45. if ((msg_user(m) == MSG_FRAGMENTER) &&
  46. (msg_type(m) == FIRST_FRAGMENT))
  47. return msg_importance(msg_get_wrapped(m));
  48. return msg_importance(m);
  49. }
  50. void tipc_msg_init(struct tipc_msg *m, u32 user, u32 type,
  51. u32 hsize, u32 destnode)
  52. {
  53. memset(m, 0, hsize);
  54. msg_set_version(m);
  55. msg_set_user(m, user);
  56. msg_set_hdr_sz(m, hsize);
  57. msg_set_size(m, hsize);
  58. msg_set_prevnode(m, tipc_own_addr);
  59. msg_set_type(m, type);
  60. msg_set_orignode(m, tipc_own_addr);
  61. msg_set_destnode(m, destnode);
  62. }
  63. /**
  64. * tipc_msg_build - create message using specified header and data
  65. *
  66. * Note: Caller must not hold any locks in case copy_from_user() is interrupted!
  67. *
  68. * Returns message data size or errno
  69. */
  70. int tipc_msg_build(struct tipc_msg *hdr, struct iovec const *msg_sect,
  71. u32 num_sect, unsigned int total_len,
  72. int max_size, int usrmem, struct sk_buff **buf)
  73. {
  74. int dsz, sz, hsz, pos, res, cnt;
  75. dsz = total_len;
  76. pos = hsz = msg_hdr_sz(hdr);
  77. sz = hsz + dsz;
  78. msg_set_size(hdr, sz);
  79. if (unlikely(sz > max_size)) {
  80. *buf = NULL;
  81. return dsz;
  82. }
  83. *buf = tipc_buf_acquire(sz);
  84. if (!(*buf))
  85. return -ENOMEM;
  86. skb_copy_to_linear_data(*buf, hdr, hsz);
  87. for (res = 1, cnt = 0; res && (cnt < num_sect); cnt++) {
  88. if (likely(usrmem))
  89. res = !copy_from_user((*buf)->data + pos,
  90. msg_sect[cnt].iov_base,
  91. msg_sect[cnt].iov_len);
  92. else
  93. skb_copy_to_linear_data_offset(*buf, pos,
  94. msg_sect[cnt].iov_base,
  95. msg_sect[cnt].iov_len);
  96. pos += msg_sect[cnt].iov_len;
  97. }
  98. if (likely(res))
  99. return dsz;
  100. kfree_skb(*buf);
  101. *buf = NULL;
  102. return -EFAULT;
  103. }
  104. #ifdef CONFIG_TIPC_DEBUG
  105. void tipc_msg_dbg(struct print_buf *buf, struct tipc_msg *msg, const char *str)
  106. {
  107. u32 usr = msg_user(msg);
  108. tipc_printf(buf, KERN_DEBUG);
  109. tipc_printf(buf, str);
  110. switch (usr) {
  111. case MSG_BUNDLER:
  112. tipc_printf(buf, "BNDL::");
  113. tipc_printf(buf, "MSGS(%u):", msg_msgcnt(msg));
  114. break;
  115. case BCAST_PROTOCOL:
  116. tipc_printf(buf, "BCASTP::");
  117. break;
  118. case MSG_FRAGMENTER:
  119. tipc_printf(buf, "FRAGM::");
  120. switch (msg_type(msg)) {
  121. case FIRST_FRAGMENT:
  122. tipc_printf(buf, "FIRST:");
  123. break;
  124. case FRAGMENT:
  125. tipc_printf(buf, "BODY:");
  126. break;
  127. case LAST_FRAGMENT:
  128. tipc_printf(buf, "LAST:");
  129. break;
  130. default:
  131. tipc_printf(buf, "UNKNOWN:%x", msg_type(msg));
  132. }
  133. tipc_printf(buf, "NO(%u/%u):", msg_long_msgno(msg),
  134. msg_fragm_no(msg));
  135. break;
  136. case TIPC_LOW_IMPORTANCE:
  137. case TIPC_MEDIUM_IMPORTANCE:
  138. case TIPC_HIGH_IMPORTANCE:
  139. case TIPC_CRITICAL_IMPORTANCE:
  140. tipc_printf(buf, "DAT%u:", msg_user(msg));
  141. if (msg_short(msg)) {
  142. tipc_printf(buf, "CON:");
  143. break;
  144. }
  145. switch (msg_type(msg)) {
  146. case TIPC_CONN_MSG:
  147. tipc_printf(buf, "CON:");
  148. break;
  149. case TIPC_MCAST_MSG:
  150. tipc_printf(buf, "MCST:");
  151. break;
  152. case TIPC_NAMED_MSG:
  153. tipc_printf(buf, "NAM:");
  154. break;
  155. case TIPC_DIRECT_MSG:
  156. tipc_printf(buf, "DIR:");
  157. break;
  158. default:
  159. tipc_printf(buf, "UNKNOWN TYPE %u", msg_type(msg));
  160. }
  161. if (msg_reroute_cnt(msg))
  162. tipc_printf(buf, "REROUTED(%u):",
  163. msg_reroute_cnt(msg));
  164. break;
  165. case NAME_DISTRIBUTOR:
  166. tipc_printf(buf, "NMD::");
  167. switch (msg_type(msg)) {
  168. case PUBLICATION:
  169. tipc_printf(buf, "PUBL(%u):", (msg_size(msg) - msg_hdr_sz(msg)) / 20); /* Items */
  170. break;
  171. case WITHDRAWAL:
  172. tipc_printf(buf, "WDRW:");
  173. break;
  174. default:
  175. tipc_printf(buf, "UNKNOWN:%x", msg_type(msg));
  176. }
  177. if (msg_reroute_cnt(msg))
  178. tipc_printf(buf, "REROUTED(%u):",
  179. msg_reroute_cnt(msg));
  180. break;
  181. case CONN_MANAGER:
  182. tipc_printf(buf, "CONN_MNG:");
  183. switch (msg_type(msg)) {
  184. case CONN_PROBE:
  185. tipc_printf(buf, "PROBE:");
  186. break;
  187. case CONN_PROBE_REPLY:
  188. tipc_printf(buf, "PROBE_REPLY:");
  189. break;
  190. case CONN_ACK:
  191. tipc_printf(buf, "CONN_ACK:");
  192. tipc_printf(buf, "ACK(%u):", msg_msgcnt(msg));
  193. break;
  194. default:
  195. tipc_printf(buf, "UNKNOWN TYPE:%x", msg_type(msg));
  196. }
  197. if (msg_reroute_cnt(msg))
  198. tipc_printf(buf, "REROUTED(%u):", msg_reroute_cnt(msg));
  199. break;
  200. case LINK_PROTOCOL:
  201. switch (msg_type(msg)) {
  202. case STATE_MSG:
  203. tipc_printf(buf, "STATE:");
  204. tipc_printf(buf, "%s:", msg_probe(msg) ? "PRB" : "");
  205. tipc_printf(buf, "NXS(%u):", msg_next_sent(msg));
  206. tipc_printf(buf, "GAP(%u):", msg_seq_gap(msg));
  207. tipc_printf(buf, "LSTBC(%u):", msg_last_bcast(msg));
  208. break;
  209. case RESET_MSG:
  210. tipc_printf(buf, "RESET:");
  211. if (msg_size(msg) != msg_hdr_sz(msg))
  212. tipc_printf(buf, "BEAR:%s:", msg_data(msg));
  213. break;
  214. case ACTIVATE_MSG:
  215. tipc_printf(buf, "ACTIVATE:");
  216. break;
  217. default:
  218. tipc_printf(buf, "UNKNOWN TYPE:%x", msg_type(msg));
  219. }
  220. tipc_printf(buf, "PLANE(%c):", msg_net_plane(msg));
  221. tipc_printf(buf, "SESS(%u):", msg_session(msg));
  222. break;
  223. case CHANGEOVER_PROTOCOL:
  224. tipc_printf(buf, "TUNL:");
  225. switch (msg_type(msg)) {
  226. case DUPLICATE_MSG:
  227. tipc_printf(buf, "DUPL:");
  228. break;
  229. case ORIGINAL_MSG:
  230. tipc_printf(buf, "ORIG:");
  231. tipc_printf(buf, "EXP(%u)", msg_msgcnt(msg));
  232. break;
  233. default:
  234. tipc_printf(buf, "UNKNOWN TYPE:%x", msg_type(msg));
  235. }
  236. break;
  237. case LINK_CONFIG:
  238. tipc_printf(buf, "CFG:");
  239. switch (msg_type(msg)) {
  240. case DSC_REQ_MSG:
  241. tipc_printf(buf, "DSC_REQ:");
  242. break;
  243. case DSC_RESP_MSG:
  244. tipc_printf(buf, "DSC_RESP:");
  245. break;
  246. default:
  247. tipc_printf(buf, "UNKNOWN TYPE:%x:", msg_type(msg));
  248. break;
  249. }
  250. break;
  251. default:
  252. tipc_printf(buf, "UNKNOWN USER:");
  253. }
  254. switch (usr) {
  255. case CONN_MANAGER:
  256. case TIPC_LOW_IMPORTANCE:
  257. case TIPC_MEDIUM_IMPORTANCE:
  258. case TIPC_HIGH_IMPORTANCE:
  259. case TIPC_CRITICAL_IMPORTANCE:
  260. switch (msg_errcode(msg)) {
  261. case TIPC_OK:
  262. break;
  263. case TIPC_ERR_NO_NAME:
  264. tipc_printf(buf, "NO_NAME:");
  265. break;
  266. case TIPC_ERR_NO_PORT:
  267. tipc_printf(buf, "NO_PORT:");
  268. break;
  269. case TIPC_ERR_NO_NODE:
  270. tipc_printf(buf, "NO_PROC:");
  271. break;
  272. case TIPC_ERR_OVERLOAD:
  273. tipc_printf(buf, "OVERLOAD:");
  274. break;
  275. case TIPC_CONN_SHUTDOWN:
  276. tipc_printf(buf, "SHUTDOWN:");
  277. break;
  278. default:
  279. tipc_printf(buf, "UNKNOWN ERROR(%x):",
  280. msg_errcode(msg));
  281. }
  282. default:
  283. break;
  284. }
  285. tipc_printf(buf, "HZ(%u):", msg_hdr_sz(msg));
  286. tipc_printf(buf, "SZ(%u):", msg_size(msg));
  287. tipc_printf(buf, "SQNO(%u):", msg_seqno(msg));
  288. if (msg_non_seq(msg))
  289. tipc_printf(buf, "NOSEQ:");
  290. else
  291. tipc_printf(buf, "ACK(%u):", msg_ack(msg));
  292. tipc_printf(buf, "BACK(%u):", msg_bcast_ack(msg));
  293. tipc_printf(buf, "PRND(%x)", msg_prevnode(msg));
  294. if (msg_isdata(msg)) {
  295. if (msg_named(msg)) {
  296. tipc_printf(buf, "NTYP(%u):", msg_nametype(msg));
  297. tipc_printf(buf, "NINST(%u)", msg_nameinst(msg));
  298. }
  299. }
  300. if ((usr != LINK_PROTOCOL) && (usr != LINK_CONFIG) &&
  301. (usr != MSG_BUNDLER)) {
  302. if (!msg_short(msg)) {
  303. tipc_printf(buf, ":ORIG(%x:%u):",
  304. msg_orignode(msg), msg_origport(msg));
  305. tipc_printf(buf, ":DEST(%x:%u):",
  306. msg_destnode(msg), msg_destport(msg));
  307. } else {
  308. tipc_printf(buf, ":OPRT(%u):", msg_origport(msg));
  309. tipc_printf(buf, ":DPRT(%u):", msg_destport(msg));
  310. }
  311. }
  312. if (msg_user(msg) == NAME_DISTRIBUTOR) {
  313. tipc_printf(buf, ":ONOD(%x):", msg_orignode(msg));
  314. tipc_printf(buf, ":DNOD(%x):", msg_destnode(msg));
  315. }
  316. if (msg_user(msg) == LINK_CONFIG) {
  317. struct tipc_media_addr orig;
  318. tipc_printf(buf, ":DDOM(%x):", msg_dest_domain(msg));
  319. tipc_printf(buf, ":NETID(%u):", msg_bc_netid(msg));
  320. memcpy(orig.value, msg_media_addr(msg), sizeof(orig.value));
  321. orig.media_id = 0;
  322. orig.broadcast = 0;
  323. tipc_media_addr_printf(buf, &orig);
  324. }
  325. if (msg_user(msg) == BCAST_PROTOCOL) {
  326. tipc_printf(buf, "BCNACK:AFTER(%u):", msg_bcgap_after(msg));
  327. tipc_printf(buf, "TO(%u):", msg_bcgap_to(msg));
  328. }
  329. tipc_printf(buf, "\n");
  330. if ((usr == CHANGEOVER_PROTOCOL) && (msg_msgcnt(msg)))
  331. tipc_msg_dbg(buf, msg_get_wrapped(msg), " /");
  332. if ((usr == MSG_FRAGMENTER) && (msg_type(msg) == FIRST_FRAGMENT))
  333. tipc_msg_dbg(buf, msg_get_wrapped(msg), " /");
  334. }
  335. #endif