From b58f3b48a36821d10a3377acfcbf18113fba0c9d Mon Sep 17 00:00:00 2001 From: Chris Leech Date: Tue, 18 Dec 2012 11:27:00 -0800 Subject: [PATCH 48/48] iscsiadm: --param parsing for advanced node creation Share parse_param and apply_param code from iscsistart, allow using multiple --param options to set arbitrary fields in node mode. Signed-off-by: Chris Leech --- usr/Makefile | 2 +- usr/iscsi_param.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ usr/iscsi_param.h | 7 ++++ usr/iscsiadm.c | 16 ++++++++-- usr/iscsistart.c | 91 ++-------------------------------------------------- 5 files changed, 120 insertions(+), 91 deletions(-) create mode 100644 usr/iscsi_param.c create mode 100644 usr/iscsi_param.h diff --git a/usr/Makefile b/usr/Makefile index 3d8ee22..a7e80c0 100644 --- a/usr/Makefile +++ b/usr/Makefile @@ -40,7 +40,7 @@ SYSDEPS_SRCS = $(wildcard ../utils/sysdeps/*.o) ISCSI_LIB_SRCS = iscsi_util.o io.o auth.o iscsi_timer.o login.o log.o md5.o \ sha1.o iface.o idbm.o sysfs.o host.o session_info.o iscsi_sysfs.o \ iscsi_net_util.o iscsid_req.o transport.o iser.o cxgbi.o be2iscsi.o \ - initiator_common.o iscsi_err.o flashnode.o uip_mgmt_ipc.o \ + initiator_common.o iscsi_err.o iscsi_param.o flashnode.o uip_mgmt_ipc.o \ $(IPC_OBJ) $(SYSDEPS_SRCS) # core initiator files INITIATOR_SRCS = initiator.o scsi.o actor.o event_poll.o mgmt_ipc.o kern_err_table.o diff --git a/usr/iscsi_param.c b/usr/iscsi_param.c new file mode 100644 index 0000000..c075e8f --- /dev/null +++ b/usr/iscsi_param.c @@ -0,0 +1,95 @@ +#include +#include "log.h" +#include "config.h" +#include "idbm.h" +#include "list.h" +#include "iface.h" +#include "idbm_fields.h" +#include "iscsi_err.h" + +int apply_params(struct list_head *user_params, struct node_rec *rec) +{ + struct user_param *param; + int rc; + + /* Must init this so we can check if user overrode them */ + rec->session.initial_login_retry_max = -1; + rec->conn[0].timeo.noop_out_interval = -1; + rec->conn[0].timeo.noop_out_timeout = -1; + + list_for_each_entry(param, user_params, list) { + /* + * user may not have passed in all params that were set by + * ibft/iscsi_boot, so clear out values that might conflict + * with user overrides + */ + if (!strcmp(param->name, IFACE_NETNAME)) { + /* overriding netname so MAC will be for old netdev */ + memset(rec->iface.hwaddress, 0, + sizeof(rec->iface.hwaddress)); + } else if (!strcmp(param->name, IFACE_HWADDR)) { + /* overriding MAC so netdev will be for old MAC */ + memset(rec->iface.netdev, 0, sizeof(rec->iface.netdev)); + } else if (!strcmp(param->name, IFACE_TRANSPORTNAME)) { + /* + * switching drivers so all old binding info is no + * longer valid. Old values were either for offload + * and we are switching to software or the reverse, + * or switching types of cards (bnx2i to cxgb3i). + */ + memset(&rec->iface, 0, sizeof(rec->iface)); + iface_setup_defaults(&rec->iface); + } + } + + rc = idbm_node_set_rec_from_param(user_params, rec, 0); + if (rc) + return rc; + + /* + * For root boot we could not change this in older versions so + * if user did not override then use the defaults. + * + * Increase to account for boot using static setup. + */ + if (rec->session.initial_login_retry_max == -1) + rec->session.initial_login_retry_max = 30; + /* we used to not be able to answer so turn off */ + if (rec->conn[0].timeo.noop_out_interval == -1) + rec->conn[0].timeo.noop_out_interval = 0; + if (rec->conn[0].timeo.noop_out_timeout == -1) + rec->conn[0].timeo.noop_out_timeout = 0; + + return 0; +} + +int parse_param(struct list_head *user_params, char *param_str) +{ + struct user_param *param; + char *name, *value; + + name = param_str; + + value = strchr(param_str, '='); + if (!value) { + log_error("Invalid --param %s. Missing value.", param_str); + return ISCSI_ERR_INVAL; + } + *value = '\0'; + + value++; + if (!strlen(value)) { + log_error("Invalid --param %s. Missing value.", param_str); + return ISCSI_ERR_INVAL; + } + + param = idbm_alloc_user_param(name, value); + if (!param) { + log_error("Could not allocate memory for param."); + return ISCSI_ERR_NOMEM; + } + + list_add(¶m->list, user_params); + return 0; +} + diff --git a/usr/iscsi_param.h b/usr/iscsi_param.h new file mode 100644 index 0000000..8b7956c --- /dev/null +++ b/usr/iscsi_param.h @@ -0,0 +1,7 @@ +#ifndef ISCSI_PARAM_H +#define ISCSI_PARAM_H + +extern int parse_param(struct list_head *user_params, char *param_str); +extern int apply_params(struct list_head *user_params, struct node_rec *rec); + +#endif diff --git a/usr/iscsiadm.c b/usr/iscsiadm.c index af6d607..2003d48 100644 --- a/usr/iscsiadm.c +++ b/usr/iscsiadm.c @@ -53,6 +53,7 @@ #include "iscsi_err.h" #include "iscsi_ipc.h" #include "iscsi_timer.h" +#include "iscsi_param.h" #include "flashnode.h" static char program_name[] = "iscsiadm"; @@ -118,6 +119,7 @@ static struct option const long_options[] = {"flashnode_idx", optional_argument, NULL, 'x'}, {"portal_type", optional_argument, NULL, 'A'}, {"newroot", required_argument, NULL, 0}, + {"param", required_argument, NULL, 0}, {NULL, 0, NULL, 0}, }; static char *short_options = "RlDVhm:a:b:c:C:p:P:T:H:i:I:U:k:L:d:r:n:v:o:sSt:ux:A:"; @@ -133,7 +135,7 @@ iscsiadm -m discoverydb [ -hV ] [ -d debug_level ] [-P printlevel] [ -t type -p [ -o operation ] [ -n name ] [ -v value ] [ -lD ] ] \n\ iscsiadm -m discovery [ -hV ] [ -d debug_level ] [-P printlevel] [ -t type -p ip:port -I ifaceN ... [ -l ] ] | [ [ -p ip:port ] [ -l | -D ] ] \n\ iscsiadm -m node [ -hV ] [ -d debug_level ] [ -P printlevel ] [ -L all,manual,automatic ] [ -U all,manual,automatic ] [ -S ] [ [ -T targetname -p ip:port -I ifaceN ] [ -l | -u | -R | -s] ] \ -[ [ -o operation ] [ -n name ] [ -v value ] ]\n\ +[ [ -o operation ] [ -n name ] [ -v value ] ] [ --param=NAME=VALUE ]\n\ iscsiadm -m session [ -hV ] [ -d debug_level ] [ -P printlevel] [ -r sessionid | sysfsdir [ -R | -u | -s ] [ -o operation ] [ -n name ] [ -v value ] ]\n\ iscsiadm -m iface [ -hV ] [ -d debug_level ] [ -P printlevel ] [ -I ifacename | -H hostno|MAC ] [ [ -o operation ] [ -n name ] [ -v value ] ] [ -C ping [ -a ip ] [ -b packetsize ] [ -c count ] [ -i interval ] ]\n\ iscsiadm -m fw [ -d debug_level ] [ -l ]\n\ @@ -2834,9 +2836,11 @@ main(int argc, char **argv) uint32_t host_no = -1; struct user_param *param; struct list_head params; + struct list_head user_params; INIT_LIST_HEAD(¶ms); INIT_LIST_HEAD(&ifaces); + INIT_LIST_HEAD(&user_params); /* do not allow ctrl-c for now... */ memset(&sa_old, 0, sizeof(struct sigaction)); memset(&sa_new, 0, sizeof(struct sigaction)); @@ -2859,8 +2863,14 @@ main(int argc, char **argv) case 0: if (long_options[longindex].flag != 0) break; - if (!strcmp(long_options[longindex].name, "newroot")) + if (!strcmp(long_options[longindex].name, "newroot")) { newroot = optarg; + break; + } + if (!strcmp(long_options[longindex].name, "param")) { + parse_param(&user_params, optarg); + break; + } break; case 'k': killiscsid = atoi(optarg); @@ -3169,6 +3179,8 @@ main(int argc, char **argv) goto out; } + apply_params(&user_params, rec); + rc = exec_node_op(op, do_login, do_logout, do_show, do_rescan, do_stats, info_level, rec, ¶ms); diff --git a/usr/iscsistart.c b/usr/iscsistart.c index 6924d49..85be35b 100644 --- a/usr/iscsistart.c +++ b/usr/iscsistart.c @@ -50,6 +50,7 @@ #include "iscsid_req.h" #include "iscsi_err.h" #include "iface.h" +#include "iscsi_param.h" /* global config info */ /* initiator needs initiator name/alias */ @@ -131,99 +132,13 @@ static int stop_event_loop(void) return rc; } -static int apply_params(struct node_rec *rec) -{ - struct user_param *param; - int rc; - - /* Must init this so we can check if user overrode them */ - rec->session.initial_login_retry_max = -1; - rec->conn[0].timeo.noop_out_interval = -1; - rec->conn[0].timeo.noop_out_timeout = -1; - - list_for_each_entry(param, &user_params, list) { - /* - * user may not have passed in all params that were set by - * ibft/iscsi_boot, so clear out values that might conflict - * with user overrides - */ - if (!strcmp(param->name, IFACE_NETNAME)) { - /* overriding netname so MAC will be for old netdev */ - memset(rec->iface.hwaddress, 0, - sizeof(rec->iface.hwaddress)); - } else if (!strcmp(param->name, IFACE_HWADDR)) { - /* overriding MAC so netdev will be for old MAC */ - memset(rec->iface.netdev, 0, sizeof(rec->iface.netdev)); - } else if (!strcmp(param->name, IFACE_TRANSPORTNAME)) { - /* - * switching drivers so all old binding info is no - * longer valid. Old values were either for offload - * and we are switching to software or the reverse, - * or switching types of cards (bnx2i to cxgb3i). - */ - memset(&rec->iface, 0, sizeof(rec->iface)); - iface_setup_defaults(&rec->iface); - } - } - - rc = idbm_node_set_rec_from_param(&user_params, rec, 0); - if (rc) - return rc; - - /* - * For root boot we could not change this in older versions so - * if user did not override then use the defaults. - * - * Increase to account for boot using static setup. - */ - if (rec->session.initial_login_retry_max == -1) - rec->session.initial_login_retry_max = 30; - /* we used to not be able to answer so turn off */ - if (rec->conn[0].timeo.noop_out_interval == -1) - rec->conn[0].timeo.noop_out_interval = 0; - if (rec->conn[0].timeo.noop_out_timeout == -1) - rec->conn[0].timeo.noop_out_timeout = 0; - - return 0; -} - -static int parse_param(char *param_str) -{ - struct user_param *param; - char *name, *value; - - name = param_str; - - value = strchr(param_str, '='); - if (!value) { - log_error("Invalid --param %s. Missing value.", param_str); - return ISCSI_ERR_INVAL; - } - *value = '\0'; - - value++; - if (!strlen(value)) { - log_error("Invalid --param %s. Missing value.", param_str); - return ISCSI_ERR_INVAL; - } - - param = idbm_alloc_user_param(name, value); - if (!param) { - log_error("Could not allocate memory for param."); - return ISCSI_ERR_NOMEM; - } - - list_add(¶m->list, &user_params); - return 0; -} - static int login_session(struct node_rec *rec) { iscsiadm_req_t req; iscsiadm_rsp_t rsp; int rc, retries = 0; - rc = apply_params(rec); + rc = apply_params(&user_params, rec); if (rc) return rc; @@ -426,7 +341,7 @@ int main(int argc, char *argv[]) fw_free_targets(&targets); exit(0); case 'P': - err = parse_param(optarg); + err = parse_param(&user_params, optarg); if (err) exit(err); break; -- 1.8.1.4