10c3a8cd0SMatthew Dillon /*
20c3a8cd0SMatthew Dillon * Copyright (c) 2012 The DragonFly Project. All rights reserved.
30c3a8cd0SMatthew Dillon *
40c3a8cd0SMatthew Dillon * This code is derived from software contributed to The DragonFly Project
50c3a8cd0SMatthew Dillon * by Matthew Dillon <dillon@dragonflybsd.org>
60c3a8cd0SMatthew Dillon *
70c3a8cd0SMatthew Dillon * Redistribution and use in source and binary forms, with or without
80c3a8cd0SMatthew Dillon * modification, are permitted provided that the following conditions
90c3a8cd0SMatthew Dillon * are met:
100c3a8cd0SMatthew Dillon *
110c3a8cd0SMatthew Dillon * 1. Redistributions of source code must retain the above copyright
120c3a8cd0SMatthew Dillon * notice, this list of conditions and the following disclaimer.
130c3a8cd0SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
140c3a8cd0SMatthew Dillon * notice, this list of conditions and the following disclaimer in
150c3a8cd0SMatthew Dillon * the documentation and/or other materials provided with the
160c3a8cd0SMatthew Dillon * distribution.
170c3a8cd0SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its
180c3a8cd0SMatthew Dillon * contributors may be used to endorse or promote products derived
190c3a8cd0SMatthew Dillon * from this software without specific, prior written permission.
200c3a8cd0SMatthew Dillon *
210c3a8cd0SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
220c3a8cd0SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
230c3a8cd0SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
240c3a8cd0SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
250c3a8cd0SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
260c3a8cd0SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
270c3a8cd0SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
280c3a8cd0SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
290c3a8cd0SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
300c3a8cd0SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
310c3a8cd0SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
320c3a8cd0SMatthew Dillon * SUCH DAMAGE.
330c3a8cd0SMatthew Dillon */
340c3a8cd0SMatthew Dillon
350c3a8cd0SMatthew Dillon #include "dmsg_local.h"
360c3a8cd0SMatthew Dillon
370c3a8cd0SMatthew Dillon const char *
dmsg_basecmd_str(uint32_t cmd)380c3a8cd0SMatthew Dillon dmsg_basecmd_str(uint32_t cmd)
390c3a8cd0SMatthew Dillon {
400c3a8cd0SMatthew Dillon static char buf[64];
410c3a8cd0SMatthew Dillon char protobuf[32];
420c3a8cd0SMatthew Dillon char cmdbuf[32];
430c3a8cd0SMatthew Dillon const char *protostr;
440c3a8cd0SMatthew Dillon const char *cmdstr;
450c3a8cd0SMatthew Dillon
460c3a8cd0SMatthew Dillon switch(cmd & DMSGF_PROTOS) {
470c3a8cd0SMatthew Dillon case DMSG_PROTO_LNK:
480c3a8cd0SMatthew Dillon protostr = "LNK_";
490c3a8cd0SMatthew Dillon break;
500c3a8cd0SMatthew Dillon case DMSG_PROTO_DBG:
510c3a8cd0SMatthew Dillon protostr = "DBG_";
520c3a8cd0SMatthew Dillon break;
531b8eded1SMatthew Dillon case DMSG_PROTO_HM2:
541b8eded1SMatthew Dillon protostr = "HM2_";
550c3a8cd0SMatthew Dillon break;
560c3a8cd0SMatthew Dillon case DMSG_PROTO_BLK:
570c3a8cd0SMatthew Dillon protostr = "BLK_";
580c3a8cd0SMatthew Dillon break;
590c3a8cd0SMatthew Dillon case DMSG_PROTO_VOP:
600c3a8cd0SMatthew Dillon protostr = "VOP_";
610c3a8cd0SMatthew Dillon break;
620c3a8cd0SMatthew Dillon default:
630c3a8cd0SMatthew Dillon snprintf(protobuf, sizeof(protobuf), "%x_",
640c3a8cd0SMatthew Dillon (cmd & DMSGF_PROTOS) >> 20);
650c3a8cd0SMatthew Dillon protostr = protobuf;
660c3a8cd0SMatthew Dillon break;
670c3a8cd0SMatthew Dillon }
680c3a8cd0SMatthew Dillon
690c3a8cd0SMatthew Dillon switch(cmd & (DMSGF_PROTOS |
700c3a8cd0SMatthew Dillon DMSGF_CMDS |
710c3a8cd0SMatthew Dillon DMSGF_SIZE)) {
720c3a8cd0SMatthew Dillon case DMSG_LNK_PAD:
730c3a8cd0SMatthew Dillon cmdstr = "PAD";
740c3a8cd0SMatthew Dillon break;
750c3a8cd0SMatthew Dillon case DMSG_LNK_PING:
760c3a8cd0SMatthew Dillon cmdstr = "PING";
770c3a8cd0SMatthew Dillon break;
780c3a8cd0SMatthew Dillon case DMSG_LNK_AUTH:
790c3a8cd0SMatthew Dillon cmdstr = "AUTH";
800c3a8cd0SMatthew Dillon break;
810c3a8cd0SMatthew Dillon case DMSG_LNK_CONN:
820c3a8cd0SMatthew Dillon cmdstr = "CONN";
830c3a8cd0SMatthew Dillon break;
840c3a8cd0SMatthew Dillon case DMSG_LNK_SPAN:
850c3a8cd0SMatthew Dillon cmdstr = "SPAN";
860c3a8cd0SMatthew Dillon break;
870c3a8cd0SMatthew Dillon case DMSG_LNK_ERROR:
880c3a8cd0SMatthew Dillon if (cmd & DMSGF_DELETE)
890c3a8cd0SMatthew Dillon cmdstr = "RETURN";
900c3a8cd0SMatthew Dillon else
910c3a8cd0SMatthew Dillon cmdstr = "RESULT";
920c3a8cd0SMatthew Dillon break;
930c3a8cd0SMatthew Dillon case DMSG_DBG_SHELL:
940c3a8cd0SMatthew Dillon cmdstr = "SHELL";
950c3a8cd0SMatthew Dillon break;
960c3a8cd0SMatthew Dillon default:
970c3a8cd0SMatthew Dillon snprintf(cmdbuf, sizeof(cmdbuf),
980c3a8cd0SMatthew Dillon "%06x", (cmd & (DMSGF_PROTOS |
990c3a8cd0SMatthew Dillon DMSGF_CMDS |
1000c3a8cd0SMatthew Dillon DMSGF_SIZE)));
1010c3a8cd0SMatthew Dillon cmdstr = cmdbuf;
1020c3a8cd0SMatthew Dillon break;
1030c3a8cd0SMatthew Dillon }
1040c3a8cd0SMatthew Dillon snprintf(buf, sizeof(buf), "%s%s", protostr, cmdstr);
1050c3a8cd0SMatthew Dillon return (buf);
1060c3a8cd0SMatthew Dillon }
1070c3a8cd0SMatthew Dillon
1080c3a8cd0SMatthew Dillon const char *
dmsg_msg_str(dmsg_msg_t * msg)1090c3a8cd0SMatthew Dillon dmsg_msg_str(dmsg_msg_t *msg)
1100c3a8cd0SMatthew Dillon {
1110c3a8cd0SMatthew Dillon dmsg_state_t *state;
1120c3a8cd0SMatthew Dillon static char buf[256];
1130c3a8cd0SMatthew Dillon char errbuf[16];
1140c3a8cd0SMatthew Dillon char statebuf[64];
1150c3a8cd0SMatthew Dillon char flagbuf[64];
1160c3a8cd0SMatthew Dillon const char *statestr;
1170c3a8cd0SMatthew Dillon const char *errstr;
1180c3a8cd0SMatthew Dillon uint32_t basecmd;
1190c3a8cd0SMatthew Dillon int i;
1200c3a8cd0SMatthew Dillon
1210c3a8cd0SMatthew Dillon /*
1220c3a8cd0SMatthew Dillon * Parse the state
1230c3a8cd0SMatthew Dillon */
1240c3a8cd0SMatthew Dillon if ((state = msg->state) != NULL) {
1250c3a8cd0SMatthew Dillon basecmd = (state->rxcmd & DMSGF_REPLY) ?
1260c3a8cd0SMatthew Dillon state->txcmd : state->rxcmd;
1270c3a8cd0SMatthew Dillon snprintf(statebuf, sizeof(statebuf),
1280c3a8cd0SMatthew Dillon " %s=%s,L=%s%s,R=%s%s",
1290c3a8cd0SMatthew Dillon ((state->txcmd & DMSGF_REPLY) ?
1300c3a8cd0SMatthew Dillon "rcvcmd" : "sndcmd"),
1310c3a8cd0SMatthew Dillon dmsg_basecmd_str(basecmd),
1320c3a8cd0SMatthew Dillon ((state->txcmd & DMSGF_CREATE) ? "C" : ""),
1330c3a8cd0SMatthew Dillon ((state->txcmd & DMSGF_DELETE) ? "D" : ""),
1340c3a8cd0SMatthew Dillon ((state->rxcmd & DMSGF_CREATE) ? "C" : ""),
1350c3a8cd0SMatthew Dillon ((state->rxcmd & DMSGF_DELETE) ? "D" : "")
1360c3a8cd0SMatthew Dillon );
1370c3a8cd0SMatthew Dillon statestr = statebuf;
1380c3a8cd0SMatthew Dillon } else {
1390c3a8cd0SMatthew Dillon statestr = "";
1400c3a8cd0SMatthew Dillon }
1410c3a8cd0SMatthew Dillon
1420c3a8cd0SMatthew Dillon /*
1430c3a8cd0SMatthew Dillon * Parse the error
1440c3a8cd0SMatthew Dillon */
1450c3a8cd0SMatthew Dillon switch(msg->any.head.error) {
1460c3a8cd0SMatthew Dillon case 0:
1470c3a8cd0SMatthew Dillon errstr = "";
1480c3a8cd0SMatthew Dillon break;
1490c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_SYNC:
1500c3a8cd0SMatthew Dillon errstr = "err=IOQ:NOSYNC";
1510c3a8cd0SMatthew Dillon break;
1520c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_EOF:
1530c3a8cd0SMatthew Dillon errstr = "err=IOQ:STREAMEOF";
1540c3a8cd0SMatthew Dillon break;
1550c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_SOCK:
1560c3a8cd0SMatthew Dillon errstr = "err=IOQ:SOCKERR";
1570c3a8cd0SMatthew Dillon break;
1580c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_FIELD:
1590c3a8cd0SMatthew Dillon errstr = "err=IOQ:BADFIELD";
1600c3a8cd0SMatthew Dillon break;
1610c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_HCRC:
1620c3a8cd0SMatthew Dillon errstr = "err=IOQ:BADHCRC";
1630c3a8cd0SMatthew Dillon break;
1640c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_XCRC:
1650c3a8cd0SMatthew Dillon errstr = "err=IOQ:BADXCRC";
1660c3a8cd0SMatthew Dillon break;
1670c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_ACRC:
1680c3a8cd0SMatthew Dillon errstr = "err=IOQ:BADACRC";
1690c3a8cd0SMatthew Dillon break;
1700c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_STATE:
1710c3a8cd0SMatthew Dillon errstr = "err=IOQ:BADSTATE";
1720c3a8cd0SMatthew Dillon break;
1730c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_NOPEER:
1740c3a8cd0SMatthew Dillon errstr = "err=IOQ:PEERCONFIG";
1750c3a8cd0SMatthew Dillon break;
1760c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_NORKEY:
1770c3a8cd0SMatthew Dillon errstr = "err=IOQ:BADRKEY";
1780c3a8cd0SMatthew Dillon break;
1790c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_NOLKEY:
1800c3a8cd0SMatthew Dillon errstr = "err=IOQ:BADLKEY";
1810c3a8cd0SMatthew Dillon break;
1820c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_KEYXCHGFAIL:
1830c3a8cd0SMatthew Dillon errstr = "err=IOQ:BADKEYXCHG";
1840c3a8cd0SMatthew Dillon break;
1850c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_KEYFMT:
1860c3a8cd0SMatthew Dillon errstr = "err=IOQ:BADFMT";
1870c3a8cd0SMatthew Dillon break;
1880c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_BADURANDOM:
1890c3a8cd0SMatthew Dillon errstr = "err=IOQ:BADRANDOM";
1900c3a8cd0SMatthew Dillon break;
1910c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_MSGSEQ:
1920c3a8cd0SMatthew Dillon errstr = "err=IOQ:BADSEQ";
1930c3a8cd0SMatthew Dillon break;
1940c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_EALREADY:
1950c3a8cd0SMatthew Dillon errstr = "err=IOQ:DUPMSG";
1960c3a8cd0SMatthew Dillon break;
1970c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_TRANS:
1980c3a8cd0SMatthew Dillon errstr = "err=IOQ:BADTRANS";
1990c3a8cd0SMatthew Dillon break;
2000c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_IVWRAP:
2010c3a8cd0SMatthew Dillon errstr = "err=IOQ:IVWRAP";
2020c3a8cd0SMatthew Dillon break;
2030c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_MACFAIL:
2040c3a8cd0SMatthew Dillon errstr = "err=IOQ:MACFAIL";
2050c3a8cd0SMatthew Dillon break;
2060c3a8cd0SMatthew Dillon case DMSG_IOQ_ERROR_ALGO:
2070c3a8cd0SMatthew Dillon errstr = "err=IOQ:ALGOFAIL";
2080c3a8cd0SMatthew Dillon break;
2090c3a8cd0SMatthew Dillon case DMSG_ERR_NOSUPP:
2100c3a8cd0SMatthew Dillon errstr = "err=NOSUPPORT";
2110c3a8cd0SMatthew Dillon break;
2120c3a8cd0SMatthew Dillon default:
2130c3a8cd0SMatthew Dillon snprintf(errbuf, sizeof(errbuf),
2140c3a8cd0SMatthew Dillon " err=%d", msg->any.head.error);
2150c3a8cd0SMatthew Dillon errstr = errbuf;
2160c3a8cd0SMatthew Dillon break;
2170c3a8cd0SMatthew Dillon }
2180c3a8cd0SMatthew Dillon
2190c3a8cd0SMatthew Dillon /*
2200c3a8cd0SMatthew Dillon * Message flags
2210c3a8cd0SMatthew Dillon */
2220c3a8cd0SMatthew Dillon i = 0;
2230c3a8cd0SMatthew Dillon if (msg->any.head.cmd & (DMSGF_CREATE | DMSGF_DELETE |
2240c3a8cd0SMatthew Dillon DMSGF_ABORT | DMSGF_REPLY)) {
2250c3a8cd0SMatthew Dillon flagbuf[i++] = '|';
2260c3a8cd0SMatthew Dillon if (msg->any.head.cmd & DMSGF_CREATE)
2270c3a8cd0SMatthew Dillon flagbuf[i++] = 'C';
2280c3a8cd0SMatthew Dillon if (msg->any.head.cmd & DMSGF_DELETE)
2290c3a8cd0SMatthew Dillon flagbuf[i++] = 'D';
2300c3a8cd0SMatthew Dillon if (msg->any.head.cmd & DMSGF_REPLY)
2310c3a8cd0SMatthew Dillon flagbuf[i++] = 'R';
2320c3a8cd0SMatthew Dillon if (msg->any.head.cmd & DMSGF_ABORT)
2330c3a8cd0SMatthew Dillon flagbuf[i++] = 'A';
2340c3a8cd0SMatthew Dillon }
2350c3a8cd0SMatthew Dillon flagbuf[i] = 0;
2360c3a8cd0SMatthew Dillon
2370c3a8cd0SMatthew Dillon /*
2380c3a8cd0SMatthew Dillon * Generate the buf
2390c3a8cd0SMatthew Dillon */
2400c3a8cd0SMatthew Dillon snprintf(buf, sizeof(buf),
241*0a9eefcaSMatthew Dillon "msg=%s%s %s %s hcrc=%08x id=%016jx",
2420c3a8cd0SMatthew Dillon dmsg_basecmd_str(msg->any.head.cmd),
2430c3a8cd0SMatthew Dillon flagbuf,
2440c3a8cd0SMatthew Dillon errstr,
245*0a9eefcaSMatthew Dillon statestr,
246*0a9eefcaSMatthew Dillon msg->any.head.hdr_crc,
247*0a9eefcaSMatthew Dillon msg->any.head.msgid);
2480c3a8cd0SMatthew Dillon
2490c3a8cd0SMatthew Dillon return(buf);
2500c3a8cd0SMatthew Dillon }
251