plugin_scsi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <inttypes.h>
  4. #include "event-parse.h"
  5. typedef unsigned long sector_t;
  6. typedef uint64_t u64;
  7. typedef unsigned int u32;
  8. /*
  9. * SCSI opcodes
  10. */
  11. #define TEST_UNIT_READY 0x00
  12. #define REZERO_UNIT 0x01
  13. #define REQUEST_SENSE 0x03
  14. #define FORMAT_UNIT 0x04
  15. #define READ_BLOCK_LIMITS 0x05
  16. #define REASSIGN_BLOCKS 0x07
  17. #define INITIALIZE_ELEMENT_STATUS 0x07
  18. #define READ_6 0x08
  19. #define WRITE_6 0x0a
  20. #define SEEK_6 0x0b
  21. #define READ_REVERSE 0x0f
  22. #define WRITE_FILEMARKS 0x10
  23. #define SPACE 0x11
  24. #define INQUIRY 0x12
  25. #define RECOVER_BUFFERED_DATA 0x14
  26. #define MODE_SELECT 0x15
  27. #define RESERVE 0x16
  28. #define RELEASE 0x17
  29. #define COPY 0x18
  30. #define ERASE 0x19
  31. #define MODE_SENSE 0x1a
  32. #define START_STOP 0x1b
  33. #define RECEIVE_DIAGNOSTIC 0x1c
  34. #define SEND_DIAGNOSTIC 0x1d
  35. #define ALLOW_MEDIUM_REMOVAL 0x1e
  36. #define READ_FORMAT_CAPACITIES 0x23
  37. #define SET_WINDOW 0x24
  38. #define READ_CAPACITY 0x25
  39. #define READ_10 0x28
  40. #define WRITE_10 0x2a
  41. #define SEEK_10 0x2b
  42. #define POSITION_TO_ELEMENT 0x2b
  43. #define WRITE_VERIFY 0x2e
  44. #define VERIFY 0x2f
  45. #define SEARCH_HIGH 0x30
  46. #define SEARCH_EQUAL 0x31
  47. #define SEARCH_LOW 0x32
  48. #define SET_LIMITS 0x33
  49. #define PRE_FETCH 0x34
  50. #define READ_POSITION 0x34
  51. #define SYNCHRONIZE_CACHE 0x35
  52. #define LOCK_UNLOCK_CACHE 0x36
  53. #define READ_DEFECT_DATA 0x37
  54. #define MEDIUM_SCAN 0x38
  55. #define COMPARE 0x39
  56. #define COPY_VERIFY 0x3a
  57. #define WRITE_BUFFER 0x3b
  58. #define READ_BUFFER 0x3c
  59. #define UPDATE_BLOCK 0x3d
  60. #define READ_LONG 0x3e
  61. #define WRITE_LONG 0x3f
  62. #define CHANGE_DEFINITION 0x40
  63. #define WRITE_SAME 0x41
  64. #define UNMAP 0x42
  65. #define READ_TOC 0x43
  66. #define READ_HEADER 0x44
  67. #define GET_EVENT_STATUS_NOTIFICATION 0x4a
  68. #define LOG_SELECT 0x4c
  69. #define LOG_SENSE 0x4d
  70. #define XDWRITEREAD_10 0x53
  71. #define MODE_SELECT_10 0x55
  72. #define RESERVE_10 0x56
  73. #define RELEASE_10 0x57
  74. #define MODE_SENSE_10 0x5a
  75. #define PERSISTENT_RESERVE_IN 0x5e
  76. #define PERSISTENT_RESERVE_OUT 0x5f
  77. #define VARIABLE_LENGTH_CMD 0x7f
  78. #define REPORT_LUNS 0xa0
  79. #define SECURITY_PROTOCOL_IN 0xa2
  80. #define MAINTENANCE_IN 0xa3
  81. #define MAINTENANCE_OUT 0xa4
  82. #define MOVE_MEDIUM 0xa5
  83. #define EXCHANGE_MEDIUM 0xa6
  84. #define READ_12 0xa8
  85. #define SERVICE_ACTION_OUT_12 0xa9
  86. #define WRITE_12 0xaa
  87. #define SERVICE_ACTION_IN_12 0xab
  88. #define WRITE_VERIFY_12 0xae
  89. #define VERIFY_12 0xaf
  90. #define SEARCH_HIGH_12 0xb0
  91. #define SEARCH_EQUAL_12 0xb1
  92. #define SEARCH_LOW_12 0xb2
  93. #define SECURITY_PROTOCOL_OUT 0xb5
  94. #define READ_ELEMENT_STATUS 0xb8
  95. #define SEND_VOLUME_TAG 0xb6
  96. #define WRITE_LONG_2 0xea
  97. #define EXTENDED_COPY 0x83
  98. #define RECEIVE_COPY_RESULTS 0x84
  99. #define ACCESS_CONTROL_IN 0x86
  100. #define ACCESS_CONTROL_OUT 0x87
  101. #define READ_16 0x88
  102. #define WRITE_16 0x8a
  103. #define READ_ATTRIBUTE 0x8c
  104. #define WRITE_ATTRIBUTE 0x8d
  105. #define VERIFY_16 0x8f
  106. #define SYNCHRONIZE_CACHE_16 0x91
  107. #define WRITE_SAME_16 0x93
  108. #define SERVICE_ACTION_BIDIRECTIONAL 0x9d
  109. #define SERVICE_ACTION_IN_16 0x9e
  110. #define SERVICE_ACTION_OUT_16 0x9f
  111. /* values for service action in */
  112. #define SAI_READ_CAPACITY_16 0x10
  113. #define SAI_GET_LBA_STATUS 0x12
  114. /* values for VARIABLE_LENGTH_CMD service action codes
  115. * see spc4r17 Section D.3.5, table D.7 and D.8 */
  116. #define VLC_SA_RECEIVE_CREDENTIAL 0x1800
  117. /* values for maintenance in */
  118. #define MI_REPORT_IDENTIFYING_INFORMATION 0x05
  119. #define MI_REPORT_TARGET_PGS 0x0a
  120. #define MI_REPORT_ALIASES 0x0b
  121. #define MI_REPORT_SUPPORTED_OPERATION_CODES 0x0c
  122. #define MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS 0x0d
  123. #define MI_REPORT_PRIORITY 0x0e
  124. #define MI_REPORT_TIMESTAMP 0x0f
  125. #define MI_MANAGEMENT_PROTOCOL_IN 0x10
  126. /* value for MI_REPORT_TARGET_PGS ext header */
  127. #define MI_EXT_HDR_PARAM_FMT 0x20
  128. /* values for maintenance out */
  129. #define MO_SET_IDENTIFYING_INFORMATION 0x06
  130. #define MO_SET_TARGET_PGS 0x0a
  131. #define MO_CHANGE_ALIASES 0x0b
  132. #define MO_SET_PRIORITY 0x0e
  133. #define MO_SET_TIMESTAMP 0x0f
  134. #define MO_MANAGEMENT_PROTOCOL_OUT 0x10
  135. /* values for variable length command */
  136. #define XDREAD_32 0x03
  137. #define XDWRITE_32 0x04
  138. #define XPWRITE_32 0x06
  139. #define XDWRITEREAD_32 0x07
  140. #define READ_32 0x09
  141. #define VERIFY_32 0x0a
  142. #define WRITE_32 0x0b
  143. #define WRITE_SAME_32 0x0d
  144. #define SERVICE_ACTION16(cdb) (cdb[1] & 0x1f)
  145. #define SERVICE_ACTION32(cdb) ((cdb[8] << 8) | cdb[9])
  146. static const char *
  147. scsi_trace_misc(struct trace_seq *, unsigned char *, int);
  148. static const char *
  149. scsi_trace_rw6(struct trace_seq *p, unsigned char *cdb, int len)
  150. {
  151. const char *ret = p->buffer + p->len;
  152. sector_t lba = 0, txlen = 0;
  153. lba |= ((cdb[1] & 0x1F) << 16);
  154. lba |= (cdb[2] << 8);
  155. lba |= cdb[3];
  156. txlen = cdb[4];
  157. trace_seq_printf(p, "lba=%llu txlen=%llu",
  158. (unsigned long long)lba, (unsigned long long)txlen);
  159. trace_seq_putc(p, 0);
  160. return ret;
  161. }
  162. static const char *
  163. scsi_trace_rw10(struct trace_seq *p, unsigned char *cdb, int len)
  164. {
  165. const char *ret = p->buffer + p->len;
  166. sector_t lba = 0, txlen = 0;
  167. lba |= (cdb[2] << 24);
  168. lba |= (cdb[3] << 16);
  169. lba |= (cdb[4] << 8);
  170. lba |= cdb[5];
  171. txlen |= (cdb[7] << 8);
  172. txlen |= cdb[8];
  173. trace_seq_printf(p, "lba=%llu txlen=%llu protect=%u",
  174. (unsigned long long)lba, (unsigned long long)txlen,
  175. cdb[1] >> 5);
  176. if (cdb[0] == WRITE_SAME)
  177. trace_seq_printf(p, " unmap=%u", cdb[1] >> 3 & 1);
  178. trace_seq_putc(p, 0);
  179. return ret;
  180. }
  181. static const char *
  182. scsi_trace_rw12(struct trace_seq *p, unsigned char *cdb, int len)
  183. {
  184. const char *ret = p->buffer + p->len;
  185. sector_t lba = 0, txlen = 0;
  186. lba |= (cdb[2] << 24);
  187. lba |= (cdb[3] << 16);
  188. lba |= (cdb[4] << 8);
  189. lba |= cdb[5];
  190. txlen |= (cdb[6] << 24);
  191. txlen |= (cdb[7] << 16);
  192. txlen |= (cdb[8] << 8);
  193. txlen |= cdb[9];
  194. trace_seq_printf(p, "lba=%llu txlen=%llu protect=%u",
  195. (unsigned long long)lba, (unsigned long long)txlen,
  196. cdb[1] >> 5);
  197. trace_seq_putc(p, 0);
  198. return ret;
  199. }
  200. static const char *
  201. scsi_trace_rw16(struct trace_seq *p, unsigned char *cdb, int len)
  202. {
  203. const char *ret = p->buffer + p->len;
  204. sector_t lba = 0, txlen = 0;
  205. lba |= ((u64)cdb[2] << 56);
  206. lba |= ((u64)cdb[3] << 48);
  207. lba |= ((u64)cdb[4] << 40);
  208. lba |= ((u64)cdb[5] << 32);
  209. lba |= (cdb[6] << 24);
  210. lba |= (cdb[7] << 16);
  211. lba |= (cdb[8] << 8);
  212. lba |= cdb[9];
  213. txlen |= (cdb[10] << 24);
  214. txlen |= (cdb[11] << 16);
  215. txlen |= (cdb[12] << 8);
  216. txlen |= cdb[13];
  217. trace_seq_printf(p, "lba=%llu txlen=%llu protect=%u",
  218. (unsigned long long)lba, (unsigned long long)txlen,
  219. cdb[1] >> 5);
  220. if (cdb[0] == WRITE_SAME_16)
  221. trace_seq_printf(p, " unmap=%u", cdb[1] >> 3 & 1);
  222. trace_seq_putc(p, 0);
  223. return ret;
  224. }
  225. static const char *
  226. scsi_trace_rw32(struct trace_seq *p, unsigned char *cdb, int len)
  227. {
  228. const char *ret = p->buffer + p->len, *cmd;
  229. sector_t lba = 0, txlen = 0;
  230. u32 ei_lbrt = 0;
  231. switch (SERVICE_ACTION32(cdb)) {
  232. case READ_32:
  233. cmd = "READ";
  234. break;
  235. case VERIFY_32:
  236. cmd = "VERIFY";
  237. break;
  238. case WRITE_32:
  239. cmd = "WRITE";
  240. break;
  241. case WRITE_SAME_32:
  242. cmd = "WRITE_SAME";
  243. break;
  244. default:
  245. trace_seq_printf(p, "UNKNOWN");
  246. goto out;
  247. }
  248. lba |= ((u64)cdb[12] << 56);
  249. lba |= ((u64)cdb[13] << 48);
  250. lba |= ((u64)cdb[14] << 40);
  251. lba |= ((u64)cdb[15] << 32);
  252. lba |= (cdb[16] << 24);
  253. lba |= (cdb[17] << 16);
  254. lba |= (cdb[18] << 8);
  255. lba |= cdb[19];
  256. ei_lbrt |= (cdb[20] << 24);
  257. ei_lbrt |= (cdb[21] << 16);
  258. ei_lbrt |= (cdb[22] << 8);
  259. ei_lbrt |= cdb[23];
  260. txlen |= (cdb[28] << 24);
  261. txlen |= (cdb[29] << 16);
  262. txlen |= (cdb[30] << 8);
  263. txlen |= cdb[31];
  264. trace_seq_printf(p, "%s_32 lba=%llu txlen=%llu protect=%u ei_lbrt=%u",
  265. cmd, (unsigned long long)lba,
  266. (unsigned long long)txlen, cdb[10] >> 5, ei_lbrt);
  267. if (SERVICE_ACTION32(cdb) == WRITE_SAME_32)
  268. trace_seq_printf(p, " unmap=%u", cdb[10] >> 3 & 1);
  269. out:
  270. trace_seq_putc(p, 0);
  271. return ret;
  272. }
  273. static const char *
  274. scsi_trace_unmap(struct trace_seq *p, unsigned char *cdb, int len)
  275. {
  276. const char *ret = p->buffer + p->len;
  277. unsigned int regions = cdb[7] << 8 | cdb[8];
  278. trace_seq_printf(p, "regions=%u", (regions - 8) / 16);
  279. trace_seq_putc(p, 0);
  280. return ret;
  281. }
  282. static const char *
  283. scsi_trace_service_action_in(struct trace_seq *p, unsigned char *cdb, int len)
  284. {
  285. const char *ret = p->buffer + p->len, *cmd;
  286. sector_t lba = 0;
  287. u32 alloc_len = 0;
  288. switch (SERVICE_ACTION16(cdb)) {
  289. case SAI_READ_CAPACITY_16:
  290. cmd = "READ_CAPACITY_16";
  291. break;
  292. case SAI_GET_LBA_STATUS:
  293. cmd = "GET_LBA_STATUS";
  294. break;
  295. default:
  296. trace_seq_printf(p, "UNKNOWN");
  297. goto out;
  298. }
  299. lba |= ((u64)cdb[2] << 56);
  300. lba |= ((u64)cdb[3] << 48);
  301. lba |= ((u64)cdb[4] << 40);
  302. lba |= ((u64)cdb[5] << 32);
  303. lba |= (cdb[6] << 24);
  304. lba |= (cdb[7] << 16);
  305. lba |= (cdb[8] << 8);
  306. lba |= cdb[9];
  307. alloc_len |= (cdb[10] << 24);
  308. alloc_len |= (cdb[11] << 16);
  309. alloc_len |= (cdb[12] << 8);
  310. alloc_len |= cdb[13];
  311. trace_seq_printf(p, "%s lba=%llu alloc_len=%u", cmd,
  312. (unsigned long long)lba, alloc_len);
  313. out:
  314. trace_seq_putc(p, 0);
  315. return ret;
  316. }
  317. static const char *
  318. scsi_trace_varlen(struct trace_seq *p, unsigned char *cdb, int len)
  319. {
  320. switch (SERVICE_ACTION32(cdb)) {
  321. case READ_32:
  322. case VERIFY_32:
  323. case WRITE_32:
  324. case WRITE_SAME_32:
  325. return scsi_trace_rw32(p, cdb, len);
  326. default:
  327. return scsi_trace_misc(p, cdb, len);
  328. }
  329. }
  330. static const char *
  331. scsi_trace_misc(struct trace_seq *p, unsigned char *cdb, int len)
  332. {
  333. const char *ret = p->buffer + p->len;
  334. trace_seq_printf(p, "-");
  335. trace_seq_putc(p, 0);
  336. return ret;
  337. }
  338. const char *
  339. scsi_trace_parse_cdb(struct trace_seq *p, unsigned char *cdb, int len)
  340. {
  341. switch (cdb[0]) {
  342. case READ_6:
  343. case WRITE_6:
  344. return scsi_trace_rw6(p, cdb, len);
  345. case READ_10:
  346. case VERIFY:
  347. case WRITE_10:
  348. case WRITE_SAME:
  349. return scsi_trace_rw10(p, cdb, len);
  350. case READ_12:
  351. case VERIFY_12:
  352. case WRITE_12:
  353. return scsi_trace_rw12(p, cdb, len);
  354. case READ_16:
  355. case VERIFY_16:
  356. case WRITE_16:
  357. case WRITE_SAME_16:
  358. return scsi_trace_rw16(p, cdb, len);
  359. case UNMAP:
  360. return scsi_trace_unmap(p, cdb, len);
  361. case SERVICE_ACTION_IN_16:
  362. return scsi_trace_service_action_in(p, cdb, len);
  363. case VARIABLE_LENGTH_CMD:
  364. return scsi_trace_varlen(p, cdb, len);
  365. default:
  366. return scsi_trace_misc(p, cdb, len);
  367. }
  368. }
  369. unsigned long long process_scsi_trace_parse_cdb(struct trace_seq *s,
  370. unsigned long long *args)
  371. {
  372. scsi_trace_parse_cdb(s, (unsigned char *) (unsigned long) args[1], args[2]);
  373. return 0;
  374. }
  375. int PEVENT_PLUGIN_LOADER(struct pevent *pevent)
  376. {
  377. pevent_register_print_function(pevent,
  378. process_scsi_trace_parse_cdb,
  379. PEVENT_FUNC_ARG_STRING,
  380. "scsi_trace_parse_cdb",
  381. PEVENT_FUNC_ARG_PTR,
  382. PEVENT_FUNC_ARG_PTR,
  383. PEVENT_FUNC_ARG_INT,
  384. PEVENT_FUNC_ARG_VOID);
  385. return 0;
  386. }
  387. void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent)
  388. {
  389. pevent_unregister_print_function(pevent, process_scsi_trace_parse_cdb,
  390. "scsi_trace_parse_cdb");
  391. }