Abstract value_string as common utility
Originally, we use this in scsi-lowlevel.c only, this works as static function. It also could be used to dump ISCSI opcode, so move it into common utils.h/utils.c. Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
This commit is contained in:
37
include/utils.h
Normal file
37
include/utils.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
Utility helper functions.
|
||||||
|
|
||||||
|
Copyright (C) 2023 by zhenwei pi <pizhenwei@bytedance.com>
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2.1 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#ifndef __iscsi_utils_h__
|
||||||
|
#define __iscsi_utils_h__
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct iscsi_value_string {
|
||||||
|
int value;
|
||||||
|
const char *string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const char *iscsi_value_string_find(struct iscsi_value_string *values, int value, const char *not_found);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __iscsi_utils_h__ */
|
||||||
@@ -6,7 +6,7 @@ libiscsipriv_la_SOURCES = \
|
|||||||
connect.c crc32c.c discovery.c init.c \
|
connect.c crc32c.c discovery.c init.c \
|
||||||
login.c nop.c pdu.c iscsi-command.c \
|
login.c nop.c pdu.c iscsi-command.c \
|
||||||
scsi-lowlevel.c socket.c sync.c task_mgmt.c \
|
scsi-lowlevel.c socket.c sync.c task_mgmt.c \
|
||||||
logging.c
|
logging.c utils.c
|
||||||
|
|
||||||
if TARGET_OS_IS_WIN32
|
if TARGET_OS_IS_WIN32
|
||||||
libiscsipriv_la_SOURCES += ../win32/win32_compat.c
|
libiscsipriv_la_SOURCES += ../win32/win32_compat.c
|
||||||
|
|||||||
@@ -56,6 +56,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "slist.h"
|
#include "slist.h"
|
||||||
#include "scsi-lowlevel.h"
|
#include "scsi-lowlevel.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
void scsi_task_set_iov_out(struct scsi_task *task, struct scsi_iovec *iov, int niov);
|
void scsi_task_set_iov_out(struct scsi_task *task, struct scsi_iovec *iov, int niov);
|
||||||
|
|
||||||
@@ -116,26 +117,10 @@ scsi_malloc(struct scsi_task *task, size_t size)
|
|||||||
return &mem->buf[0];
|
return &mem->buf[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
struct value_string {
|
|
||||||
int value;
|
|
||||||
const char *string;
|
|
||||||
};
|
|
||||||
|
|
||||||
static const char *
|
|
||||||
value_string_find(struct value_string *values, int value)
|
|
||||||
{
|
|
||||||
for (; values->string; values++) {
|
|
||||||
if (value == values->value) {
|
|
||||||
return values->string;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
scsi_sense_key_str(int key)
|
scsi_sense_key_str(int key)
|
||||||
{
|
{
|
||||||
static struct value_string keys[] = {
|
static struct iscsi_value_string keys[] = {
|
||||||
{SCSI_SENSE_NO_SENSE,
|
{SCSI_SENSE_NO_SENSE,
|
||||||
"NO SENSE"},
|
"NO SENSE"},
|
||||||
{SCSI_SENSE_RECOVERED_ERROR,
|
{SCSI_SENSE_RECOVERED_ERROR,
|
||||||
@@ -167,13 +152,13 @@ scsi_sense_key_str(int key)
|
|||||||
{0, NULL}
|
{0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
return value_string_find(keys, key);
|
return iscsi_value_string_find(keys, key, "UNKNOWN");
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
scsi_sense_ascq_str(int ascq)
|
scsi_sense_ascq_str(int ascq)
|
||||||
{
|
{
|
||||||
static struct value_string ascqs[] = {
|
static struct iscsi_value_string ascqs[] = {
|
||||||
{SCSI_SENSE_ASCQ_SANITIZE_IN_PROGRESS,
|
{SCSI_SENSE_ASCQ_SANITIZE_IN_PROGRESS,
|
||||||
"SANITIZE_IN_PROGRESS"},
|
"SANITIZE_IN_PROGRESS"},
|
||||||
{SCSI_SENSE_ASCQ_WRITE_AFTER_SANITIZE_REQUIRED,
|
{SCSI_SENSE_ASCQ_WRITE_AFTER_SANITIZE_REQUIRED,
|
||||||
@@ -239,13 +224,13 @@ scsi_sense_ascq_str(int ascq)
|
|||||||
{0, NULL}
|
{0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
return value_string_find(ascqs, ascq);
|
return iscsi_value_string_find(ascqs, ascq, "UNKNOWN");
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
scsi_pr_type_str(enum scsi_persistent_out_type pr_type)
|
scsi_pr_type_str(enum scsi_persistent_out_type pr_type)
|
||||||
{
|
{
|
||||||
static struct value_string pr_type_strings[] = {
|
static struct iscsi_value_string pr_type_strings[] = {
|
||||||
{SCSI_PERSISTENT_RESERVE_TYPE_WRITE_EXCLUSIVE,
|
{SCSI_PERSISTENT_RESERVE_TYPE_WRITE_EXCLUSIVE,
|
||||||
"Write Exclusive"},
|
"Write Exclusive"},
|
||||||
{SCSI_PERSISTENT_RESERVE_TYPE_EXCLUSIVE_ACCESS,
|
{SCSI_PERSISTENT_RESERVE_TYPE_EXCLUSIVE_ACCESS,
|
||||||
@@ -261,13 +246,13 @@ scsi_pr_type_str(enum scsi_persistent_out_type pr_type)
|
|||||||
{0, NULL}
|
{0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
return value_string_find(pr_type_strings, pr_type);
|
return iscsi_value_string_find(pr_type_strings, pr_type, "UNKNOWN");
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
scsi_opcode_str(int opcode)
|
scsi_opcode_str(int opcode)
|
||||||
{
|
{
|
||||||
static struct value_string opcode_strings[] = {
|
static struct iscsi_value_string opcode_strings[] = {
|
||||||
{SCSI_OPCODE_TESTUNITREADY,
|
{SCSI_OPCODE_TESTUNITREADY,
|
||||||
"TESTUNITREADY"},
|
"TESTUNITREADY"},
|
||||||
{SCSI_OPCODE_READ6,
|
{SCSI_OPCODE_READ6,
|
||||||
@@ -361,7 +346,7 @@ scsi_opcode_str(int opcode)
|
|||||||
{0, NULL}
|
{0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
return value_string_find(opcode_strings, opcode);
|
return iscsi_value_string_find(opcode_strings, opcode, "UNKNOWN");
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t
|
uint64_t
|
||||||
|
|||||||
35
lib/utils.c
Normal file
35
lib/utils.c
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
Utility helper functions.
|
||||||
|
|
||||||
|
Copyright (C) 2023 by zhenwei pi <pizhenwei@bytedance.com>
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2.1 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#ifdef HAVE_CONFIG_H
|
||||||
|
#include "config.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
const char *iscsi_value_string_find(struct iscsi_value_string *values,
|
||||||
|
int value, const char *not_found)
|
||||||
|
{
|
||||||
|
for ( ; values->string; values++) {
|
||||||
|
if (value == values->value) {
|
||||||
|
return values->string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return not_found;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user